def main(): app = QApplication(sys.argv) scheduler = QtScheduler(QtCore) window = Window() window.show() text = "TIME FLIES LIKE AN ARROW" def on_next(info): label, (x, y), i = info label.move(x + i * 12 + 15, y) label.show() def handle_label(label, i): delayer = ops.delay(i * 0.100) mapper = ops.map(lambda xy: (label, xy, i)) return window.mousemove.pipe( delayer, mapper, ) labeler = ops.flat_map_indexed(handle_label) mapper = ops.map(lambda c: QLabel(c, window)) reactivex.from_(text).pipe( mapper, labeler, ).subscribe(on_next, on_error=print, scheduler=scheduler) sys.exit(app.exec_())
def main() -> None: root = Tk() root.title("Rx for Python rocks") scheduler = TkinterScheduler(root) mousemoves: Subject[Event[Any]] = Subject() frame = Frame(root, width=600, height=600) frame.bind("<Motion>", mousemoves.on_next) text = "TIME FLIES LIKE AN ARROW" def on_next(info: Tuple[tkinter.Label, "Event[Frame]", int]) -> None: label, ev, i = info label.place(x=ev.x + i * 12 + 15, y=ev.y) def label2stream( label: tkinter.Label, index: int ) -> Observable[Tuple[tkinter.Label, "Event[Frame]", int]]: return mousemoves.pipe( ops.map(lambda ev: (label, ev, index)), ops.delay(index * 0.1), ) def char2label(char: str) -> Label: return Label(frame, text=char, borderwidth=0, padx=0, pady=0) reactivex.from_(text).pipe( ops.map(char2label), ops.flat_map_indexed(label2stream), ).subscribe(on_next, on_error=print, scheduler=scheduler) frame.pack() root.mainloop()
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 main(): scheduler = GtkScheduler(GLib) scrolled_window = Gtk.ScrolledWindow() window = Window() window.connect("delete-event", Gtk.main_quit) container = Gtk.Fixed() scrolled_window.add(container) window.add(scrolled_window) text = "TIME FLIES LIKE AN ARROW" def on_next(info): label, (x, y), i = info container.move(label, x + i * 12 + 15, y) label.show() def handle_label(label, i): delayer = ops.delay(i * 0.100) mapper = ops.map(lambda xy: (label, xy, i)) return window.mousemove.pipe( delayer, mapper, ) def make_label(char): label = Gtk.Label(label=char) container.put(label, 0, 0) label.hide() return label mapper = ops.map(make_label) labeler = ops.flat_map_indexed(handle_label) reactivex.from_(text).pipe( mapper, labeler, ).subscribe(on_next, on_error=print, scheduler=scheduler) window.show_all() Gtk.main()
def projection(x: _T1, i: int) -> Observable[Any]: mapper_result: Any = (mapper(x) if mapper else mapper_indexed(x, i) if mapper_indexed else identity) if isinstance(mapper_result, Future): result: Observable[Any] = from_future( cast("Future[Any]", mapper_result)) elif isinstance(mapper_result, Observable): result = mapper_result else: result = from_(mapper_result) return result
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)]
async def go(loop): scheduler = AsyncIOScheduler(loop) xs = reactivex.from_([x for x in range(10)], scheduler=scheduler) gen = xs.pipe(to_async_generator()) # Wish we could write something like: # ys = (x for x in yield from gen()) while True: x = await gen() if x is None: break print(x)
def main(): app = wx.App() scheduler = WxScheduler(wx) app.TopWindow = frame = Frame() frame.Show() text = "TIME FLIES LIKE AN ARROW" def on_next(info): label, (x, y), i = info label.Move(x + i * 12 + 15, y) label.Show() def handle_label(label, i): delayer = ops.delay(i * 0.100) mapper = ops.map(lambda xy: (label, xy, i)) return frame.mousemove.pipe( delayer, mapper, ) def make_label(char): label = wx.StaticText(frame, label=char) label.Hide() return label mapper = ops.map(make_label) labeler = ops.flat_map_indexed(handle_label) reactivex.from_(text).pipe( mapper, labeler, ).subscribe(on_next, on_error=print, scheduler=scheduler) frame.Bind(wx.EVT_CLOSE, lambda e: (scheduler.cancel_all(), e.Skip())) app.MainLoop()
def test_double_subscribe_to_iterable(self): iterable_finite = [1, 2, 3] scheduler = TestScheduler() obs = reactivex.from_(iterable_finite) results = scheduler.start(lambda: reactivex.concat(obs, obs)) assert results.messages == [ on_next(200, 1), on_next(200, 2), on_next(200, 3), on_next(200, 1), on_next(200, 2), on_next(200, 3), on_completed(200), ]
def create(): return reactivex.from_(iterable_finite)
def test_run_from_first(self): result = reactivex.from_([1, 2, 3]).pipe(ops.first()).run() assert result == 1
def test_run_from(self): result = reactivex.from_([1, 2, 3]).run() assert result == 3
async def go(): nonlocal result source = reactivex.from_([40, 41, 42]) result = await source