chainlet.funclink module¶
Helpers for creating ChainLinks from functions
Tools of this module allow writing simpler code by expressing functionality via functions. The interface to other chainlet objects is automatically built around the functions. Using functions in chains allows for simple, stateless blocks.
A regular function can be directly used by wrapping FunctionLink
around it:
from mylib import producer, consumer
def stepper(value, resolution=10):
return (value // resolution) * resolution
producer >> FunctionLink(stepper, 20) >> consumer
If a function is used only as a chainlet, one may permanently convert it by applying a decorator:
from collections import deque
from mylib import producer, consumer
@GeneratorLink.linklet
def stepper(value, resolution=10):
# ...
producer >> stepper(20) >> consumer
-
class
chainlet.funclink.
FunctionLink
(slave, *args, **kwargs)¶ Bases:
chainlet.wrapper.WrapperMixin
,chainlet.primitives.link.ChainLink
Wrapper making a function act like a ChainLink
Parameters: - slave – the function to wrap
- args – positional arguments for the slave
- kwargs – keyword arguments for the slave
Note: Use the
funclet()
function if you wish to decorate a function to produce FunctionLinks.This class wraps a function (or other callable), calling it to perform work when receiving a value and passing on the result. The
slave
can be any object that is callable, and should take at least a named parametervalue
.When receiving a :tern:`data chunk`
value
as part of a chain,send()
acts likeslave(value, *args, **kwargs)
. Any calls tothrow()
andclose()
are ignored.-
chainlet_send
(value=None)¶ Send a value to this element
-
chainlet.funclink.
funclet
(function)¶ Convert a function to a
ChainLink
@funclet def square(value): "Convert every data chunk to its numerical square" return value ** 2
The data chunk
value
is passed anonymously as the first positional parameter. In other words, the wrapped function should have the signature:-
.
slave
(value, *args, **kwargs)¶
-