rdkit.VLib.Output

Package rdkit :: Package VLib :: Module Output |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.VLib.Output

     1  #  $Id$
     2  #
     3  #  Copyright (C) 2003 Rational Discovery LLC
     4  #     All Rights Reserved
     5  #
     6  import sys
     7  from rdkit import six
     8
     9  from rdkit.VLib.Node import VLibNode
    10
    11 -class OutputNode(VLibNode):
    12    """ base class for nodes which dump output
    13
    14    Assumptions:
    15
    16      - destination supports a write() method
    17
    18      - strFunc, if provided, returns a string representation of
    19        the input
    20
    21      - inputs (parents) can be stepped through in lockstep
    22
    23
    24    Usage Example:
    25      >>> from rdkit.VLib.Supply import SupplyNode
    26      >>> supplier = SupplyNode(contents=[1,2,3])
    27      >>> from rdkit.six import StringIO
    28      >>> sio = StringIO()
    29      >>> node = OutputNode(dest=sio,strFunc=lambda x:'%s '%(str(x)))
    30      >>> node.AddParent(supplier)
    31      >>> node.next()
    32      1
    33      >>> sio.getvalue()
    34      '1 '
    35      >>> node.next()
    36      2
    37      >>> sio.getvalue()
    38      '1 2 '
    39
    40    """
    41 -  def __init__(self,dest=None,strFunc=None,**kwargs):
    42      VLibNode.__init__(self,**kwargs)
    43      self._dest = dest
    44      self._func=strFunc
    45 -  def next(self):
    46      parents = self.GetParents()
    47      args = []
    48      for parent in parents:
    49        try:
    50          args.append(parent.next())
    51        except StopIteration:
    52          raise StopIteration
    53      if len(args)>1:
    54        args = tuple(args)
    55      else:
    56        args = args[0]
    57      if self._func is not None:
    58        outp = self._func(args)
    59      else:
    60        outp = str(args)
    61      if self._dest:
    62        self._dest.write(outp)
    63      return args
    64
    65  if six.PY3:
    66      OutputNode.__next__ = OutputNode.next
    67
    68  #------------------------------------
    69  #
    70  #  doctest boilerplate
    71  #
    72 -def _test():
    73    import doctest,sys
    74    return doctest.testmod(sys.modules["__main__"])
    75
    76  if __name__ == '__main__':
    77    import sys
    78    failed,tried = _test()
    79    sys.exit(failed)
    80