def test_timed_window(self): x = Stream('x') y = Stream('y') def f(v): return v timed_window(func=f, in_stream=x, out_stream=y, window_duration=10, step_time=10) x.extend([(1, 'a'), (8, 'b'), (12, 'c')]) run() assert (recent_values(y) == [(10, [(1, 'a'), (8, 'b')])]) x.extend([(14, 'd'), (36, 'e'), (43, 'g'), (75, 'h')]) run() assert (recent_values(y) == [(10, [(1, 'a'), (8, 'b')]), (20, [(12, 'c'), (14, 'd')]), (40, [(36, 'e')]), (50, [(43, 'g')])]) x.extend([(79, 'i'), (101, 'j')]) run() assert (recent_values(y) == [(10, [(1, 'a'), (8, 'b')]), (20, [(12, 'c'), (14, 'd')]), (40, [(36, 'e')]), (50, [(43, 'g')]), (80, [(75, 'h'), (79, 'i')])]) return
def test_(self): def copy(list_of_in_lists, state): return ([ in_list.list[in_list.start:in_list.stop] for in_list in list_of_in_lists ], state, [in_list.stop for in_list in list_of_in_lists]) input_stream_0 = Stream('input_stream_0', num_in_memory=32) input_stream_1 = Stream('input_stream_1', num_in_memory=32) output_stream_0 = Stream('output_stream_0', num_in_memory=32) output_stream_1 = Stream('output_stream_1', num_in_memory=32) A = Agent(in_streams=[input_stream_0, input_stream_1], out_streams=[output_stream_0, output_stream_1], transition=copy, name='A') input_stream_0.extend(list(range(10))) run() assert (output_stream_0.stop == 10) assert (output_stream_1.stop == 0) assert (output_stream_0.recent[:10] == list(range(10))) assert (input_stream_0.start == {A: 10}) assert (input_stream_1.start == {A: 0}) input_stream_1.extend(list(range(10, 25, 1))) run() assert (output_stream_0.stop == 10) assert (output_stream_1.stop == 15) assert (output_stream_0.recent[:10] == list(range(10))) assert (output_stream_1.recent[:15] == list(range(10, 25, 1))) assert (input_stream_0.start == {A: 10}) assert (input_stream_1.start == {A: 15})
def test_class(self): class C(object): def __init__(self): return def f(self, value): if value > 0: return self.pos(value) else: return self.neg(value) def pos(self, value): return value * value def neg(self, value): return value + value s = Stream('s') t = Stream('t') c = C() @map_e def g(v): return c.f(v) g(in_stream=s, out_stream=t) s.extend(list(range(-4, 4))) run() assert (recent_values(t) == [-8, -6, -4, -2, 0, 1, 4, 9])
def test_operator_overloading(selfself): x = Stream('x') y = Stream('y') u = Stream('u') v = Stream('v') z = x + y a = x - y b = x * y c = x % u d = x // u e = x // u f = u < v x.extend(list(range(100, 120, 2))) y.extend(list(range(10))) u.extend(list(range(1, 11, 1))) v.extend(list(range(0, 20, 2))) Stream.scheduler.step() assert (recent_values(z) == [ 100, 103, 106, 109, 112, 115, 118, 121, 124, 127 ]) assert (recent_values(a) == [ 100, 101, 102, 103, 104, 105, 106, 107, 108, 109 ]) assert (recent_values(b) == [ 0, 102, 208, 318, 432, 550, 672, 798, 928, 1062 ]) assert (recent_values(c) == [0, 0, 2, 2, 3, 2, 0, 2, 8, 8]) assert (recent_values(d) == [100, 51, 34, 26, 21, 18, 16, 14, 12, 11]) assert (recent_values(f) == [ False, False, True, True, True, True, True, True, True, True ])
def test_r_sub(self): x = Stream() y = Stream() r_sub(x, y, 2) x.extend(list(range(5))) run() assert recent_values(y) == [-2, -1, 0, 1, 2]
def test_example_1(self): # Specify streams x = Stream('x') y = Stream('y') # Specify encapsulated functions (if any) def f(v): return 2 * v # Specify agents. map_element(func=f, in_stream=x, out_stream=y) # Execute a step # Put test values in the input streams. x.extend(list(range(3))) # Execute a step run() # Look at recent values of output streams. assert recent_values(y) == [0, 2, 4] # Execute a step # Put test values in the input streams. x.extend([10, 20, 30]) # Execute a step run() # Look at recent values of output streams. assert recent_values(y) == [0, 2, 4, 20, 40, 60] # Execute a step # Put test values in the input streams. x.extend([0, -10]) # Execute a step run() # Look at recent values of output streams. assert recent_values(y) == [0, 2, 4, 20, 40, 60, 0, -20]
def test_thread_2(self): @merge_sink_e def f(list_of_elements, q_out): q_out.put(sum(list_of_elements)) x = Stream('x') y = Stream('y') q_in = queue.Queue() q_out = queue.Queue() f([x, y], q_out=q_out) streams = [x, y] finished = _close thr = threading.Thread(target=self.thread_target, args=(q_in, q_out, streams, finished)) thr.start() # Put data into input queue q_in.put(('x', 1)) q_in.put(('y', 100)) q_in.put(finished) output = [] while True: w = q_out.get() if w == finished: break else: output.append(w) thr.join() assert output == [101]
def test_r_mul(self): x = Stream() y = Stream() a = r_mul(x, y, 2) x.extend(list(range(5))) run() assert recent_values(y) == [0, 2, 4, 6, 8]
def test_r_add(self): x = Stream() y = Stream() z = 5 r_add(x, y, z) x.extend(list(range(3))) run() assert recent_values(y) == [5, 6, 7]
def test_initial_value(self): def double(v): return v * 2 x = Stream('x') y = Stream(name='y', initial_value=[0] * 5) a = map_element(func=double, in_stream=x, out_stream=y) x.extend(list(range(5))) run() assert recent_values(y) == [0] * 5 + [0, 2, 4, 6, 8]
def test_None_in_stream(self): x = Stream('x', discard_None=False) y = Stream(name='y', discard_None=False) z = Stream(name='z') map_element(lambda v: v, x, y) map_element(lambda v: v, x, z) x.extend([0, None, 1, None, 2, _no_value, 3]) run() assert (recent_values(y) == [0, None, 1, None, 2, 3]) assert (recent_values(z) == [0, 1, 2, 3])
def test_simple_merge_1(self): scheduler = Stream.scheduler x = Stream('x') u = Stream('u') s = Stream('s') d = zip_map(func=sum, in_streams=[x, u], out_stream=s, name='d') x.extend(list(range(3))) u.extend([10, 15, 18]) run() assert (recent_values(s) == [10, 16, 20])
def test_sink_2(self): x = Stream() y = Stream() @sink_e def f(v, out_stream): out_stream.append(v + 100) f(x, out_stream=y) x.extend(list(range(5))) run()
def test_multiple_relations_2(self): @map_e def double(v): return v * 2 x = Stream('x', [10, 11]) y = Stream('y') double(x, y) double(x, y) x.extend(list(range(5))) run()
def test_sink_1(self): x = Stream() y = Stream() @sink_w def f(v, out_stream): out_stream.append(sum(v) + 10) f(x, window_size=2, step_size=1, out_stream=y) x.extend(list(range(5))) run()
def test_map_window_list_2(self): x = Stream() y = Stream() @map_wl def f(window, state): return [v + 10 * state for v in window], state + 1 f(x, y, window_size=2, step_size=2, state=0) x.extend(list(range(10))) run() assert recent_values(y) == [0, 1, 12, 13, 24, 25, 36, 37, 48, 49]
def test_map_with_state(self): x = Stream() y = Stream() @map_e def f(v, state): return v + state, state + 1 f(x, y, state=0) x.extend(list(range(5))) run() assert recent_values(y) == [0, 2, 4, 6, 8]
def test_map_window_with_state(self): x = Stream() y = Stream() @map_w def f(window, state): return sum(window) + state, state + 1 f(x, y, window_size=2, step_size=2, state=0) x.extend(list(range(10))) run() assert recent_values(y) == [1, 6, 11, 16, 21]
def test_filter_number_1(self): input_list = list(range(-5, 5)) in_stream = Stream('input') out_stream = Stream('output') def f(v): return v > 0 filter_element(f, in_stream, out_stream) in_stream.extend(input_list) run() assert recent_values(out_stream) == list(filter(f, input_list))
def test_map_with_keyword_arg(self): x = Stream() y = Stream() @map_e def f(v, k): return v + k f(x, y, k=10) x.extend(list(range(5))) run() assert recent_values(y) == [10, 11, 12, 13, 14]
def test_operators_on_three_streams(self): x = Stream('x') y = Stream('y') z = x + y w = x - y + z DATA = list(range(5)) x.extend(DATA) y.extend(DATA) run() assert recent_values(z) == [2 * v for v in DATA] assert recent_values(w) == [2 * v for v in DATA]
def test_map_window_list_1(self): x = Stream() y = Stream() @map_wl def f(window): return [2 * v for v in window] f(x, y, window_size=2, step_size=2) x.extend(list(range(10))) run() assert recent_values(y) == list(range(0, 20, 2))
def test(): publisher = PikaPublisher( routing_key='temperature', exchange='publications', host='localhost') x = Stream('x') y = Stream('y') map_element(lambda v: 2*v, x, y) publisher.publish(y) for i in range(3): x.extend(list(range(i*4, (i+1)*4))) run() time.sleep(0.001)
def square_and_count_pos_and_non_pos(self, count, input_list): @map_e def f(v, count): if v > 0: count[1] += 1 else: count[0] += 1 return v * v x = Stream('x') y = Stream('y') f(in_stream=x, out_stream=y, count=count) x.extend(input_list) run() return recent_values(y)
def test_example_4(self): # Illustrates a cycle of agents and also shows use # of a class within a wrapper. # Specify network: streams, functions, agents # (a) Specify streams x = Stream('x') y = Stream('y') # (b) Specify encapsulated functions (if any) def f(v, state): final, prefinal = state next_output = final + prefinal # In the next state: # prefinal becomes final # final becomes next_output next_state = next_output, final return next_output, next_state class G(object): def __init__(self): self.divisor = 4 def g(self, v): if v % self.divisor == 0: return _no_value else: return v # (c) Specify agents. encapsulator = G() map_element(func=f, in_stream=y, out_stream=x, state=(0, 1)) map_element(func=encapsulator.g, in_stream=x, out_stream=y) # Drive the network in steps. # Execute a step # Put test values in the input streams. y.append(0) # Execute a step run() # Look at recent values of output streams. assert recent_values(x) == [1, 1, 2, 3, 5, 8] # Execute a step after changing agent parameters encapsulator.divisor = 2 # Put test values in the input streams. y.append(0) # Execute a step run() # Look at recent values of output streams. assert recent_values(x) == \ [1, 1, 2, 3, 5, 8, 13, 21, 34]
def test_merge_1(self): @merge_e def f(list_of_numbers): return sum(list_of_numbers) x = Stream('x') y = Stream('y') z = Stream('z') f([x, y], z) x.extend(list(range(5))) y.extend(list(range(10))) run() assert (recent_values(z) == [0, 2, 4, 6, 8])
def test_delay(self): y = Stream(initial_value=[0] * 5) x = Stream() @map_e def f(v): return 2 * v f(x, y) x.extend(list(range(10))) run() assert recent_values(y) == [ 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 ]
def test_pass_parameter(self): result = [] def f(v, state, result): state += v result.append(state) return v, state x = Stream('x') y = Stream('y') map_element(func=f, in_stream=x, out_stream=y, state=0, result=result) x.extend(list(range(5))) run() assert result == [0, 1, 3, 6, 10]
def test_map_window_list_3(self): x = Stream() y = Stream() @map_wl def f(window, state, K): return [v + 10 * state + K for v in window], state + 1 f(x, y, window_size=2, step_size=2, state=0, K=100) x.extend(list(range(10))) run() assert recent_values(y) == [ 100, 101, 112, 113, 124, 125, 136, 137, 148, 149 ]
def test_1(self): # From map_element_examples x = Stream('x') y = Stream('y') def f(in_stream_element): out_stream_element = 2 * in_stream_element return out_stream_element map_element(func=f, in_stream=x, out_stream=y) x.extend(list(range(5))) run() assert recent_values(y) == [0, 2, 4, 6, 8]