def test_fixed_map(self): def func(): yield [1, 2, 3] yield [4, 5, 6] out = ts.Reduce(*[x + 1 for x in ts.Func(func).map(3)]) assert ts.run(out) == [(2, 3, 4), (5, 6, 7)]
def test_reduce_stateful(self): def func1(): yield 0 yield 2 yield 4 def func2(): yield 1 yield 3 yield 5 def func3(): yield 2 yield 4 yield 6 def reduce(node1_value, node2_value, node3_value, reducer_node): if not hasattr(reducer_node, "state"): # on first call, make sure node tracks state reducer_node.set("state", {"n1": None, "n2": None, "n3": None}) if node1_value is not None: reducer_node.state["n1"] = node1_value if node2_value is not None: reducer_node.state["n2"] = node2_value if node3_value is not None: reducer_node.state["n3"] = node3_value return reducer_node.state n1 = ts.Func(func1) n2 = ts.Func(func2) n3 = ts.Func(func3) r = ts.Reduce(n1, n2, n3, reducer=reduce, inject_node=True) out = ts.run(r) print(out) assert out == [ { "n1": 0, "n2": 1, "n3": 2 }, { "n1": 2, "n2": 3, "n3": 4 }, { "n1": 4, "n2": 5, "n3": 6 }, ]
def test_reduce(self): def foo1(): yield 1 yield 4 def foo2(): yield 2 yield 5 yield 7 def foo3(): yield 3 yield 6 yield 8 out = ts.Reduce(ts.Foo(foo1), ts.Foo(foo2), ts.Foo(foo3)) assert ts.run(out) == [(1, 2, 3), (4, 5, 6)]
def test_reduce(self): def func1(): yield 1 yield 4 def func2(): yield 2 yield 5 yield 7 def func3(): yield 3 yield 6 yield 8 out = ts.Reduce(ts.Func(func1), ts.Func(func2), ts.Func(func3)) assert ts.run(out) == [(1, 2, 3), (4, 5, 6)]
def test_interval(self): def reducer(short, long, node): if not node.has("state"): node.set("state", {"long": 0, "short": 0}) if long: node.state["long"] += long if short: node.state["short"] += short return node.state.copy() async def short(): await sleep(1) return 1 async def long(): await sleep(2) return 2 def interval(func, time=1): task = None async def _ret(): nonlocal task if task is None: task = asyncio.ensure_future(func()) if not task.done(): await sleep(time) return None result = task.result() task = None return result return _ret short_node = ts.Func(interval(short, 1), count=5) long_node = ts.Func(interval(long, 1), count=5) out = ts.Reduce(short_node, long_node, reducer=reducer, inject_node=True).print() ts.run(out)