Adding Block.set_value(*args) and Block documentation.

Sun, 28 Apr 2013 22:13:19 +0200

author
Andy Buckley <andy@insectnation.org>
date
Sun, 28 Apr 2013 22:13:19 +0200
changeset 203
ce90a0dace07
parent 202
d3d069f4549a
child 204
bef82eaef56e

Adding Block.set_value(*args) and Block documentation.

ChangeLog file | annotate | diff | comparison | revisions
pyslha.py file | annotate | diff | comparison | revisions
     1.1 --- a/ChangeLog	Sun Apr 28 21:56:51 2013 +0200
     1.2 +++ b/ChangeLog	Sun Apr 28 22:13:19 2013 +0200
     1.3 @@ -1,5 +1,7 @@
     1.4  2013-04-28  Andy Buckley  <andy.buckley@cern.ch>
     1.5  
     1.6 +	* Adding Block.set_value(*args) and Block documentation.
     1.7 +
     1.8  	* Various fixes to ISAWIG output (and conversion to use new
     1.9  	tuple-indexing on mixing matrices and single-value accessing on
    1.10  	ALPHA).
     2.1 --- a/pyslha.py	Sun Apr 28 21:56:51 2013 +0200
     2.2 +++ b/pyslha.py	Sun Apr 28 22:13:19 2013 +0200
     2.3 @@ -20,16 +20,14 @@
     2.4  TODOs:
     2.5  
     2.6  For 2.0.0:
     2.7 - * Provide a nice set_value(key=None, val) type function
     2.8   * Provide a Block constructor that takes a dict of entries
     2.9 - * Rename add_entry to add_entry_from_tuple or similar
    2.10   * Output column alignment cosmetics
    2.11   * Precision setting obedience in SLHA output of values
    2.12 - * Split writeSLHA into writeSLHA{Blocks,Decays}
    2.13  
    2.14  For 2.1.0:
    2.15   * Preserve comments from read -> write (needs full-line/inline comment separation?)
    2.16   * Consider whether Block should inherit direct from dict
    2.17 + * Split writeSLHA into writeSLHA{Blocks,Decays}
    2.18  
    2.19  Later/maybe:
    2.20   * Identify HERWIG decay matrix element to use in ISAWIG
    2.21 @@ -89,11 +87,31 @@
    2.22  
    2.23  class Block(object):
    2.24      """
    2.25 -    Object representation of any BLOCK elements read from the SLHA file.  Blocks
    2.26 -    have a name, may have an associated Q value, and contain a collection of data
    2.27 -    entries, each indexed by one or more keys. Types in the dictionary are
    2.28 -    numeric (int or float) when a cast from the string in the file has been
    2.29 -    possible.
    2.30 +    Object representation of any BLOCK elements read from an SLHA file.
    2.31 +
    2.32 +    Blocks have a name, may have an associated Q value, and contain a collection
    2.33 +    of data entries, each indexed by one or more keys. Entries in the dictionary
    2.34 +    are stored as numeric types (int or float) when a cast from the string in
    2.35 +    the file has been possible.
    2.36 +
    2.37 +    Block is closely related to a Python dict (and, in fact, is implemented via
    2.38 +    an OrderedDict when possible). The preferred methods of entry access use the
    2.39 +    dict-like [] operator for getting and setting, and the keys() and items()
    2.40 +    methods for iteration. Purely iterating over the object behaves like keys(),
    2.41 +    as for an ordinary dict.
    2.42 +
    2.43 +    Multiple indices are possible, especially for entries in mixing matrix
    2.44 +    blocks. These are now implemented in the natural way, e.g. for access to the
    2.45 +    (1,2) element of a mxing matrix block, use bmix[1,2] = 0.123 and print
    2.46 +    bmix[1,2]. The value() and set_value() functions behave similarly.
    2.47 +
    2.48 +    It is possible, although not usual, to store unindexed values in a
    2.49 +    block. This is only supported when that entry is the only one in the block,
    2.50 +    and it is stored in the normal fashion but with None as the lookup key. The
    2.51 +    value() method may be used without a key argument to retrieve this value, if
    2.52 +    the is_single_valued() method returns True, and similarly the set_value()
    2.53 +    method may be used to set it if only one argument is supplied and the object
    2.54 +    is compatible.
    2.55      """
    2.56      def __init__(self, name, q=None):
    2.57          self.name = name
    2.58 @@ -101,14 +119,22 @@
    2.59          self.q = _autotype(q)
    2.60  
    2.61      def add_entry(self, entry):
    2.62 -        """Add an entry to the block from an iterable (i.e. list or tuple).
    2.63 +        """Add an entry to the block from an iterable (i.e. list or tuple) or
    2.64 +        whitespace-separated string.
    2.65 +
    2.66          Indexing will be determined automatically such that there is always a
    2.67          single-element value: multi-value or None indices may be constructed
    2.68          implicitly.
    2.69          """
    2.70 +        ## If the argument is a single string, split it and proceed
    2.71 +        if type(entry) is str:
    2.72 +            entry = entry.split()
    2.73 +        ## Check that the arg is an iterable
    2.74          if not hasattr(entry, "__iter__"):
    2.75              raise AccessError("Block entries must be iterable")
    2.76 +        ## Auto-convert the types in the list
    2.77          entry = map(_autotype, entry)
    2.78 +        ## Add the entry to the map, with appropriate indices
    2.79          if len(entry) == 1:
    2.80              self.entries[None] = entry[0]
    2.81          elif len(entry) == 2:
    2.82 @@ -131,6 +157,22 @@
    2.83              raise AccessError("Tried to access unique value of multi-value block")
    2.84          return self.entries[key]
    2.85  
    2.86 +    def set_value(self, *args):
    2.87 +        """Set a value in the block with supplied key, val args (or just val for
    2.88 +        an unindexed block).
    2.89 +
    2.90 +        If no key is given, then the block must contain only one non-indexed
    2.91 +        value otherwise an AccessError exception will be raised.\
    2.92 +        """
    2.93 +        if len(args) == 0:
    2.94 +            raise AccessError("set_value() called without arguments")
    2.95 +        elif len(args) == 1:
    2.96 +            if len(self.entries) > 0 and not self.is_single_valued():
    2.97 +                raise AccessError("Tried to set a unique value on a multi-value block")
    2.98 +            self.entries[None] = args[0]
    2.99 +        else:
   2.100 +            self.entries[tuple(args[:-1])] = args[-1]
   2.101 +
   2.102      def keys(self):
   2.103          """Access the block item keys."""
   2.104          return self.entries.keys()

mercurial