# Example of summing all the values in a data stream import rx from rx import operators as op # Set up a source observable = rx.from_list([2, 3, 5, 7]) # Apply sum function to initial source observable2 = observable.pipe(op.sum()) # Subscribe to the result generated by sum observable2.subscribe(lambda v: print(v)) print('-' * 20) # Rolling or incremental sum rx.from_([2, 3, 5, 7]).pipe( op.scan(lambda subtotal, i: subtotal + i)).subscribe(lambda v: print(v))
# By considering the terms in the Fibonacci sequence whose values do not # exceed four million, find the sum of the even-valued terms. MAX_VALUE = 4e6 # Legacy method def fill_fibonacci(max_value): fib_list = [1, 1] increment = 1 while (True): fib_list.append(fib_list[increment - 1] + fib_list[increment]) increment = increment + 1 if (fib_list[increment] > max_value): fib_list.pop() break return fib_list print(sum([num for num in fill_fibonacci(MAX_VALUE) if num % 2 == 0])) # With reactive programming from rx import from_list, operators as op source = from_list(fill_fibonacci(MAX_VALUE)) source.pipe(op.filter(lambda x: x % 2 == 0), op.sum()).subscribe(lambda i: print(i))
# A subscribers example import rx from rx.subjects import Subject from datetime import datetime # You can choose which of these to use # source = rx.from_([2, 3, 5, 7]) # source = rx.from_iterable([2, 3, 5, 7]) source = rx.from_list([2, 3, 5, 7]) class TimeStampSubject(Subject): """ Class implementing a Subject """ def on_next(self, value): print('Subject Received', value) super().on_next((value, datetime.now())) def on_completed(self): print('Data Stream Completed') super().on_completed() def on_error(self, error): print('In Subject - Error Occurred', error) super().on_error(error) def prime_number_reporter(value): print('Function Received', value)
def get_devices(self) -> Observable: return rx.concat(rx.from_list(self.__device_list), self.__added_devices)
def get_buses(self) -> Observable: return rx.concat(rx.from_list(self.__bus_map.values()), self.__bus_subject)
def _path_elements(path): return from_list(path.split('/')).pipe( filter(lambda name: name and name.strip() ) # Allow for double // and trailing / in path )
def get_values(self) -> Observable: return rx.concat(rx.from_list(self.__latest_values.values()), self.__value_subject)
# An example illustrating how to merge two data sources import rx # Set up two sources source1 = rx.from_list([2, 3, 5, 7]) source2 = rx.from_list([10, 11, 12]) # Use the merge function to create a single observable data stream rx.merge(source1, source2)\ .subscribe(lambda v: print(v, end=','))
import rx import rx.operators as op import threading # 与えられたものをそのまま rx.just(3).subscribe(print) rx.from_list([1, 2, 3, 4]).subscribe(print) rx.from_iterable([1, 2, 3, 4]).subscribe(print) # bufferはたまってから処理する rx.from_iterable(range(10)).pipe(op.map(lambda x: x * 2), op.buffer_with_count(3)).subscribe(print) rx.from_iterable(range(10)).pipe( op.map(lambda x: x * 2), op.buffer_with_count(3), # ここではリストになっている op.map(len)).subscribe(print) # windowもたまってから処理するがlistではなくObservableになる rx.from_iterable(range(10)).pipe( op.map(lambda x: x * 2), op.window_with_count(3), ).subscribe(print) # observableの中身を1つ1つ処理したい場合はflatmapが使える rx.from_iterable(range(10)).pipe( op.map(lambda x: x * 2), op.window_with_count(3), # ただしこの時点でxはObservable # 複数のObservableを1つにまとめる
def vaciarFrameReactiveX(self, frame): self.fuente = rx.from_list(frame.winfo_children()).pipe( op.map(self.VaciarFrame)) frame.pack_forget()