More tidying and tweaking

Sun, 28 Apr 2013 19:21:33 +0200

author
Andy Buckley <andy@insectnation.org>
date
Sun, 28 Apr 2013 19:21:33 +0200
changeset 196
7caa9b877b18
parent 195
880af4ab57ba
child 197
fdcc9cd1e00d

More tidying and tweaking

pyslha.py file | annotate | diff | comparison | revisions
testorder file | annotate | diff | comparison | revisions
     1.1 --- a/pyslha.py	Sun Apr 28 18:41:46 2013 +0200
     1.2 +++ b/pyslha.py	Sun Apr 28 19:21:33 2013 +0200
     1.3 @@ -98,49 +98,70 @@
     1.4          self.q = _autotype(q)
     1.5  
     1.6      def add_entry(self, entry):
     1.7 -        if type(entry) is str:
     1.8 -            raise Exception("Block entries must be tuples or lists")
     1.9 +        """Add an entry to the block from an iterable (i.e. list or tuple).
    1.10 +        Indexing will be determined automatically such that there is always a
    1.11 +        single-element value: multi-value or None indices may be constructed
    1.12 +        implicitly.
    1.13 +        """
    1.14 +        if not hasattr(entry, "__iter__"):
    1.15 +            raise AccessError("Block entries must be iterable")
    1.16          entry = map(_autotype, entry)
    1.17 -        if len(entry) < 2:
    1.18 -            raise Exception("Block entry tuples must have at least two entries")
    1.19 +        if len(entry) == 1:
    1.20 +            self.entries[None] = entry[0]
    1.21          elif len(entry) == 2:
    1.22              self.entries[entry[0]] = entry[1]
    1.23          else:
    1.24              self.entries[tuple(entry[:-1])] = entry[-1]
    1.25  
    1.26 +    def is_single_valued(self):
    1.27 +        """Return true if there is only one entry, and it has no index: the
    1.28 +        'value()' attribute may be used in that case without an argument."""
    1.29 +        return not( len(self.entries) == 1 and self.entries.keys[0] is None )
    1.30 +
    1.31 +    def value(self, key=None):
    1.32 +        """Get a value from the block with the supplied key.
    1.33 +
    1.34 +        If no key is given, then the block must contain only one non-indexed
    1.35 +        value otherwise an AccessError exception will be raised.\
    1.36 +        """
    1.37 +        if key == None and not self.is_single_valued():
    1.38 +            raise AccessError("Tried to access unique value of multi-value block")
    1.39 +        return self.entries[key]
    1.40 +
    1.41      def items(self, key=None):
    1.42 +        """Access the block items as (key,value) tuples.
    1.43 +
    1.44 +        Note: The Python 3 dict attribute 'items()' is used rather than the
    1.45 +        'old' Python 2 'iteritems()' name for forward-looking compatibility.\
    1.46 +        """
    1.47          return self.entries.iteritems()
    1.48  
    1.49 -    @property
    1.50 -    def value(self):
    1.51 -        if len(self.entries) != 1:
    1.52 -            raise AccessError("Request for unique value of a multi-value block")
    1.53 -        return self.entries.values()[0]
    1.54 -
    1.55      def __len__(self):
    1.56          return len(self.entries)
    1.57  
    1.58 +    def __iter(self):
    1.59 +        return self.entries.__iter__()
    1.60 +
    1.61      def __getitem__(self, key):
    1.62          return self.entries[key]
    1.63  
    1.64      def __cmp__(self, other):
    1.65 -        # TODO: Surely we should also compare the entries?
    1.66 -        return cmp(self.name, other.name)
    1.67 +        return cmp(self.name, other.name) and cmp(self.entries, other.entries)
    1.68  
    1.69      def __repr__(self):
    1.70          s = self.name
    1.71          if self.q is not None:
    1.72              s += " (Q=%s)" % self.q
    1.73 -        s += "\n"
    1.74 -        if isinstance(self.entries, dict):
    1.75 -            s += "{ " + ", ".join("%r : %r" % (k, v) for k, v in self.entries.iteritems()) + " }"
    1.76 -        else:
    1.77 -            s += str(self.entries)
    1.78 +        def _format_kv(k, v):
    1.79 +            if type(k) is not tuple:
    1.80 +                s = "%r" % k
    1.81 +            else:
    1.82 +                s = ",".join("%r" % subindex for subindex in k)
    1.83 +            s += " : %r" % v
    1.84 +            return s
    1.85 +        s += " { " + "; ".join(_format_kv(k, v) for k, v in self.items()) + " }"
    1.86          return s
    1.87  
    1.88 -    # def __repr__(self):
    1.89 -    #     return self.__str__()
    1.90 -
    1.91  
    1.92  class Decay(object):
    1.93      """
     2.1 --- a/testorder	Sun Apr 28 18:41:46 2013 +0200
     2.2 +++ b/testorder	Sun Apr 28 19:21:33 2013 +0200
     2.3 @@ -37,4 +37,7 @@
     2.4  """)
     2.5  
     2.6  
     2.7 +for block in blocks.values():
     2.8 +    print block
     2.9 +
    2.10  print pyslha.writeSLHA(blocks, decays)

mercurial