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  from rdkit import six
     7  from rdkit.VLib.Node import VLibNode
     8
     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      >>> from rdkit.six import StringIO
    27      >>> sio = StringIO()
    28      >>> node = OutputNode(dest=sio,strFunc=lambda x:'%s '%(str(x)))
    29      >>> node.AddParent(supplier)
    30      >>> node.next()
    31      1
    32      >>> sio.getvalue()
    33      '1 '
    34      >>> node.next()
    35      2
    36      >>> sio.getvalue()
    37      '1 2 '
    38
    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
    46 -  def next(self):
    47      parents = self.GetParents()
    48      args = tuple([parent.next() for parent in parents])
    49      if len(args) == 1:
    50        args = args[0]
    51      if self._dest:
    52        if self._func is not None:
    53          outp = self._func(args)
    54        else:
    55          outp = str(args)
    56        self._dest.write(outp)
    57      return args
    58
    59
    60  if six.PY3:
    61    OutputNode.__next__ = OutputNode.next
    62
    63
    64  # ------------------------------------
    65  #
    66  #  doctest boilerplate
    67  #
    68 -def _runDoctests(verbose=None):  # pragma: nocover
    69    import doctest
    70    import sys
    71    failed, _ = doctest.testmod(optionflags=doctest.ELLIPSIS, verbose=verbose)
    72    sys.exit(failed)
    73
    74
    75  if __name__ == '__main__':  # pragma: nocover
    76    _runDoctests()
    77