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.funclink module
  • Edit on GitHub

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 parameter value.

When receiving a :tern:`data chunk` value as part of a chain, send() acts like slave(value, *args, **kwargs). Any calls to throw() and close() are ignored.

chainlet_send(value=None)¶

Send a value to this element

class chainlet.funclink.PartialSlave¶

Bases: object

args¶
func¶
keywords¶
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)¶
Next Previous

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

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