pyslha.py

changeset 4
ff16e3d891cd
parent 3
d52a5a633a6c
child 5
825fed016ac1
equal deleted inserted replaced
3:d52a5a633a6c 4:ff16e3d891cd
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 2
3 import re 3 import re
4
5 def _autotype(var):
6 if type(var) != str:
7 return var
8 if var.isdigit():
9 return int(var)
10 try:
11 f = float(var)
12 return f
13 except ValueError:
14 return var
15
4 16
5 class Block: 17 class Block:
6 def __init__(self, name): 18 def __init__(self, name):
7 self.name = name 19 self.name = name
8 self.entries = {} 20 self.entries = {}
10 def add_entry(self, entry): 22 def add_entry(self, entry):
11 #print entry 23 #print entry
12 nextparent = self.entries 24 nextparent = self.entries
13 if len(entry) < 2: 25 if len(entry) < 2:
14 raise Exception("Block entries must be at least a 2-tuple") 26 raise Exception("Block entries must be at least a 2-tuple")
27 #print "in", entry
28 entry = map(_autotype, entry)
29 #print "out", entry
15 for e in entry[:-2]: 30 for e in entry[:-2]:
16 if e is not entry[-1]: 31 if e is not entry[-1]:
17 nextparent = nextparent.setdefault(e, {}) 32 nextparent = nextparent.setdefault(e, {})
18 nextparent[entry[-2]] = entry[-1] 33 nextparent[entry[-2]] = entry[-1]
19 #print self.entries 34 #print self.entries
79 94
80 # for bname, b in sorted(blocks.iteritems()): 95 # for bname, b in sorted(blocks.iteritems()):
81 # print b 96 # print b
82 # print 97 # print
83 98
84 print blocks["MASS"].entries["25"] 99 print blocks["MASS"].entries[25]

mercurial