Skip to content

string_buffer.py

StringBuffer dataclass

Bases: IO[str]

IO that uses an in-memory string buffer to load and save data. Useful for buffering data across nodes without writing to disk.

Example:

>>> from ordeq_common import StringBuffer
>>> buffer = StringBuffer()
>>> buffer.load()
''

The buffer is initially empty, unless provided with initial data:

>>> buffer = StringBuffer("Initial data")
>>> buffer.load()
'Initial data'

Saving to the buffer appends data to the existing content:

>>> buffer.save("New data")
>>> buffer.load()
'Initial dataNew data'

Example in a node:

>>> from ordeq_args import CommandLineArg
>>> from ordeq_common import StringBuffer, Static
>>> from ordeq import node, run
>>> result = StringBuffer("Greeting")
>>> @node(
...     inputs=[StringBuffer("Hello"), Static("you")],
...     outputs=result
... )
... def greet(greeting: str, name: str) -> str:
...     return f"{greeting}, {name}!"
>>> run(greet).get(result)
'Hello, you!'