The API

Core functionality of the lusmu library

Copyright 2013 Eniram Ltd. See the LICENSE file at the top-level directory of this distribution and at https://github.com/akaihola/lusmu/blob/master/LICENSE

class lusmu.core.BaseNode(name=None, value=<lusmu.base.DIRTY>)

Base class for Inputs and Nodes

get_value()

Return the value of the object

class lusmu.core.Input(name=None, value=<lusmu.base.DIRTY>)

The input node class for reactive programming

name (optional): string
The internal name of the Input. Used in the __repr__ of the object. If omitted, a name is automatically generated.
value (optional):
The initial value for the Input.

Examples of Inputs:

>>> input_1 = Input()  # no name, no default value
>>> input_2 = Input(value=10.0)  # input with a default value
>>> exponent = Input(name='exponent')  # named input
>>> sensor = Input(name='sensor', value=-5.3)  # named, with default
get_value()

Return the value of the Input

set_value(new_value)

Set a new value for an Input

If this caused the value to change, paints dependent Nodes dirty and returns the set of those dependent Nodes which are marked “triggered” and should be re-evaluated.

value

Return the value of the Input

class lusmu.core.Node(name=None, action=None, inputs=((), None), triggered=False)

The Node class for reactive programming

name (optional): string
The internal name of the Node. Used in the __repr__ of the object. If omitted, a name is automatically generated.
action: callable(*positional_inputs, **keyword_inputs)
The function for calculating the value of a calculated node. Values from inputs are provided in positional and keyword arguments as defined in the inputs= argument.
inputs (optional): ((Input/Node, ...), {key: Input/Node, ...})
The Nodes and Inputs whose values are used as inputs for the action. This argument can be created with Node.inputs() which provides a cleaner syntax.
triggered: boolean (default=False)
True is this Node shoud be automatically evaluated when any of its dependency Nodes or Inputs change value

Examples of Nodes:

>>> input_1, input_2, exponent = [Input() for i in range(3)]
>>> # sum Node with two positional inputs
>>> sum_node = Node(action=lambda *args: sum(args),
...                 inputs=Node.inputs(input_1, input_2))
>>> # triggered (auto-calculated) Node with two keyword inputs
>>> triggered_node = Node(
...     action=lambda a, x: a ** x,
...     inputs=Node.inputs(a=input_1, x=exponent),
...     triggered=True)
get_value()

Return Node value, evaluate if needed and paint dependents dirty

static inputs(*args, **kwargs)

Construct a value for the inputs= kwarg of the constructor

Allows writing this:

>>> inputs = [Input() for i in range(4)]
>>> node = Node(inputs=Node.inputs(inputs[0], inputs[1],
...                                kw1=inputs[2], kw2=inputs[3]))

instead of this:

>>> node = Node(inputs=([inputs[0], inputs[1]],
...                     {'kw1': inputs[2], 'kw2': inputs[3]}))
set_inputs(*args, **kwargs)

Replace current positional and keyword inputs

set_value(new_value)

Set a new value for an Input

If this caused the value to change, paints dependent Nodes dirty and returns the set of those dependent Nodes which are marked “triggered” and should be re-evaluated.

value

Return Node value, evaluate if needed and paint dependents dirty

lusmu.core.get_func_name(function, default=None)

Return the name of the function, falling back to a default

lusmu.core.items(dictionary)

Return a set-like object, a view on key/value pairs of a dict

lusmu.core.update_inputs(inputs_and_values)

Update values of multiple Inputs and trigger dependents

Use this variant of the update_inputs* functions if you don’t need to access the set of triggered dependent Nodes.

lusmu.core.update_inputs_get_triggered(inputs_and_values)

Update values of multiple Inputs and trigger dependents

This variant of the update_inputs* functions returns triggered dependent Nodes as a Python set.

lusmu.core.update_inputs_iter(inputs_and_values)

Update values of multiple Inputs and trigger dependents

This is a generator which iterates through the set of triggered dependent Nodes.

lusmu.core.values(dictionary)

Return an object providing a view on the values of a dictionary

Previous topic

Lusmu – a reactive programming library for Python

Next topic

Examples

This Page