1.6. Processing blocks for common tasks

This is the API reference for processing blocks, that help with common tasks, when constructing a processing network.

1.6.1. Routing data in a processing network

class connectors.blocks.PassThrough(data=None)

A trivial processing block, that simply passes its input value to its output. Instances of this can be useful to distribute a single parameter to multiple inputs, if this parameter is used in several places in a processing chain.

Parameters:data – the input object

Returns the object, that has been passed with the input() method.

Returns:the given object

Specifies the input object.

Parameters:data – the input object
Returns:the PassThrough instance
class connectors.blocks.Multiplexer(selector=None)

A class, that routes one of arbitrarily many inputs to its output.

Usage Example: (the assignments _ = ... are only to make doctest ignore the return values of the operations.)

>>> import connectors
>>> # Create some test objects
>>> test1 = connectors.blocks.PassThrough(data="One")
>>> test2 = connectors.blocks.PassThrough(data="Two")
>>> multiplexer = connectors.blocks.Multiplexer()
>>> # Connect the test objects to the multiplexer
>>> # Note, how the input of the input connector of the multiplexer is accessed like a dictionary
>>> _ = multiplexer.input["1"].connect(test1.output)
>>> _ = test2.output.connect(multiplexer.input[2])    # the order, which is connected to which is not important
>>> # Select the output with the same selector, that has been passed as key, during connecting
>>> _ = multiplexer.select("1")
>>> multiplexer.output()
>>> _ = multiplexer.select(2)
>>> multiplexer.output()
Parameters:selector – the selector of the input, that shall be routed to the output

Returns the value from the selected input. If no data is found for the given selector, the first input data set, that was added, is returned.

Returns:the selected input object

Specifies an input object, that can be selected to be routed to the output. When connecting to this method, the selector, by which the given connection can be selected, can be specified with the __getitem__() overload:

Parameters:data – the input object
Returns:an ID, that can be used as a selector value t

Is used to remove an input object from the collection, when the respective connector is disconnected from the input.

Parameters:data_id – the data_id under which the input object is stored
Returns:the Multiplexer instance
replace(data_id, data)

Is used to replace an input object, when the respective connected connector has produced a new one.

  • data_id – the data_id under which the input object is stored
  • data – the new input object

data_id, because the ID does not change for the replaced data

1.6.2. Reducing the memory consumption

class connectors.blocks.WeakrefProxyGenerator(data=None)

A helper class to reduce memory usage in certain processing chains by discarding intermediate results. It takes an object as input and outputs a weak reference proxy to it. The deletion of the strong reference to the input object can triggered by connecting the output connector for the final result to this class’s delete_reference() method. This will cause the input object to be garbage collected, if no further references to it exist. This class should be used in combination with the caching of the final result, since this prevents, that the deleted input data is required to re-compute the result, when it’s retrieved repeatedly.

Parameters:data – the input object

Creates and returns a weak reference proxy to the input object. As long as the object has not been garbage collected, this proxy should behave exactly as the input object.

Returns:a weak reference proxy to the input object, if it can be weakly referenced, otherwise the object itself

Specifies the input object.

Parameters:data – the input object
Returns:the WeakrefProxyGenerator instance
delete_reference(*args, **kwargs)

Causes the strong reference to the input object to be deleted, so that the input object can be garbage collected. This connector should be connected to the output of the final result, so it is notified, when the result has been computed and the input object is no longer required.

Parameters:*args,**kwargs – these parameters just there fore compatibility with other input connectors and are not used in this method
Returns:the WeakrefProxyGenerator instance