pyslha.py

Sun, 01 Aug 2010 15:56:33 +0200

author
Andy Buckley <andy@insectnation.org>
date
Sun, 01 Aug 2010 15:56:33 +0200
changeset 4
ff16e3d891cd
parent 3
d52a5a633a6c
child 5
825fed016ac1
permissions
-rw-r--r--

Automatic string to numeric type conversion

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

mercurial