chainlet
latest

Documentation Topics

  • Practical Introduction to using chainlet
  • Technical Aspects of chainlet
  • Glossary

Library Overview

  • Basic Building Blocks
    • Subpackages
    • Submodules
      • chainlet.chainlink module
      • chainlet.chainsend module
      • chainlet.dataflow module
      • chainlet.driver module
      • chainlet.funclink module
      • chainlet.genlink module
      • chainlet.protolink module
      • chainlet.signals module
      • chainlet.utility module
      • chainlet.wrapper module
  • Builtin and Protocol Wrappers
  • Changelog
  • Module Index
    • chainlet package
      • Subpackages
      • Submodules
        • chainlet.chainlink module
        • chainlet.chainsend module
        • chainlet.dataflow module
        • chainlet.driver module
        • chainlet.funclink module
        • chainlet.genlink module
        • chainlet.protolink module
        • chainlet.signals module
        • chainlet.utility module
        • chainlet.wrapper module
chainlet
  • Docs »
  • chainlet package »
  • chainlet.genlink module
  • Edit on GitHub

chainlet.genlink module¶

Helpers for creating ChainLinks from generators

Tools of this module allow writing simpler code by expressing functionality via generators. The interface to other chainlet objects is automatically built around the generator. Using generators in chains allows to carry state between steps.

A regular generator can be directly used by wrapping GeneratorLink around it:

from collections import deque
from mylib import producer, consumer

def windowed_average(size=8):
    buffer = collections.deque([(yield)], maxlen=size)
    while True:
        new_value = yield(sum(buffer)/len(buffer))
        buffer.append(new_value)

producer >> GeneratorLink(windowed_average(16)) >> consumer

If a generator is used only as a chainlet, one may permanently convert it by applying a decorator:

from collections import deque
from mylib import producer, consumer

@genlet
def windowed_average(size=8):
    # ...

producer >> windowed_average(16) >> consumer
class chainlet.genlink.GeneratorLink(slave, prime=True)¶

Bases: chainlet.wrapper.WrapperMixin, chainlet.primitives.link.ChainLink

Wrapper making a generator act like a ChainLink

Parameters:
  • slave – the generator instance to wrap
  • prime (bool) – advance the generator to the next/first yield
Note:

Use the genlet() function if you wish to decorate a generator function to produce GeneratorLinks.

This class wraps a generator, using it to perform work when receiving a value and passing on the result. The slave can be any object that implements the generator protocol - the methods send, throw and close are directly called on the slave.

chainlet_send(value=None)¶

Send a value to this element for processing

close()¶

Close this element, freeing resources and blocking further interactions

throw(type, value=None, traceback=None)¶

Raise an exception in this element

class chainlet.genlink.StashedGenerator(generator_function, *args, **kwargs)¶

Bases: object

A generator iterator which can be copied/pickled before any other operations

Parameters:
  • generator_function (function) – the source generator function
  • args – positional arguments to pass to generator_function
  • kwargs – keyword arguments to pass to generator_function

This class can be used instead of instantiating a generator function. The following two calls will behave the same for all generator operations:

my_generator(1, 2, 3, foo='bar')
StashedGenerator(my_generator, 1, 2, 3, foo='bar')

However, a StashedGenerator can be pickled and unpickled before any generator operations are used on it. It explicitly disallows pickling after next(), send(), throw() or close().

def parrot(what='Polly says %s'):
    value = yield
    while True:
        value = yield (what % value)

simon = StashedGenerator(parrot, 'Simon says %s')
simon2 = pickle.loads(pickle.dumps(simon))
next(simon2)
print(simon2.send('Hello'))  # Simon says Hello
simon3 = pickle.loads(pickle.dumps(simon2))  # raise TypeError
close() → raise GeneratorExit inside generator.¶
next() → the next value, or raise StopIteration¶
send(arg) → send 'arg' into generator,¶

return next yielded value or raise StopIteration.

throw(typ[, val[, tb]]) → raise exception in generator,¶

return next yielded value or raise StopIteration.

chainlet.genlink.genlet(generator_function=None, prime=True)¶

Decorator to convert a generator function to a ChainLink

Parameters:
  • generator_function (generator) – the generator function to convert
  • prime (bool) – advance the generator to the next/first yield

When used as a decorator, this function can also be called with and without keywords.

@genlet
def pingpong():
    "Chainlet that passes on its value"
    last = yield
    while True:
        last = yield last

@genlet(prime=True)
def produce():
    "Chainlet that produces a value"
    while True:
        yield time.time()

@genlet(True)
def read(iterable):
    "Chainlet that reads from an iterable"
    for item in iterable:
        yield item
chainlet.genlink.link_generator(element)¶

Convert active generators to a GeneratorLink instance

This converter automatically constructs a GeneratorLink from any generator iterator (not a generator function). The following two lines produce the same chain:

a >> generator >> e
a >> GeneratorLink(generator) >> e

Note that the source of the generator is inconsequential. For example, a generator expression can be used to provide values:

((value, value**2) for value in range(500)) >> printlet(flatten=True)

The generator iterator is not primed when binding. This makes it suitable for producing values, but not for transforming values.

Next Previous

© Copyright 2016 - 2018 Max Fischer. Revision 4e17f999.

Built with Sphinx using a theme provided by Read the Docs.