This is the commit where I admit that having more than one input/output format for readers and writers was complicating the code too much for a very small gain, and that it would be easier to only have one way to do it. So such way is now: - Returning (or yielding) a dict if you have key-value type collections. - Returning (or yielding) a tuple if you have a list-type collection. - Returning (or yielding) something else otherwise, which will continue to work like the old "arg0" format. IOFORMAT options has been removed in favour of a RemovedOption, which will complain if you're still trying to set it to anything else than the one value allowed.
82 lines
1.6 KiB
Python
82 lines
1.6 KiB
Python
from bonobo.config.processors import ContextProcessor
|
|
from bonobo.constants import BEGIN, END
|
|
from bonobo.execution.graph import GraphExecutionContext
|
|
from bonobo.strategies import NaiveStrategy
|
|
from bonobo.structs import Bag, Graph
|
|
|
|
|
|
def generate_integers():
|
|
yield from range(10)
|
|
|
|
|
|
def square(i: int) -> int:
|
|
return i**2
|
|
|
|
|
|
def push_result(results, i: int):
|
|
results.append(i)
|
|
|
|
|
|
@ContextProcessor.decorate(push_result)
|
|
def results(f, context):
|
|
results = []
|
|
yield results
|
|
context.parent.results = results
|
|
|
|
|
|
chain = (generate_integers, square, push_result)
|
|
|
|
|
|
def test_empty_execution_context():
|
|
graph = Graph()
|
|
|
|
ctx = GraphExecutionContext(graph)
|
|
assert not len(ctx.nodes)
|
|
assert not len(ctx.plugins)
|
|
|
|
assert not ctx.alive
|
|
|
|
|
|
def test_execution():
|
|
graph = Graph()
|
|
graph.add_chain(*chain)
|
|
|
|
strategy = NaiveStrategy()
|
|
ctx = strategy.execute(graph)
|
|
|
|
assert ctx.results == [1, 4, 9, 16, 25, 36, 49, 64, 81]
|
|
|
|
|
|
def test_simple_execution_context():
|
|
graph = Graph()
|
|
graph.add_chain(*chain)
|
|
|
|
context = GraphExecutionContext(graph)
|
|
assert len(context.nodes) == len(chain)
|
|
assert not len(context.plugins)
|
|
|
|
for i, node in enumerate(chain):
|
|
assert context[i].wrapped is node
|
|
|
|
assert not context.alive
|
|
assert not context.started
|
|
assert not context.stopped
|
|
|
|
context.write(BEGIN, Bag(), END)
|
|
|
|
assert not context.alive
|
|
assert not context.started
|
|
assert not context.stopped
|
|
|
|
context.start()
|
|
|
|
assert context.alive
|
|
assert context.started
|
|
assert not context.stopped
|
|
|
|
context.stop()
|
|
|
|
assert not context.alive
|
|
assert context.started
|
|
assert context.stopped
|