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_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_replaceResultType(self): c = to_list() # List consumes values and results # in a list of those values. Yet # is has no definite type... assert c.type_in().is_variable() p = const(10) assert p.type_out() == int sp = p >> c assert sp.type_result() == [int]
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
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 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 consumer(self, style): if style == "result": return to_list() if style == "append": return to_list([])
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])
def producer(self): return ((from_list([1, 2, 3]) >> to_list()) * (from_list([4, 5, 6]) >> nop()))
def producer(self): return (from_list([1, 2, 3]) * from_list([4, 5, 6]) >> (to_list() * nop()))
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_maxAmount(self): c = to_list(max_amount=2) sp = const(10) >> c assert sp.run() == [10, 10]
# 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
# values. It can use the send method, to send values downstream. def produce(self, env, send): # Send the next value. send(env[0]) # Set next value to be send. env[0] += 1 if sys.version_info[0] != 3 and env[0] == sys.maxint: # No we definetly can't produce any more integers, so we need to # signal that we want to stop: return Stop # We can go one, but we do not need to. return MayResume # We could also return Resume to signal that we need to go on. # For some processors (e.g. those which use resources) it could be necessary # to implement a teardown method. It will be called, after the processor # finished executing, getting the environment previously produce by setup. # def teardown(self, env): # pass # As our processor only needs to exist once, we could as well create an instance # of it, that could be used anywhere. all_ints_larger_than = AllIntsLargerThan() sp = all_ints_larger_than >> to_list(max_amount=100) assert sp.run(10) == range(10, 110)
def test_TimesX(self): sp = from_list(item_type=int) >> self.TimesX() >> to_list() assert sp.run([], 1) == (1, [])
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 assert get_values.type_in() == Type.get(str)
def test_amount(self): sp = const(10, amount=2) >> to_list() assert sp.run() == [10, 10]