Dispatchers

piecutter‘s dispatchers are made to configure pipelines. As a summary, use a dispatcher wherever you could use a single callable.

Dispatchers are configurable callables.

Note

In future releases, dispatchers may be distributed in their own library, because they provide features that could be useful apart from piecutter, i.e. piecutter would depend on this external library.

FirstResultDispatcher

Iterate over callables in pipeline, calling each one with original arguments. As soon as one returns “non empty” result, break and return this result.

>>> from piecutter.utils import dispatchers

>>> zero = lambda x: None
>>> one = lambda x: dispatchers.NO_RESULT
>>> two = lambda x: 'two {thing}'.format(thing=x)
>>> three = lambda x: 'three {thing}'.format(thing=x)
>>> callable = dispatchers.FirstResultDispatcher([zero, one, two, three])
>>> print(callable('sheep'))
two sheep

LastResultDispatcher

Iterate over callables in pipeline, call each one with original arguments. Only remember last “non empty” result. At the end, return this result.

>>> one = lambda x: 'one {thing}'.format(thing=x)
>>> two = lambda x: 'two {thing}'.format(thing=x)
>>> three = lambda x: 'three {thing}'.format(thing=x)
>>> callable = dispatchers.LastResultDispatcher([zero, one, two, three])
>>> print(callable('sheep'))
three sheep

ChainDispatcher

Iterate over callables in pipeline, call the first one with original arguments, then use the current output as input for the next callable... Return the value returned by the last callable in the pipeline.

>>> strip = lambda x: x.strip()
>>> upper = lambda x: x.upper()
>>> strong = lambda x: '<strong>{thing}</strong>'.format(thing=x)
>>> callable = dispatchers.ChainDispatcher([strip, upper, strong])
>>> print(callable('    sheep           '))
<strong>SHEEP</strong>