Datatoy Logo

Decorators

Mark sources, tasks and routers

@source, source_from(...) and @splitter (importable from olympipe) record the marked object in the olympipe.registry registry under its name, then return it unchanged. That is their only effect: Pipeline does not read this registry, so execution is identical with or without the decorator.

Signature

@source                              # generator function
source_from(iterable, name=None)     # returns the iterable unchanged
@splitter                            # routing function for .split()
# also available: @task, @filter (checks the -> bool annotation),
# @class_task(method, close=None, name=None)
python
from typing import Optional, Tuple
from olympipe import Pipeline, source, source_from, splitter

@source
def numbers():
    yield from range(6)

letters = source_from(["a", "b"], name="letters")   # returns the list unchanged

@splitter
def parity(x: int) -> Tuple[Optional[int], Optional[int]]:
    return (x, None) if x % 2 == 0 else (None, x)

# The decorated objects are used exactly as before
evens, odds = Pipeline(numbers()).split(parity)
print(evens.wait_for_results([odds]))
print(Pipeline(letters).task(str.upper).wait_for_result())
Output
[[0, 2, 4], [1, 3, 5]]
['A', 'B']

How it works

source_from(iterable, name=None) records an iterable (list, range, generator) under name or, failing that, its __name__. What lets .split(fn) work without n is not @splitter but the function's Tuple[...] return annotation. @filter also checks that the return annotation is bool.