set_value / add_entry reworking

Tue, 07 May 2013 22:01:19 +0200

author
Andy Buckley <andy@insectnation.org>
date
Tue, 07 May 2013 22:01:19 +0200
changeset 219
99ab72d8a77e
parent 218
dc6b80cfe4ac
child 220
51b25ede6f70

set_value / add_entry reworking

pyslha.py file | annotate | diff | comparison | revisions
     1.1 --- a/pyslha.py	Tue May 07 21:53:15 2013 +0200
     1.2 +++ b/pyslha.py	Tue May 07 22:01:19 2013 +0200
     1.3 @@ -135,21 +135,25 @@
     1.4              self.entries.update(entries)
     1.5          self.q = _autotype(q)
     1.6  
     1.7 -    def add_entry(self, entry):
     1.8 -        """Add an entry to the block from an iterable (i.e. list or tuple) or
     1.9 -        whitespace-separated string.
    1.10 +    # TODO: Rename? To what?
    1.11 +    def add_entry(self, args):
    1.12 +        """Add an entry to the block from an iterable (i.e. list or tuple) of
    1.13 +        strings, or from a whitespace-separated string.
    1.14  
    1.15 +        This method is just for convenience: it splits the single string
    1.16 +        argument if necessary and converts the list of strings into numeric
    1.17 +        types when possible.
    1.18          """
    1.19          ## If the argument is a single string, split it and proceed
    1.20 -        if type(entry) is str:
    1.21 -            entry = entry.split()
    1.22 +        if type(args) is str:
    1.23 +            args = args.split()
    1.24          ## Check that the arg is an iterable
    1.25 -        if not hasattr(entry, "__iter__"):
    1.26 +        if not hasattr(args, "__iter__"):
    1.27              raise AccessError("Block entries must be iterable")
    1.28          ## Auto-convert the types in the list
    1.29 -        entry = map(_autotype, entry)
    1.30 +        args = map(_autotype, args)
    1.31          ## Add the entry to the map, with appropriate indices
    1.32 -        self.set_value(entry)
    1.33 +        self.set_value(args)
    1.34  
    1.35      def is_single_valued(self):
    1.36          """Return true if there is only one entry, and it has no index: the
    1.37 @@ -167,18 +171,17 @@
    1.38          return self.entries[key]
    1.39  
    1.40      def set_value(self, *args):
    1.41 -        """Set a value in the block with supplied key, val args (or just val for
    1.42 -        an unindexed block).
    1.43 +        """Set a value in the block via supplied key/value arguments.
    1.44  
    1.45          Indexing is determined automatically: any leading integers will be
    1.46          treated as a multi-dimensional index, with the remaining entries being a
    1.47 -        multi-dimensional value. If all N values in the entry iterable are ints,
    1.48 -        then the first N-1 are treated as the index and the Nth as the value. If
    1.49 -        there is only one value it will be treated as the value of a
    1.50 -        single-valued block.
    1.51 +        (potentially multi-dimensional) value. If all N args are ints, then the
    1.52 +        first N-1 are treated as the index and the Nth as the value.
    1.53  
    1.54 -        If no key is given, then the block must contain only one non-indexed
    1.55 -        value otherwise an AccessError exception will be raised.\
    1.56 +        If there is only one arg it will be treated as the value of a
    1.57 +        single-valued block. In this case the block must already contain at most
    1.58 +        one non-indexed value otherwise an AccessError exception will be
    1.59 +        raised.\
    1.60          """
    1.61          if len(args) == 0:
    1.62              raise AccessError("set_value() called without arguments")

mercurial