pyslha.py

changeset 194
b6709078072b
parent 193
40d024dac179
child 195
880af4ab57ba
equal deleted inserted replaced
193:40d024dac179 194:b6709078072b
105 raise Exception("Block entry tuples must have at least two entries") 105 raise Exception("Block entry tuples must have at least two entries")
106 elif len(entry) == 2: 106 elif len(entry) == 2:
107 self.entries[entry[0]] = entry[1] 107 self.entries[entry[0]] = entry[1]
108 else: 108 else:
109 self.entries[tuple(entry[:-1])] = entry[-1] 109 self.entries[tuple(entry[:-1])] = entry[-1]
110
111 # def is_indexed(self, entry):
112 # """Determine if this block is a multi-value object or just a single
113 # value. Emptiness is considered as being indexed."""
114 # return not( len(self.entries) == 1 and self.entries.keys()[0] is None )
115 110
116 def items(self, key): 111 def items(self, key):
117 return self.entries.iteritems() 112 return self.entries.iteritems()
118 113
119 @property 114 @property
269 if currentblock is not None: 264 if currentblock is not None:
270 items = line.split() 265 items = line.split()
271 if len(items) < 1: 266 if len(items) < 1:
272 continue 267 continue
273 if currentblock != "DECAY": 268 if currentblock != "DECAY":
274 if len(items) < 2: 269 if len(items) > 1:
275 ## Treat the ALPHA block differently 270 blocks[currentblock].add_entry(items)
276 blocks[currentblock].value = _autotype(items[0])
277 blocks[currentblock].entries = _autotype(items[0])
278 else: 271 else:
279 blocks[currentblock].add_entry(items) 272 ## The ALPHA block in particular only has one, non-indexed value
273 blocks[currentblock].entries[None] = _autotype(items[0])
280 else: 274 else:
281 br = float(items[0]) if items[0].upper() != "NAN" else None 275 br = float(items[0]) if items[0].upper() != "NAN" else None
282 nda = int(items[1]) 276 nda = int(items[1])
283 ids = map(int, items[2:]) 277 ids = map(int, items[2:])
284 if br > 0.0 or not ignorenobr: # br == None is < 0 278 if br > 0.0 or not ignorenobr: # br == None is < 0
317 blockstr = "BLOCK %s\n" % namestr 311 blockstr = "BLOCK %s\n" % namestr
318 entrystrs = [] 312 entrystrs = []
319 for k, v in b.entries.iteritems(): 313 for k, v in b.entries.iteritems():
320 entrystr = "" 314 entrystr = ""
321 if type(k) == tuple: 315 if type(k) == tuple:
322 entrystr = sep.join(_autostr(i) for i in k) 316 entrystr += sep.join(_autostr(i) for i in k)
323 else: 317 elif k is not None:
324 entrystr = _autostr(k) 318 entrystr += _autostr(k)
325 entrystr += sep + _autostr(v) # TODO: apply precision formatting for floats 319 entrystr += sep + _autostr(v) # TODO: apply precision formatting for floats
326 entrystrs.append(entrystr) 320 entrystrs.append(entrystr)
327 blockstr += "\n".join(entrystrs) 321 blockstr += "\n".join(entrystrs)
328 blockstrs.append(blockstr) 322 blockstrs.append(blockstr)
329 ## 323 ##

mercurial