pyslha.py

changeset 193
40d024dac179
parent 192
453a523cba25
child 194
b6709078072b
equal deleted inserted replaced
192:453a523cba25 193:40d024dac179
19 19
20 TODOs: 20 TODOs:
21 21
22 For 1.6.0 (or 2.0.0 if changes are really sweeping): 22 For 1.6.0 (or 2.0.0 if changes are really sweeping):
23 * Convert ISAWIG reader/writer to use new block entries access scheme 23 * Convert ISAWIG reader/writer to use new block entries access scheme
24 * Improve Block.__str__/__repr__
24 * Direct [] access to decay info on Decay 25 * Direct [] access to decay info on Decay
25 * Block (and Decay) to have __iter__/items() accesssors... or inherit direct from dict? 26 * Block (and Decay) to have __iter__/items() accesssors... or inherit direct from dict?
26 * Preserve comments from read -> write (needs full-line/inline comment separation?) 27 * Preserve comments from read -> write (needs full-line/inline comment separation?)
27 * Output column alignment cosmetics 28 * Output column alignment cosmetics
28 * Split writeSLHA into writeSLHA{Blocks,Decays} 29 * Split writeSLHA into writeSLHA{Blocks,Decays}
65 if type(var) is float: 66 if type(var) is float:
66 return ("%." + str(precision) + "e") % var 67 return ("%." + str(precision) + "e") % var
67 return str(var) 68 return str(var)
68 69
69 70
71 class AccessError(Exception):
72 "Exception object to be raised when a SLHA block is accessed in an invalid way"
73 def __init__(self, errmsg):
74 self.msg = errmsg
75 def __str__(self):
76 return self.msg
77
70 class ParseError(Exception): 78 class ParseError(Exception):
71 "Exception object to be raised when a spectrum file/string is malformed" 79 "Exception object to be raised when a spectrum file/string is malformed"
72 def __init__(self, errmsg): 80 def __init__(self, errmsg):
73 self.msg = errmsg 81 self.msg = errmsg
74 def __str__(self): 82 def __str__(self):
98 elif len(entry) == 2: 106 elif len(entry) == 2:
99 self.entries[entry[0]] = entry[1] 107 self.entries[entry[0]] = entry[1]
100 else: 108 else:
101 self.entries[tuple(entry[:-1])] = entry[-1] 109 self.entries[tuple(entry[:-1])] = entry[-1]
102 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
116 def items(self, key):
117 return self.entries.iteritems()
118
119 @property
120 def value(self):
121 if len(self.entries) != 1:
122 raise AccessError("Request for unique value of a multi-value block")
123
124 def __len__(self):
125 return len(self.entries)
126
103 def __getitem__(self, key): 127 def __getitem__(self, key):
104 return self.entries[key] 128 return self.entries[key]
105 129
106 def __cmp__(self, other): 130 def __cmp__(self, other):
131 # TODO: Surely we should also compare the entries?
107 return cmp(self.name, other.name) 132 return cmp(self.name, other.name)
108 133
109 def __str__(self): 134 def __repr__(self):
110 s = self.name 135 s = self.name
111 if self.q is not None: 136 if self.q is not None:
112 s += " (Q=%s)" % self.q 137 s += " (Q=%s)" % self.q
113 s += "\n" 138 s += "\n"
114 s += str(self.entries) 139 if isinstance(self.entries, dict):
140 s += "{ " + ", ".join("%r : %r" % (k, v) for k, v in self.entries.iteritems()) + " }"
141 else:
142 s += str(self.entries)
115 return s 143 return s
116 144
117 def __repr__(self): 145 # def __repr__(self):
118 return self.__str__() 146 # return self.__str__()
119 147
120 148
121 class Decay(object): 149 class Decay(object):
122 """ 150 """
123 Object representing a decay entry on a particle decribed by the SLHA file. 151 Object representing a decay entry on a particle decribed by the SLHA file.

mercurial