Working out a better way to deal with single/multivalue blocks

Sun, 28 Apr 2013 18:33:16 +0200

author
Andy Buckley <andy@insectnation.org>
date
Sun, 28 Apr 2013 18:33:16 +0200
changeset 193
40d024dac179
parent 192
453a523cba25
child 194
b6709078072b

Working out a better way to deal with single/multivalue blocks

pyslha.py file | annotate | diff | comparison | revisions
     1.1 --- a/pyslha.py	Fri Apr 26 20:57:42 2013 +0200
     1.2 +++ b/pyslha.py	Sun Apr 28 18:33:16 2013 +0200
     1.3 @@ -21,6 +21,7 @@
     1.4  
     1.5  For 1.6.0 (or 2.0.0 if changes are really sweeping):
     1.6   * Convert ISAWIG reader/writer to use new block entries access scheme
     1.7 + * Improve Block.__str__/__repr__
     1.8   * Direct [] access to decay info on Decay
     1.9   * Block (and Decay) to have __iter__/items() accesssors... or inherit direct from dict?
    1.10   * Preserve comments from read -> write (needs full-line/inline comment separation?)
    1.11 @@ -67,6 +68,13 @@
    1.12      return str(var)
    1.13  
    1.14  
    1.15 +class AccessError(Exception):
    1.16 +    "Exception object to be raised when a SLHA block is accessed in an invalid way"
    1.17 +    def __init__(self, errmsg):
    1.18 +        self.msg = errmsg
    1.19 +    def __str__(self):
    1.20 +        return self.msg
    1.21 +
    1.22  class ParseError(Exception):
    1.23      "Exception object to be raised when a spectrum file/string is malformed"
    1.24      def __init__(self, errmsg):
    1.25 @@ -100,22 +108,42 @@
    1.26          else:
    1.27              self.entries[tuple(entry[:-1])] = entry[-1]
    1.28  
    1.29 +    # def is_indexed(self, entry):
    1.30 +    #     """Determine if this block is a multi-value object or just a single
    1.31 +    #     value. Emptiness is considered as being indexed."""
    1.32 +    #     return not( len(self.entries) == 1 and self.entries.keys()[0] is None )
    1.33 +
    1.34 +    def items(self, key):
    1.35 +        return self.entries.iteritems()
    1.36 +
    1.37 +    @property
    1.38 +    def value(self):
    1.39 +        if len(self.entries) != 1:
    1.40 +            raise AccessError("Request for unique value of a multi-value block")
    1.41 +
    1.42 +    def __len__(self):
    1.43 +        return len(self.entries)
    1.44 +
    1.45      def __getitem__(self, key):
    1.46          return self.entries[key]
    1.47  
    1.48      def __cmp__(self, other):
    1.49 +        # TODO: Surely we should also compare the entries?
    1.50          return cmp(self.name, other.name)
    1.51  
    1.52 -    def __str__(self):
    1.53 +    def __repr__(self):
    1.54          s = self.name
    1.55          if self.q is not None:
    1.56              s += " (Q=%s)" % self.q
    1.57          s += "\n"
    1.58 -        s += str(self.entries)
    1.59 +        if isinstance(self.entries, dict):
    1.60 +            s += "{ " + ", ".join("%r : %r" % (k, v) for k, v in self.entries.iteritems()) + " }"
    1.61 +        else:
    1.62 +            s += str(self.entries)
    1.63          return s
    1.64  
    1.65 -    def __repr__(self):
    1.66 -        return self.__str__()
    1.67 +    # def __repr__(self):
    1.68 +    #     return self.__str__()
    1.69  
    1.70  
    1.71  class Decay(object):

mercurial