Skip to content

io.py

IO

Bases: Input[T], Output[T]

Base class for all IOs in Ordeq. An IO is a class that can both load and save data. See the Ordeq IO packages for some out-of-the-box implementations (e.g., YAML, StringBuffer, etc.).

IO can also be used directly as placeholder. This can be useful when you want to pass data from one node to another, but you do not want to save the data in between:

>>> from ordeq import Input, node
>>> from ordeq_common import StringBuffer, Static

>>> hello = StringBuffer("hi")
>>> name = Static("Bob")
>>> greeting = IO[str]()
>>> greeting_capitalized = StringBuffer()

>>> @node(
...     inputs=[hello, name],
...     outputs=greeting
... )
... def greet(greeting: str, name: str) -> str:
...     return f"{greeting}, {name}!"

>>> @node(
...     inputs=greeting,
...     outputs=greeting_capitalized
... )
... def capitalize(s: str) -> str:
...     return s.capitalize()

In the example above, greeting represents the placeholder output to the node greet, as well as the placeholder input to capitalize.

When you run the nodes greeting and capitalize the result of greeting will be passed along unaffected to capitalize, much like a cache:

>>> from ordeq import run
>>> result = run(greet, capitalize)
>>> result[greeting]
'hi, Bob!'

IOException

Bases: Exception

Exception raised by IO implementations in case of load/save failure. IO implementations should provide instructive information.

Input

Bases: _InputOptions[Tin], _InputHooks[Tin], _InputReferences[Tin], _InputCache[Tin], _InputException[Tin], Generic[Tin]

Base class for all inputs in Ordeq. An Input is a class that loads data. All Input classes should implement a load method. By default, loading an input raises a NotImplementedError. See the Ordeq IO packages for some out-of-the-box implementations (e.g., Static, StringBuffer, etc.).

Input can also be used directly as placeholder. This can be useful when you are defining a node, but you do not want to provide an actual input yet. In this case, you can:

>>> from ordeq.framework import Input, node
>>> from ordeq_common import StringBuffer

>>> name = Input[str]()
>>> greeting = StringBuffer()

>>> @node(
...     inputs=name,
...     outputs=greeting
... )
... def greet(name: str) -> str:
...     return f"Hello, {name}!"

In the example above, name represents the placeholder input to the node greet. Running the node greet as-is will raise a NotImplementedError:

>>> from ordeq import run
>>> run(greet) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
NotImplementedError:

To use the greet node, we need to provide an actual input. For instance:

>>> from ordeq_common import Static
>>> result = run(greet, io={name: Static("Alice")})
>>> result[greeting]
'Hello, Alice!'

Output

Bases: _OutputOptions[Tout], _OutputHooks[Tout], _OutputReferences[Tout], _OutputException[Tout], Generic[Tout]

Base class for all outputs in Ordeq. An Output is a class that saves data. All Output classes should implement a save method. By default, saving an output does nothing. See the Ordeq IO packages for some out-of-the-box implementations (e.g., YAML, StringBuffer, etc.).

Output can also be used directly as placeholder. This can be useful when you are defining a node, but you do not want to provide an actual output. In this case, you can:

>>> from ordeq.framework import Output, node
>>> from ordeq_common import StringBuffer

>>> greeting = StringBuffer("hello")
>>> greeting_upper = Output[str]()

>>> @node(
...     inputs=greeting,
...     outputs=greeting_upper
... )
... def uppercase(greeting: str) -> str:
...     return greeting.upper()

In the example above, greeting_upper represents the placeholder output to the node uppercase. When you run the node uppercase, its result can be retrieved from the greeting_upper output. For instance:

>>> from ordeq import run
>>> result = run(uppercase)
>>> result[greeting_upper]
'HELLO'