def test_distinct_mux(): source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 3), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 5), rs.OnNextMux((1, ), 3), rs.OnCompletedMux((1, )), ] actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.ops.distinct(), ), ).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 3), rs.OnNextMux((1, ), 5), rs.OnCompletedMux((1, )), ]
def test_variance_mux(): s1 = [random.normalvariate(0.0, 1.0) for _ in range(10)] s2 = [random.normalvariate(1.0, 2.0) for _ in range(10)] source = [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), ] source.extend([rs.OnNextMux((1, None), i) for i in s1]) source.extend([rs.OnNextMux((2, None), i) for i in s2]) source.extend([ rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]) actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.math.formal.variance(reduce=True), ), ).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), rs.OnNextMux((1, None), approx(np.var(s1))), rs.OnCompletedMux((1, None)), rs.OnNextMux((2, None), approx(np.var(s2))), rs.OnCompletedMux((2, None)), ]
def test_sum_mux_completed_on_empty(): source = [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_completed = [] actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.math.sum(reduce=True), ), ).subscribe(on_next=actual_result.append, on_error=lambda e: print(e), on_completed=lambda: actual_completed.append(True)) assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), rs.OnNextMux((1, None), 0), rs.OnCompletedMux((1, None)), rs.OnNextMux((2, None), 0), rs.OnCompletedMux((2, None)), ]
def test_assert_mux_ok(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), 3), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), 3), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_result = [] rx.from_(source).pipe(rs.cast_as_mux_observable(), rs.ops.assert_(lambda i: i > 0)).subscribe( on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), 3), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), 3), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]
def test_max_mux_empty_reduce(): source = [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.math.max(reduce=True), ), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), rs.OnNextMux((1, None), None), rs.OnCompletedMux((1, None)), rs.OnNextMux((2, None), None), rs.OnCompletedMux((2, None)), ]
def test_completion(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnNextMux((1, None), 2), rs.OnCompletedMux((1, None)), ] actual_result = [] actual_completed = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store( rs.ops.tee_map( rs.ops.first(), rs.ops.last(), ), ), ).subscribe(on_next=actual_result.append, on_completed=lambda: actual_completed.append(True)) #assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), (1, 2)), rs.OnCompletedMux((1, None)), ]
def on_next(i): nonlocal state if isinstance(i, rs.OnNextMux): value = i.store.get_state(state, i.key) if value is not rs.state.markers.STATE_NOTSET: if predicate(value, i.item) is True: observer.on_next(rs.OnNextMux(i.key, i)) else: observer.on_error( error("assert {} failed on: {}-{}".format( name, value, i.item))) else: observer.on_next(rs.OnNextMux(i.key, i)) i.store.set_state(state, i.key, i.item) elif isinstance(i, rs.OnCreateMux): i.store.add_key(state, i.key) observer.on_next(i) elif isinstance(i, rs.OnCompletedMux) \ or isinstance(i, rs.OnErrorMux): i.store.del_key(state, i.key) observer.on_next(i) elif type(i) is rs.state.ProbeStateTopology: state = i.topology.create_state(name='assert1', data_type='obj') observer.on_next(i) else: observer.on_next(i)
def test_describe_mux(): s1 = [random.normalvariate(0.0, 1.0) for _ in range(200)] s2 = [random.normalvariate(1.0, 2.0) for _ in range(200)] source = [ rs.OnCreateMux((1 ,None)), rs.OnCreateMux((2, None)), ] source.extend([rs.OnNextMux((1, None), i) for i in s1]) source.extend([rs.OnNextMux((2, None), i) for i in s2]) source.extend([ rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]) actual_result = [] store = rs.state.StoreManager(store_factory=rs.state.MemoryStore) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_store( store, rs.math.dist.update(reduce=True), ), rs.math.dist.describe(), ).subscribe(on_next=actual_result.append)
def test_multiplex(): source = [1, 2, 3, 4] actual_error = [] actual_completed = [] actual_result = [] mux_actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.ops.multiplex(rx.pipe(ops.do_action( mux_actual_result.append), ), ), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == source assert mux_actual_result == [ rs.OnCreateMux((0, )), rs.OnNextMux((0, ), 1), rs.OnNextMux((0, ), 2), rs.OnNextMux((0, ), 3), rs.OnNextMux((0, ), 4), rs.OnCompletedMux((0, )), ]
def on_next(i, x): if isinstance(x, rs.OnCreateMux): if i == 0: if zip is True or combine is True: append_count = (x.key[0] + 1) * n - len(queue) if append_count > 0: for _ in range(append_count): queue.append(None) has_next.append(False) observer.on_next(x) return elif isinstance(x, rs.OnCompletedMux): if i == n - 1: observer.on_next(x) if zip is True or combine is True: base_index = x.key[0] * n queue[base_index + i] = None has_next[base_index + i] = False return elif not isinstance(x, rs.OnNextMux): observer.on_next(x) return if zip is True or combine is True: base_index = x.key[0] * n index = base_index + i queue[index] = x.item has_next[index] = True if zip is True: _next = has_next[base_index:base_index + n] if all(_next): try: _queue = queue[base_index:base_index + n] res = tuple(_queue) res = rs.OnNextMux(x.key, res, x.store) for index in range(n): has_next[base_index + index] = False queue[base_index + index] = None except Exception as ex: # pylint: disable=broad-except observer.on_error(ex) return observer.on_next(res) else: try: _queue = queue[base_index:base_index + n] res = tuple(_queue) res = rs.OnNextMux(x.key, res, x.store) except Exception as ex: # pylint: disable=broad-except observer.on_error(ex) return observer.on_next(res) else: observer.on_next(x)
def test_to_list_mux(): actual_result = [] source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnCreateMux((2, )), rs.OnNextMux((1, ), 3), rs.OnNextMux((2, ), 10), rs.OnNextMux((2, ), 11), rs.OnNextMux((1, ), 4), rs.OnNextMux((2, ), 12), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.data.to_list(), ), ).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, )), rs.OnCreateMux((2, )), rs.OnNextMux((1, ), [1, 2, 3, 4]), rs.OnCompletedMux((1, )), rs.OnNextMux((2, ), [10, 11, 12]), rs.OnCompletedMux((2, )), ]
def test_tee_map_mux(): source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), 2), rs.OnNextMux((2, ), 2), rs.OnNextMux((1, ), 1), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ] actual_result = [] actual_error = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.tee_map(lambda d: d.pipe(rs.ops.map(lambda i: i * 2), ), lambda d: d.pipe(rs.ops.map(lambda i: i))), ).subscribe( on_next=actual_result.append, on_error=actual_error.append, ) assert actual_error == [] assert actual_result == [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), (2, 1)), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), (4, 2)), rs.OnNextMux((2, ), (4, 2)), rs.OnNextMux((1, ), (2, 1)), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ]
def test_mean_mux_key_mapper(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), ('a', 2)), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), ('A', 3)), rs.OnNextMux((2, None), ('B', 6)), rs.OnNextMux((1, None), ('b', 3)), rs.OnNextMux((1, None), ('c', 10)), rs.OnNextMux((1, None), ('d', 4)), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.math.mean(lambda i: i[1], reduce=True), ), ).subscribe( on_next=actual_result.append, on_error=lambda e: print(e), ) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), rs.OnNextMux((1, None), 4.75), rs.OnCompletedMux((1, None)), rs.OnNextMux((2, None), 4.5), rs.OnCompletedMux((2, None)), ]
def test_min_mux_reduce(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 4), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 8), rs.OnNextMux((2, None), 6), rs.OnNextMux((1, None), 10), rs.OnNextMux((1, None), 3), rs.OnNextMux((1, None), 2), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.math.min(reduce=True)), ).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), rs.OnNextMux((1, None), 2), rs.OnCompletedMux((1, None)), rs.OnNextMux((2, None), 6), rs.OnCompletedMux((2, None)), ]
def test_scan_mux_reduce_empty_on_complete(): source = [ rs.OnCreateMux((0, None)), rs.OnCompletedMux((0, None)), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store( rs.ops.scan(lambda acc, i: i + acc, seed=0, reduce=True), )).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((0, None)), rs.OnNextMux((0, None), 0), rs.OnCompletedMux((0, None)), ]
def on_next(i): nonlocal state if type(i) is rs.OnNextMux: i.store.set_state(state, i.key, i.item) elif type(i) is rs.OnCreateMux: i.store.add_key(state, i.key) observer.on_next(i) elif type(i) is rs.OnCompletedMux: value = i.store.get_state(state, i.key) if value is not rs.state.markers.STATE_NOTSET: observer.on_next(rs.OnNextMux(i.key, value, i.store)) observer.on_next(i) i.store.del_key(state, i.key) elif type(i) is rs.OnErrorMux: observer.on_next(i) i.store.del_key(state, i.key) elif type(i) is rs.state.ProbeStateTopology: state = i.topology.create_state(name='last', data_type='obj') observer.on_next(i) else: observer.on_next(i)
def test_max_mux(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 4), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 8), rs.OnNextMux((2, None), 6), rs.OnNextMux((1, None), 10), rs.OnNextMux((1, None), 3), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.math.max(), ), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] try: assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 4), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 8), rs.OnNextMux((2, None), 8), rs.OnNextMux((1, None), 10), rs.OnNextMux((1, None), 10), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] except Exception as e: import traceback traceback.print_tb(e.__traceback__) raise e
def test_assert_mux_fail(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), -1), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), -1), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_result = [] error = [] rx.from_(source).pipe(rs.cast_as_mux_observable(), rs.ops.assert_(lambda i: i > 0)).subscribe( on_next=actual_result.append, on_error=error.append, ) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), ] assert type(error[0]) == ValueError
def test_take_mux(): source = [ rs.OnCreateMux((1 ,None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 10), rs.OnNextMux((2, None), 11), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), 3), rs.OnNextMux((2, None), 12), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store( rs.ops.take(2), ), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1 ,None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 10), rs.OnNextMux((2, None), 11), rs.OnNextMux((1, None), 2), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]
def test_roll(): source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 3), rs.OnNextMux((1, ), 4), rs.OnNextMux((1, ), 5), rs.OnCompletedMux((1, )), ] actual_result = [] mux_actual_result = [] def on_next(i): actual_result.append(i) store = rs.state.StoreManager(store_factory=rs.state.MemoryStore) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_store( store, rs.data.roll(window=3, stride=3, pipeline=rx.pipe( ops.do_action(mux_actual_result.append), )), ), ).subscribe(on_next) assert actual_result == source assert type(mux_actual_result[0]) is rs.state.ProbeStateTopology assert mux_actual_result[1:] == [ rs.OnCreateMux((1, (1, )), store), rs.OnNextMux((1, (1, )), 1, store), rs.OnNextMux((1, (1, )), 2, store), rs.OnNextMux((1, (1, )), 3, store), rs.OnCompletedMux((1, (1, )), store), rs.OnCreateMux((1, (1, )), store), rs.OnNextMux((1, (1, )), 4, store), rs.OnNextMux((1, (1, )), 5, store), rs.OnCompletedMux((1, (1, )), store), ]
def test_tee_map_mux_combine_latest(): source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), 2), rs.OnNextMux((2, ), 3), rs.OnNextMux((1, ), 2), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ] actual_result = [] actual_error = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.tee_map( lambda d: d.pipe(rs.ops.map(lambda i: i * 2), ), lambda d: d.pipe( rs.ops.map(lambda i: i), rs.ops.filter(lambda i: i % 2 != 0), ), join='combine_latest', ), ).subscribe( on_next=actual_result.append, on_error=actual_error.append, ) assert actual_error == [] assert actual_result == [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), (2, None)), rs.OnNextMux((1, ), (2, 1)), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), (4, None)), rs.OnNextMux((2, ), (6, None)), rs.OnNextMux((2, ), (6, 3)), rs.OnNextMux((1, ), (4, 1)), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ]
def test_flat_map_mux(): source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), [1, 2, 3, 4]), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), [10, 11, 12, 13]), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.flat_map(), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 3), rs.OnNextMux((1, ), 4), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), 10), rs.OnNextMux((2, ), 11), rs.OnNextMux((2, ), 12), rs.OnNextMux((2, ), 13), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ]
def test_tee_map_mux_combine_latest_empty(): source = [ rs.OnCreateMux((1, )), rs.OnCompletedMux((1, )), rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 10), rs.OnNextMux((1, ), 20), rs.OnCompletedMux((1, )), ] actual_result = [] actual_error = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.tee_map( rs.ops.map(lambda i: i * 2), rs.ops.map(lambda i: i), join='combine_latest', ), ).subscribe( on_next=actual_result.append, on_error=actual_error.append, ) assert actual_error == [] assert actual_result == [ rs.OnCreateMux((1, )), rs.OnCompletedMux((1, )), rs.OnCreateMux((1, )), rs.OnNextMux((1, ), (20, None)), rs.OnNextMux((1, ), (20, 10)), rs.OnNextMux((1, ), (40, 10)), rs.OnNextMux((1, ), (40, 20)), rs.OnCompletedMux((1, )), ]
def test_group_by(): source = [1, 2, 2, 1] actual_error = [] actual_completed = [] actual_result = [] mux_actual_result = [] def on_completed(): actual_completed.append(True) store = rs.state.StoreManager(store_factory=rs.state.MemoryStore) rx.from_(source).pipe( rs.state.with_store( store, rx.pipe( rs.ops.group_by( lambda i: i, rx.pipe(ops.do_action(mux_actual_result.append), ), ))), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == source assert type(mux_actual_result[0]) is rs.state.ProbeStateTopology assert mux_actual_result[1:] == [ rs.OnCreateMux((0, (0, )), store), rs.OnNextMux((0, (0, )), 1, store), rs.OnCreateMux((1, (0, )), store), rs.OnNextMux((1, (0, )), 2, store), rs.OnNextMux((1, (0, )), 2, store), rs.OnNextMux((0, (0, )), 1, store), rs.OnCompletedMux((0, (0, )), store), rs.OnCompletedMux((1, (0, )), store), ]
def test_probe_topology(): actual_result = [] actual_store_result = [] store = rs.state.StoreManager(store_factory=rs.state.MemoryStore) rx.from_([1, 2, 3, 4]).pipe( rs.state.with_store(store, rx.pipe( on_mux_action(actual_store_result.append) )) ).subscribe( on_next=actual_result.append ) assert actual_result == [1, 2, 3, 4] assert type(actual_store_result[0]) == ProbeStateTopology assert actual_store_result[1:] == [ rs.OnCreateMux((0,), store), rs.OnNextMux((0,), 1, store), rs.OnNextMux((0,), 2, store), rs.OnNextMux((0,), 3, store), rs.OnNextMux((0,), 4, store), rs.OnCompletedMux((0,), store), ]
def on_next(i): if type(i) is rs.OnNextMux: try: if predicate(i.item) is True: observer.on_next(rs.OnNextMux(i.key, i.item)) else: observer.on_error( error("assert {} failed on: {}".format( name, i.item))) except Exception as e: observer.on_next(rs.OnErrorMux(i.key, e)) else: observer.on_next(i)
def on_next(i): if type(i) is rs.OnErrorMux: try: print("error") ii = mapper(i.error) observer.on_next( rs.OnNextMux( key=i.key, item=ii, store=i.store, )) except Exception as e: observer.on_error(e) else: observer.on_next(i)
def on_next(i): nonlocal state if type(i) is rs.OnNextMux: try: value = i.store.get_state(state, i.key) if value is rs.state.markers.STATE_NOTSET: value = seed() if callable( seed) else copy.deepcopy(seed) acc = accumulator(value, i.item) i.store.set_state(state, i.key, acc) if reduce is False: observer.on_next(rs.OnNextMux(i.key, acc, i.store)) except Exception as e: observer.on_next(rs.OnErrorMux(i.key, e, i.store)) elif type(i) is rs.OnCreateMux: i.store.add_key(state, i.key) observer.on_next(i) elif type(i) is rs.OnCompletedMux: if reduce is True: value = i.store.get_state(state, i.key) if value is rs.state.markers.STATE_NOTSET: value = seed() if callable( seed) else copy.deepcopy(seed) observer.on_next(rs.OnNextMux(i.key, value, i.store)) observer.on_next(i) i.store.del_key(state, i.key) elif type(i) is rs.OnErrorMux: observer.on_next(i) i.store.del_key(state, i.key) elif type(i) is rs.state.ProbeStateTopology: state = i.topology.create_state(name='scan', data_type=type(seed)) observer.on_next(i) else: observer.on_next(i)
def test_starmap_mux(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), (1, 3)), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), (2, 5)), rs.OnNextMux((2, None), (2, 2)), rs.OnNextMux((1, None), (1, 1)), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.starmap(lambda i, j: i + j), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 4), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 7), rs.OnNextMux((2, None), 4), rs.OnNextMux((1, None), 2), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]
def test_identity_mux(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), 2), rs.OnNextMux((1, None), 1), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.identity(), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), 2), rs.OnNextMux((1, None), 1), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]