def buffer_( boundaries: Observable[Any], ) -> Callable[[Observable[_T]], Observable[List[_T]]]: return compose( ops.window(boundaries), ops.flat_map(ops.to_list()), )
def buffer_toggle_( openings: Observable[Any], closing_mapper: Callable[[Any], Observable[Any]] ) -> Callable[[Observable[_T]], Observable[List[_T]]]: return compose( ops.window_toggle(openings, closing_mapper), ops.flat_map(ops.to_list()), )
async def test_flat_map(): x: Subject[int] = Subject() x.pipe(ops.flat_map(mapper)).subscribe(on_next, on_error, scheduler=scheduler) x.on_next(10) await asyncio.sleep(0.1)
def buffer_when_( closing_mapper: Callable[[], Observable[Any]] ) -> Callable[[Observable[_T]], Observable[List[_T]]]: return compose( ops.window_when(closing_mapper), ops.flat_map(ops.to_list()), )
def test_flat_map_then_error_error(self): ex = "ex" scheduler = TestScheduler() xs = scheduler.create_cold_observable( on_next(100, 4), on_next(200, 2), on_next(300, 3), on_next(400, 1), on_error(500, ex), ) ys = scheduler.create_cold_observable( on_next(50, "foo"), on_next(100, "bar"), on_next(150, "baz"), on_next(200, "qux"), on_error(250, ex), ) results = scheduler.start(lambda: xs.pipe(ops.flat_map(ys))) assert results.messages == [ on_next(350, "foo"), on_next(400, "bar"), on_next(450, "baz"), on_next(450, "foo"), on_next(500, "qux"), on_next(500, "bar"), on_error(550, ex), ] assert xs.subscriptions == [subscribe(200, 550)] assert ys.subscriptions == [ subscribe(300, 550), subscribe(400, 550), subscribe(500, 550), ]
def on_mouse_up(self) -> Observable[MouseButtonEvent]: return self.on_state_change.pipe( ops.map(lambda s: s.buttons), ops.distinct_until_changed(), ops.pairwise(), ops.flat_map(lambda b: b[0] - b[1]), ops.map(lambda b: MouseUpEvent(self, self.state.unwrap(), b)))
def create(): def mapper(x): ys = [x] * x inners.append(ys) return ys return xs.pipe(ops.flat_map(mapper))
def test_window_sum(self): res = [] reactivex.from_(range(6)).pipe( ops.window_with_count(count=3, skip=1), ops.flat_map(lambda i: i.pipe(ops.sum(), )), ).subscribe(on_next=res.append) assert res == [3, 6, 9, 12, 9, 5, 0]
def factory(): def projection(x): invoked[0] += 1 if invoked[0] == 3: raise Exception(ex) return x return xs.pipe(ops.flat_map(projection))
def __init__(self) -> None: self._subject = Subject() self._scheduler = ThreadPoolScheduler(max_workers=1) obs = self._subject.pipe(ops.observe_on(self._scheduler)) self._disposable = obs \ .pipe(ops.window_with_time_or_count(count=5, timespan=datetime.timedelta(milliseconds=10_000)), ops.flat_map(lambda x: self._window_to_group(x)), ops.map(mapper=lambda x: self._retryable(data=x, delay=self._jitter_delay(jitter_interval=1000))), ops.merge_all()) \ .subscribe(self._result, self._error, self._on_complete) pass
def buffer_with_time_( timespan: typing.RelativeTime, timeshift: Optional[typing.RelativeTime] = None, scheduler: Optional[abc.SchedulerBase] = None, ) -> Callable[[Observable[_T]], Observable[List[_T]]]: if not timeshift: timeshift = timespan return compose( ops.window_with_time(timespan, timeshift, scheduler), ops.flat_map(ops.to_list()), )
def test_groupby_count(self): res = [] counts = reactivex.from_(range(10)).pipe( ops.group_by(lambda i: "even" if i % 2 == 0 else "odd"), ops.flat_map(lambda i: i.pipe( ops.count(), ops.map(lambda ii: (i.key, ii)), )), ) counts.subscribe(on_next=res.append) assert res == [("even", 5), ("odd", 5)]
def test_flat_map_then_never_complete(self): scheduler = TestScheduler() xs = scheduler.create_cold_observable( on_next(100, 4), on_next(200, 2), on_next(300, 3), on_next(400, 1), on_next(500, 5), on_next(700, 0), ) ys = scheduler.create_cold_observable( on_next(50, "foo"), on_next(100, "bar"), on_next(150, "baz"), on_next(200, "qux"), on_completed(250), ) results = scheduler.start(lambda: xs.pipe(ops.flat_map(ys))) assert results.messages == [ on_next(350, "foo"), on_next(400, "bar"), on_next(450, "baz"), on_next(450, "foo"), on_next(500, "qux"), on_next(500, "bar"), on_next(550, "baz"), on_next(550, "foo"), on_next(600, "qux"), on_next(600, "bar"), on_next(650, "baz"), on_next(650, "foo"), on_next(700, "qux"), on_next(700, "bar"), on_next(750, "baz"), on_next(750, "foo"), on_next(800, "qux"), on_next(800, "bar"), on_next(850, "baz"), on_next(900, "qux"), on_next(950, "foo"), ] assert xs.subscriptions == [subscribe(200, 1000)] assert ys.subscriptions == [ subscribe(300, 550), subscribe(400, 650), subscribe(500, 750), subscribe(600, 850), subscribe(700, 950), subscribe(900, 1000), ]
def create(): def mapper(xy): x, y = xy return "{}{}".format(x.value, y.value) return xs.pipe( ops.join( ys, lambda x: reactivex.timer(x.interval), lambda y: reactivex.timer(y.interval).pipe( ops.flat_map( reactivex.throw(ex) if y.value == "tin" else reactivex.empty())), ), ops.map(mapper), )
def buffer_with_count(source: Observable[_T]) -> Observable[List[_T]]: nonlocal skip if skip is None: skip = count def mapper(value: Observable[_T]) -> Observable[List[_T]]: return value.pipe( ops.to_list(), ) def predicate(value: List[_T]) -> bool: return len(value) > 0 return source.pipe( ops.window_with_count(count, skip), ops.flat_map(mapper), ops.filter(predicate), )
def open(self): print("WebSocket opened") # A Subject is both an observable and observer, so we can both subscribe # to it and also feed (on_next) it with new values self.subject: Subject[Dict[str, int]] = Subject() # Now we take on our magic glasses and project the stream of bytes into # a ... query = self.subject.pipe( # 1. stream of keycodes ops.map(lambda obj: obj["keycode"]), # 2. stream of windows (10 ints long) ops.window_with_count(10, 1), # 3. stream of booleans, True or False ops.flat_map(lambda win: win.pipe(ops.sequence_equal(codes))), # 4. stream of Trues ops.filter(lambda equal: equal), ) # 4. we then subscribe to the Trues, and signal Konami! if we see any query.subscribe(on_next=lambda x: self.write_message("Konami!"))
""" Create process pool for parallel encoding into LineProtocol """ cpu_count = multiprocessing.cpu_count() with concurrent.futures.ProcessPoolExecutor(cpu_count, initializer=init_counter, initargs=(counter_, progress_, queue_)) as executor: """ Converts incoming HTTP stream into sequence of LineProtocol """ data = rx \ .from_iterable(DictReader(io_wrapper)) \ .pipe(ops.buffer_with_count(10_000), # Parse 10_000 rows into LineProtocol on subprocess ops.flat_map(lambda rows: executor.submit(parse_rows, rows, content_length))) """ Write data into InfluxDB """ data.subscribe(on_next=lambda x: None, on_error=lambda ex: print(f'Unexpected error: {ex}')) """ Terminate Writer """ queue_.put(None) queue_.join() print() print(f'Import finished in: {datetime.now() - startTime}') print() """
def factory(): return xs.pipe(ops.flat_map(ys))
def create(): return xs.pipe(ops.flat_map(lambda x: x))
def factory(): return xs.pipe(ops.flat_map(lambda x: x))
def factory(): def projection(x): return reactivex.interval(10).pipe( ops.map_indexed(lambda a, b: x), ops.take(x)) return xs.pipe(ops.flat_map(projection))
import reactivex from reactivex import operators as ops a = reactivex.cold(" ---a0---a1----------------a2-| ") b = reactivex.cold(" ---b1---b2---| ") c = reactivex.cold(" ---c1---c2---| ") d = reactivex.cold(" -----d1---d2---|") e1 = reactivex.cold("a--b--------c-----d-------| ") observableLookup = {"a": a, "b": b, "c": c, "d": d} source = e1.pipe( ops.flat_map(lambda value: observableLookup[value]), ops.do_action(lambda v: print(v)), ) source.run()
from reactivex import operators as ops from reactivex.testing.marbles import marbles_testing """ Tests MergeMap from reactivexjs https://github.com/ReactiveX/rxjs/blob/master/spec/operators/mergeMap-spec.ts it should flat_map many regular interval inners """ with marbles_testing(timespan=1.0) as context: start, cold, hot, exp = context a = cold(" ----a---a----a----(a,|) ") b = cold(" ----1----b----(b,|) ") c = cold(" -------c---c---c----c---(c,|)") d = cold(" -------(d,|) ") e1 = hot("-a---b-----------c-------d------------| ") ex = exp("-----a---(a,1)(a,b)(a,b)c---c---(c,d)c---(c,|)") expected = ex observableLookup = {"a": a, "b": b, "c": c, "d": d} obs = e1.pipe(ops.flat_map(lambda value: observableLookup[value])) results = start(obs) assert results == expected print("flat_map: results vs expected") for r, e in zip(results, expected): print(r, e)