Input#

class maize.core.interface.Input(*args: Any, **kwargs: Any)[source]#

Bases: Port[T]

Input port to allow receiving or parameterising data.

An input port can either be connected to another node’s output port to dynamically receive data, or set to a value (with optional default) before workflow execution to obtain a static value, making it behave analogously to Parameter.

Parameters:
  • default – Default value for the parameter

  • default_factory – The default factory in case of mutable values

  • timeout – Timeout used for continuously polling the connection for data on a blocking receive call.

  • optional – If True, this port does not have to be connected. Nodes should then check if data is available first (by a call to ready()) before accessing it. Also determines whether this port is required for the process to stay alive. If the connection to an optional port is closed by a neighbouring process, the current node will not shutdown.

  • mode – Whether to 'link', 'move' or 'copy' (default) files.

  • cached – If True, will cache the latest received value and immediately return this value when calling receive while the channel is empty. This is useful in cases where a node will run in a loop, but some inputs stay constant, as those constant inputs will only need to receive a value a single time.

name#

Unique port name

parent#

Parent node instance this port is part of

Raises:

PortException – If the port is used without a connected channel

__init__(default: T | None = None, default_factory: Callable[[], T] | None = None, timeout: float = 0.5, optional: bool = False, mode: Literal['copy', 'link', 'move'] = 'copy', cached: bool = False) None[source]#

Methods

__init__([default, default_factory, ...])

build(name, parent)

Instantiate an interface from the description.

check(value)

Checks if a value is valid using type annotations.

close()

Closes the port.

dump()

Dump any data contained in the channel.

is_connected(port)

Specifies whether the port is connected.

preload(value)

Preload a value on an input.

ready()

Specifies whether the input has data available to read.

receive()

Receives data from the port and blocks.

receive_optional()

Receives data from the port and blocks.

set(value)

Set the input to a static value.

set_channel(channel)

Set the channel associated with the port.

Attributes

active

Specifies whether the port is active or not.

changed

Returns whether the input was explicitly set

connected

Specifies whether the port is connected.

is_default

Returns whether the default value is set

is_set

Indicates whether the input has been set to a non-None value or is an active port.

path

Provides a unique path to the interface.

serialized

Provides a serialized summary of the parameter

size

Returns the approximate number of items waiting in the channel

value

Receives a value.

name

parent

datatype

doc

property active: bool#

Specifies whether the port is active or not.

build(name: str, parent: Component) _TInter#

Instantiate an interface from the description.

Parameters:
  • name – Name of the interface, will typically be the attribute name of the parent object

  • parent – Parent component instance

Returns:

Copy of the current instance, with references to the name and the parent

Return type:

_TInter

property changed: bool#

Returns whether the input was explicitly set

check(value: T) bool#

Checks if a value is valid using type annotations.

Parameters:

value – Value to typecheck

Returns:

True if the value is valid, False otherwise

Return type:

bool

close() None#

Closes the port.

This can be detected by neighbouring nodes waiting on the port, and subsequently cause them to shut down.

Raises:

PortException – If the port is not connected

property connected: bool#

Specifies whether the port is connected.

dump() list[T][source]#

Dump any data contained in the channel.

Returns:

List or all items contained in the channel

Return type:

list[T]

static is_connected(port: _PortType) TypeGuard[_PortChannel[T]]#

Specifies whether the port is connected.

property is_default: bool#

Returns whether the default value is set

property is_set: bool#

Indicates whether the input has been set to a non-None value or is an active port.

property path: tuple[str, ...]#

Provides a unique path to the interface.

preload(value: T) None[source]#

Preload a value on an input. Internal use only.

Parameters:

value – Value to set the parameter to

Raises:

ValueError – If the datatype doesn’t match with the parameter type

ready() bool[source]#

Specifies whether the input has data available to read.

This allows checking for data without a blocking receive, thus allowing nodes to use optional inputs.

Returns:

True if there is data in the channel ready to be read

Return type:

bool

Examples

>>> if self.input.ready():
...     val = self.input.receive()
... else:
...     val = 42
receive() T[source]#

Receives data from the port and blocks.

Returns:

Item received from the channel.

Return type:

T

Raises:
  • PortInterrupt – Special signal to immediately quit the node, without any further processing

  • PortException – If trying to receive from an unconnected port, or if the received value is None

Examples

>>> self.input.receive()
42

See also

Input.receive_optional

Can potentially return ‘None’, use this method when utilising optional inputs

receive_optional() T | None[source]#

Receives data from the port and blocks.

If the port has the optional flag and isn’t checked using ready() the output can be None. To avoid this behaviour use the receive method instead.

Returns:

Item received from the channel.

Return type:

T | None

Raises:
  • PortInterrupt – Special signal to immediately quit the node, without any further processing

  • PortException – If trying to receive from an unconnected port

Examples

>>> self.input.receive()
42

See also

Input.receive

Raises a PortException instead of returning None

property serialized: dict[str, Any]#

Provides a serialized summary of the parameter

set(value: T) None[source]#

Set the input to a static value.

Parameters:

value – Value to set the input to

Raises:
  • ValueError – If the datatype doesn’t match with the parameter type

  • ParameterException – When attempting to set a value for a connected port

set_channel(channel: Channel[T]) None#

Set the channel associated with the port. This needs to be called when connecting two ports together.

Parameters:

channel – An instantiated subclass of Channel

property size: int#

Returns the approximate number of items waiting in the channel

property value: T#

Receives a value. Alias for receive()