def test_combinatorSymmetry(): tl = to_list() tl_n = nop() * tl tl2 = to_list() prod = from_list([1, 2, 3]) * from_list([4.0, 5.0, 6.0]) sp = prod >> tl_n >> tl2 assert sp.run() == ([4.0, 5.0, 6.0], [1, 2, 3])
def test_pipeOnly(self, pipe): with pytest.raises(TypeError) as excinfo: maps(from_list([1])) assert "pipe" in str(excinfo.value) with pytest.raises(TypeError) as excinfo: maps(to_list()) assert "pipe" in str(excinfo.value) with pytest.raises(TypeError) as excinfo: maps(from_list([1]) >> to_list()) assert "pipe" in str(excinfo.value)
def test_mapsWithInitAndResult(self): sp = from_list([1, 2, 3]) >> self.TimesX() >> to_list() assert sp.run(1) == (1, [1, 2, 3]) assert sp.run(2) == (2, [2, 4, 6]) sp = from_list([[1, 2, 3], [4, 5, 6]]) >> maps( self.TimesX()) >> to_list() assert sp.type_init() == int assert sp.type_result() == ([int], [[int]]) assert sp.run(1) == ([1, 1], [[1, 2, 3], [4, 5, 6]]) assert sp.run(2) == ([2, 2], [[2, 4, 6], [8, 10, 12]])
def test_valueConstructorOrEnv(self): l = [10] c1 = from_list(l) assert c1.type_out() == Type.get(int) assert c1.setup((), lambda x: None) with pytest.raises(TypeError) as excinfo: c1.setup((l, ), lambda x: None) assert "type" in str(excinfo.value) c2 = from_list(item_type=int) assert c2.type_out() == Type.get(int) with pytest.raises(TypeError) as excinfo: c2.setup((), lambda x: None) assert "type" in str(excinfo.value) assert c2.setup((l, ), lambda x: None)
def test_combinatorBug(): fl1 = from_list([1, 2, 3]) fl2 = from_list([4.0, 5.0, 6.0]) prod = fl1 * fl2 assert prod.processors == [fl1, fl2] tl = to_list() n1 = nop() tl_n = tl * n1 assert tl_n.processors == [tl, n1] tl2 = to_list() sp = prod >> (nop() * nop()) >> (tl * tl2) assert sp.run() == ([1, 2, 3], [4.0, 5.0, 6.0]) sp = prod >> tl_n >> tl2 assert sp.run() == ([1, 2, 3], [4.0, 5.0, 6.0])
def test_append(self): l = [] c = to_list(l) inp = [10] * 10 sp = from_list(inp) >> c sp.run() assert l == inp
def test_initForEveryList(self): init_count = [0] class CheckGetInitialEnv(StreamProcessor): def __init__(self): super(CheckGetInitialEnv, self).__init__((), (), int, int) def setup(self, params, result): assert params == () init_count[0] += 1 def step(self, env, stream): stream.send(stream. await ()) return MayResume sp = from_list([[1, 2, 3], [4, 5, 6]]) >> maps( CheckGetInitialEnv()) >> to_list() assert sp.run() == [[1, 2, 3], [4, 5, 6]] assert init_count[0] == 2
def test_empty(self): pr = from_list(["foo"]) c = to_list() sp = pr >> pass_if(str, lambda x: False) >> c res = sp.run() assert res == []
def test_mapsBug2(self): sp = from_list([[1, 2, 3], []]) >> maps(self.TimesX()) >> to_list() assert sp.type_init() == int assert sp.type_result() == ([int], [[int]]) assert sp.run(1) == ([1, 1], [[1, 2, 3], []])
def test_noEmptyListConstructor(self): with pytest.raises(ValueError) as excinfo: from_list([]) assert "empty" in str(excinfo.value)
def test_TimesX(self): sp = from_list(item_type=int) >> self.TimesX() >> to_list() assert sp.run([], 1) == (1, [])
def test_eitherListOrItemType(self): with pytest.raises(TypeError) as excinfo: from_list() assert "item_type" in str(excinfo.value)
Give a regular expression as init value. Take strings from upstream and send the number the regex matched on the string downstream. Provide the total amount of matches a result. Regex -> int % string -> int This is the processor from the typed example above. """ def __init__(self): re_type = type(re.compile(".")) super(SearchWithRegex, self).__init__(re_type, int, str, int) def setup(self, params, result): result(0) return (params[0], [0]) def step(self, env, stream): # Stream is an object that provides the methods await, send and result. string = stream. await () amount = len(env[0].findall(string)) env[1][0] += amount stream.result(env[1][0]) stream.send(amount) return MayResume searchWithRegex = SearchWithRegex() sp = from_list(["1a33efg", "1", "a"]) >> searchWithRegex >> to_list() assert sp.run(re.compile("\d")) == (4, [3, 1, 0])
# As with the producer, we need to specify the types of the processor. # As we do not want to restrict on the upstream type, we use a type # variable: _any = Type.get() # init type, result tyoe and consumed type. super(Prints, self).__init__(str, int, _any) def setup(self, params, result): # We did not see any values recently. result(0) # We need to capture the string and a mutable amount in our environment. return (params[0], [0]) # This method will be called when we should consume a new value from # upstream. def consume(self, env, await, result): val = await () # We saw a new value... env[1][0] += 1 result(env[1][0]) print(env[0] % val) return MayResume # We bind a lambda here, as we need different prints for different # input types. prints = lambda: Prints() sp = from_list(range(0, 10)) >> prints() assert sp.run("Got value: %s") == 10
def test_noMixedTypesConstructor(self): with pytest.raises(TypeError) as excinfo: from_list([1, "foo"]) assert "item" in str(excinfo.value)
def producer(self): return from_list([1, 2, 3]) * from_list([4, 5, 6])
def test_complexTypeOut(self): p = from_list([([10], "foo")]) assert p.type_out() == Type.get([int], str)
def test_constWithListHashUnitInitType(self): p = from_list([10] * 10) assert p.type_init() == unit
from streamr import Pipe, MayResume, from_list, to_list from streamr.types import Type class Echo(Pipe): """ A pipe that echos values it retreives from upstream for a certain times, given as init. """ def __init__(self): # We need to pass an init type, an upstream type and a downstream type _any = Type.get() super(Echo, self).__init__(int, _any, _any) def setup(self, params, result): return params[0] def transform(self, env, await, send): val = await () for _ in range(0, env): send(val) return MayResume echo = lambda: Echo() sp = from_list([1, 2]) >> echo() >> to_list() assert sp.run(2) == [1, 1, 2, 2]
def producer(self): return (from_list([1, 2, 3]) * from_list([4, 5, 6]) >> (to_list() * nop()))
# Copyright (C) 2015 Richard Klees <*****@*****.**> from streamr import from_list, to_list, const, pipe, transformation, pass_if pr1 = from_list([1, 2, 3, 4, 5, 6]) co1 = to_list() sp = pr1 >> co1 print(sp.run()) pr2 = const("Hello") co2 = to_list(max_amount=10) sp = pr2 >> co2 print(sp.run()) def append_word(word): @transformation(str, str) def append(inp): return "%s %s" % (inp, word) return append def chunks(type_io, length): @pipe(type_io, type_io) def chunks(await, send): send([await () for _ in range(0, length)]) return chunks
def producer(self): return ((from_list([1, 2, 3]) >> to_list()) * (from_list([4, 5, 6]) >> nop()))
def get(ks, value): if len(ks) == 0: return value return get(ks, value[ks.pop(0)]) for k in keys: res.append((k, get(k.split("."), d))) return res # We run some tests on our way... from streamr import from_list, to_list lp = from_list([({"a": "b", "c": {"d": "e"}}, ["a", "c.d"])]) lc = to_list() sp = lp >> dict_values >> lc res = sp.run()[0] print(res) assert ("a", "b") in res assert ("c.d", "e") in res # This is how we get the values we need to replace: we take a string, # turn it to json, search for the placeholders and join this with # our custom dict_values transformation. get_values = tee() >> to_json * search("{{([^}]+)}}") >> dict_values
class TestListProducer(_TestProducer): @pytest.fixture(params=[(from_list(vlist=[10] * 10), [10] * 10, ()), (from_list(item_type=int), [10] * 10, ([10] * 10, ))]) def producers(self, request, max_amount): return request.param @pytest.fixture def max_amount(self): return 10 @pytest.fixture def producer(self, producers): return producers[0] @pytest.fixture def env_params(self, producers): return producers[2] @pytest.fixture def result(self, producers): return producers[1] def test_eitherListOrItemType(self): with pytest.raises(TypeError) as excinfo: from_list() assert "item_type" in str(excinfo.value) def test_valueConstructorOrEnv(self): l = [10] c1 = from_list(l) assert c1.type_out() == Type.get(int) assert c1.setup((), lambda x: None) with pytest.raises(TypeError) as excinfo: c1.setup((l, ), lambda x: None) assert "type" in str(excinfo.value) c2 = from_list(item_type=int) assert c2.type_out() == Type.get(int) with pytest.raises(TypeError) as excinfo: c2.setup((), lambda x: None) assert "type" in str(excinfo.value) assert c2.setup((l, ), lambda x: None) def test_noEmptyListConstructor(self): with pytest.raises(ValueError) as excinfo: from_list([]) assert "empty" in str(excinfo.value) def test_noMixedTypesConstructor(self): with pytest.raises(TypeError) as excinfo: from_list([1, "foo"]) assert "item" in str(excinfo.value) def test_constWithListHashUnitInitType(self): p = from_list([10] * 10) assert p.type_init() == unit def test_complexTypeOut(self): p = from_list([([10], "foo")]) assert p.type_out() == Type.get([int], str)