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

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

mercurial