pyslha.py

Sun, 01 Aug 2010 20:55:29 +0200

author
Andy Buckley <andy@insectnation.org>
date
Sun, 01 Aug 2010 20:55:29 +0200
changeset 5
825fed016ac1
parent 4
ff16e3d891cd
child 6
1e9ee18259e2
permissions
-rw-r--r--

Handling Q= entries on block names

andy@1 1 #! /usr/bin/env python
andy@1 2
andy@1 3 import re
andy@1 4
andy@4 5 def _autotype(var):
andy@4 6 if type(var) != str:
andy@4 7 return var
andy@4 8 if var.isdigit():
andy@4 9 return int(var)
andy@4 10 try:
andy@4 11 f = float(var)
andy@4 12 return f
andy@4 13 except ValueError:
andy@4 14 return var
andy@4 15
andy@4 16
andy@1 17 class Block:
andy@5 18 def __init__(self, name, q=None):
andy@1 19 self.name = name
andy@1 20 self.entries = {}
andy@5 21 self.q = _autotype(q)
andy@1 22
andy@1 23 def add_entry(self, entry):
andy@1 24 #print entry
andy@1 25 nextparent = self.entries
andy@1 26 if len(entry) < 2:
andy@1 27 raise Exception("Block entries must be at least a 2-tuple")
andy@4 28 #print "in", entry
andy@4 29 entry = map(_autotype, entry)
andy@4 30 #print "out", entry
andy@1 31 for e in entry[:-2]:
andy@1 32 if e is not entry[-1]:
andy@1 33 nextparent = nextparent.setdefault(e, {})
andy@1 34 nextparent[entry[-2]] = entry[-1]
andy@1 35 #print self.entries
andy@1 36
andy@1 37 def __cmp__(self, other):
andy@1 38 return self.name < other.name
andy@1 39
andy@1 40 def __str__(self):
andy@1 41 s = self.name
andy@5 42 if self.q is not None:
andy@5 43 s += " (Q=%s)" % self.q
andy@1 44 s += "\n"
andy@1 45 s += str(self.entries)
andy@1 46 return s
andy@1 47
andy@1 48
andy@1 49 class Decay:
andy@1 50 def __init__(self, pid, totalwidth):
andy@1 51 self.pid = totalwidth
andy@1 52
andy@1 53
andy@1 54 def readSpcFile(filename):
andy@1 55 ## TODO: Use new file handling from future
andy@1 56 blocks = {}
andy@1 57 decays = {}
andy@1 58 f = open(filename, "r")
andy@1 59 currentblock = None
andy@1 60 for l in f:
andy@1 61 line = l.strip()[:-1]
andy@1 62 ## Handle (ignore) comment lines
andy@1 63 if line.startswith("#"):
andy@1 64 continue
andy@1 65 if "#" in line:
andy@1 66 line = line[:line.index("#")]
andy@1 67
andy@1 68 ## Handle BLOCK/DECAY start lines
andy@3 69 if line.upper().startswith("BLOCK"):
andy@5 70 match = re.match(r"BLOCK\s+(\w+)\s+(Q=\s*.+)?", line.upper())
andy@5 71 if not match:
andy@5 72 continue
andy@5 73 blockname = match.group(1)
andy@5 74 qstr = match.group(2)
andy@5 75 if qstr is not None:
andy@5 76 qstr = qstr[2:].strip()
andy@1 77 currentblock = blockname
andy@5 78 blocks[blockname] = Block(blockname, q=qstr)
andy@3 79 elif line.upper().startswith("DECAY"):
andy@1 80 currentblock = "DECAY"
andy@1 81 # TODO: particle details
andy@1 82 elif currentblock is not None:
andy@1 83 items = line.split()
andy@1 84 if len(items) < 2:
andy@1 85 continue
andy@1 86 # TODO: Sort out tuple item types: autoconvert integers and floats
andy@1 87 if currentblock != "DECAY":
andy@1 88 #print currentblock
andy@1 89 blocks[currentblock].add_entry(items)
andy@1 90 else:
andy@1 91 #print "NOT HANDLING DECAYS YET!"
andy@1 92 pass
andy@1 93
andy@1 94 f.close()
andy@1 95 return blocks, decays
andy@1 96
andy@1 97
andy@1 98 if __name__ == "__main__":
andy@1 99 import sys
andy@1 100 for a in sys.argv[1:]:
andy@1 101 blocks, decays = readSpcFile(a)
andy@3 102
andy@5 103 for bname, b in sorted(blocks.iteritems()):
andy@5 104 print b
andy@5 105 print
andy@3 106
andy@4 107 print blocks["MASS"].entries[25]

mercurial