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