pyslha.py

changeset 196
7caa9b877b18
parent 195
880af4ab57ba
child 197
fdcc9cd1e00d
equal deleted inserted replaced
195:880af4ab57ba 196:7caa9b877b18
96 self.name = name 96 self.name = name
97 self.entries = _mkdict() 97 self.entries = _mkdict()
98 self.q = _autotype(q) 98 self.q = _autotype(q)
99 99
100 def add_entry(self, entry): 100 def add_entry(self, entry):
101 if type(entry) is str: 101 """Add an entry to the block from an iterable (i.e. list or tuple).
102 raise Exception("Block entries must be tuples or lists") 102 Indexing will be determined automatically such that there is always a
103 single-element value: multi-value or None indices may be constructed
104 implicitly.
105 """
106 if not hasattr(entry, "__iter__"):
107 raise AccessError("Block entries must be iterable")
103 entry = map(_autotype, entry) 108 entry = map(_autotype, entry)
104 if len(entry) < 2: 109 if len(entry) == 1:
105 raise Exception("Block entry tuples must have at least two entries") 110 self.entries[None] = entry[0]
106 elif len(entry) == 2: 111 elif len(entry) == 2:
107 self.entries[entry[0]] = entry[1] 112 self.entries[entry[0]] = entry[1]
108 else: 113 else:
109 self.entries[tuple(entry[:-1])] = entry[-1] 114 self.entries[tuple(entry[:-1])] = entry[-1]
110 115
116 def is_single_valued(self):
117 """Return true if there is only one entry, and it has no index: the
118 'value()' attribute may be used in that case without an argument."""
119 return not( len(self.entries) == 1 and self.entries.keys[0] is None )
120
121 def value(self, key=None):
122 """Get a value from the block with the supplied key.
123
124 If no key is given, then the block must contain only one non-indexed
125 value otherwise an AccessError exception will be raised.\
126 """
127 if key == None and not self.is_single_valued():
128 raise AccessError("Tried to access unique value of multi-value block")
129 return self.entries[key]
130
111 def items(self, key=None): 131 def items(self, key=None):
132 """Access the block items as (key,value) tuples.
133
134 Note: The Python 3 dict attribute 'items()' is used rather than the
135 'old' Python 2 'iteritems()' name for forward-looking compatibility.\
136 """
112 return self.entries.iteritems() 137 return self.entries.iteritems()
113
114 @property
115 def value(self):
116 if len(self.entries) != 1:
117 raise AccessError("Request for unique value of a multi-value block")
118 return self.entries.values()[0]
119 138
120 def __len__(self): 139 def __len__(self):
121 return len(self.entries) 140 return len(self.entries)
122 141
142 def __iter(self):
143 return self.entries.__iter__()
144
123 def __getitem__(self, key): 145 def __getitem__(self, key):
124 return self.entries[key] 146 return self.entries[key]
125 147
126 def __cmp__(self, other): 148 def __cmp__(self, other):
127 # TODO: Surely we should also compare the entries? 149 return cmp(self.name, other.name) and cmp(self.entries, other.entries)
128 return cmp(self.name, other.name)
129 150
130 def __repr__(self): 151 def __repr__(self):
131 s = self.name 152 s = self.name
132 if self.q is not None: 153 if self.q is not None:
133 s += " (Q=%s)" % self.q 154 s += " (Q=%s)" % self.q
134 s += "\n" 155 def _format_kv(k, v):
135 if isinstance(self.entries, dict): 156 if type(k) is not tuple:
136 s += "{ " + ", ".join("%r : %r" % (k, v) for k, v in self.entries.iteritems()) + " }" 157 s = "%r" % k
137 else: 158 else:
138 s += str(self.entries) 159 s = ",".join("%r" % subindex for subindex in k)
160 s += " : %r" % v
161 return s
162 s += " { " + "; ".join(_format_kv(k, v) for k, v in self.items()) + " }"
139 return s 163 return s
140
141 # def __repr__(self):
142 # return self.__str__()
143 164
144 165
145 class Decay(object): 166 class Decay(object):
146 """ 167 """
147 Object representing a decay entry on a particle decribed by the SLHA file. 168 Object representing a decay entry on a particle decribed by the SLHA file.

mercurial