Another intermediate state recording

Tue, 07 May 2013 22:18:36 +0200

author
Andy Buckley <andy@insectnation.org>
date
Tue, 07 May 2013 22:18:36 +0200
changeset 221
4f6ce5c1904d
parent 220
51b25ede6f70
child 222
3c65cf35a5b5

Another intermediate state recording

pyslha.py file | annotate | diff | comparison | revisions
     1.1 --- a/pyslha.py	Tue May 07 22:05:54 2013 +0200
     1.2 +++ b/pyslha.py	Tue May 07 22:18:36 2013 +0200
     1.3 @@ -83,6 +83,12 @@
     1.4          return ("% ." + str(precision) + "e") % var
     1.5      return str(var)
     1.6  
     1.7 +def _autotuple(a):
     1.8 +    """Automatically convert the supplied iterable to a scalar or tuple as appropriate."""
     1.9 +    if len(a) == 1:
    1.10 +        return a[0]
    1.11 +    return tuple(a)
    1.12 +
    1.13  
    1.14  class AccessError(Exception):
    1.15      "Exception object to be raised when a SLHA block is accessed in an invalid way"
    1.16 @@ -153,7 +159,7 @@
    1.17          ## Auto-convert the types in the list
    1.18          args = map(_autotype, args)
    1.19          ## Add the entry to the map, with appropriate indices
    1.20 -        self.set_value(args)
    1.21 +        self.set_value(*args)
    1.22  
    1.23      def is_single_valued(self):
    1.24          """Return true if there is only one entry, and it has no index: the
    1.25 @@ -189,19 +195,14 @@
    1.26              if len(self.entries) > 0 and not self.is_single_valued():
    1.27                  raise AccessError("Tried to set a unique value on a multi-value block")
    1.28              self.entries[None] = args[0]
    1.29 -        elif len(entry) == 2:
    1.30 -            if type(entry[0]) is not int:
    1.31 -                raise AccessError("Attempted to set a block entry with a non-integer(s) index")
    1.32 -            self.entries[entry[0]] = entry[1]
    1.33          else:
    1.34              ## Find the first non-integer -- all previous items are indices
    1.35 -            i_first_nonint = -1
    1.36 -            for i, x in enumerate(entry):
    1.37 +            for i, x in enumerate(args):
    1.38                  if type(x) is not int:
    1.39                      i_first_nonint = i
    1.40                      break
    1.41              if i_first_nonint == 0:
    1.42 -                raise AccessError("No valid integer indices found when setting a multi-index/multi-value block entry")
    1.43 +                raise AccessError("Attempted to set a block entry with a non-integer(s) index")
    1.44              elif i_first_nonint == 1:
    1.45                  self.entries[args[0]] = args[1:]
    1.46              elif i_first_nonint == len(args)-1:

mercurial