def test_for_each_index_some_data(self): lstX = [] lstI = [] def action(x, i): lstX.append(x) lstI.append(i) Observable.range(10, 10).to_blocking().for_each(action) assert(lstX == [x for x in range(10, 20)]) assert(lstI == [x for x in range(10)])
def test_for_each_index_on_next_throws(self): ex = Exception() xs = Observable.range(0, 10) def action(x, i): _raise(ex) self.assertRaises(RxException, lambda: xs.to_blocking().for_each(action))
def filter(self, event, size, msgs=2): # min size of the event data source = Observable.of(event) source\ .map(lambda s: len(s))\ .filter(lambda i : i>=size)\ .subscribe(lambda value : print(value)) # an observer is setup here with subscribe function # map associet une fonction/transforme fait une action # filter garde ce qu'on veut # subscribe c'est l'output en fonction d'un type d'events next, error, terminé # emits integer each 1000 ms # Observables can be created for button events, requests, timers, and even live Twitter feeds. Observable.interval(1000)\ .map(lambda i : "{0} Mississipi".format(i))\ .subscribe(lambda s : print(s)) input("press any key to quit\n") from random import randint ''' use the rx events for the connection with the agent bidder class ?? ''' three_emissions = Observable.range(1,msgs) # msg are separately sent to different subscribers three_random_ints = three_emissions.map(lambda i: randint(1,1000)) three_random_ints.subscribe(lambda i: print("subscriber 1 received".format(i))) three_random_ints.subscribe(lambda i: print("subscriber 2 received".format(i))) # send the same message to different subscribers three_random_ints = three_emissions.mpa(lambda i: randint(1,1000)).publish() three_random_ints.subscribe(lambda i: print("subscriber 1 received".format(i))) three_random_ints.subscribe(lambda i: print("subscriber 1 received".format(i))) three_random_ints.connect() # .auto_connect(2) hold off until the number of subscriber reach 2 # then emissions is firing at this moment # Observable.zip(letters, intervals, lambda s, i: (s, i)) \ .subscribe(lambda t: print(t)) ''' save event SQL alchemy DB from sqlalchemy import create_engine, text from rx import Observable engine = create_engine('sqlite:///rexon_metals.db') conn = engine.connect() def customer_for_id(customer_id): stmt = text("SELECT * FROM CUSTOMER WHERE CUSTOMER_ID = :id") return Observable.from_(conn.execute(stmt, id=customer_id)) # Query customers with IDs 1, 3, and 5 Observable.of(1, 3, 5) \ .flat_map(lambda id: customer_for_id(id)) \ .subscribe(lambda r: print(r)) ''' return
def test_range_double_subscribe(self): scheduler = TestScheduler() obs = Observable.range(1, 3) results = scheduler.start(lambda: obs) results.messages.assert_equal(on_next(200, 1), on_next(200, 2), on_next(200, 3), on_completed(200)) results = scheduler.start(lambda: obs) results.messages.assert_equal(on_next(1001, 1), on_next(1001, 2), on_next(1001, 3), on_completed(1001))
def test_range_double_subscribe(self): scheduler = TestScheduler() obs = Observable.range(1, 4) results = scheduler.start(lambda: obs.concat(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 run_in_thread(func, on_success, on_fail, main_scheduler=None): """ 线程中执行耗时操作 :param func: :param on_success: :param on_fail: :param main_scheduler: :return: """ api = Observable.range(0, 1)\ .map(lambda x: func())\ .subscribe_on(pool_scheduler) if main_scheduler: api = api.observe_on(main_scheduler) api.subscribe(on_next=on_success, on_error=on_fail) return api
def class_twentyfive(): import requests text_request = requests.get( "http://www.gutenberg.org/cache/epub/55506/pg55506.txt") if text_request.ok == False: print("request failed: {}".format(text_request.reason)) print("{}".format(text_request.url)) return from tempfile import NamedTemporaryFile temp = NamedTemporaryFile() temp.file.write(text_reqiest.content) temp.file.seek(0) # do something with the file Observable.from_(["a", "b", "c", "d", "e", "f"]) numbers = Observable.range(1, 5) Observable.zip(letters, numbers, lambda l, n: "{} <===> {}".format(l, n)).subscribe(print)
def main(img_path: str) -> Observable: image_arr, origin, spacing = utils.load_itk_image(img_path) output_path = os.environ['OUTPUT_PATH'] cand_path = os.environ['CAND_PATH'] bin_output_path = os.environ['BIN_OUTPUT_PATH'] get_voxel_coord = partial(utils.world_to_voxel_coord, origin, spacing) # number of scans slices slices_num = image_arr.shape[0] # capture the pacient id by regexp patient_id = re.findall("^.*\/(.*).mhd$", img_path)[0] patient_dir = os.path.join(output_path, patient_id) bin_patient_dir = os.path.join(bin_output_path, patient_id) if not os.path.isdir(patient_dir): os.mkdir(patient_dir) if not os.path.isdir(bin_patient_dir): os.mkdir(bin_patient_dir) return ( Observable.from_(image_arr) .map(utils.normalize_planes) .zip( Observable.range(0, slices_num), lambda image, ind: {"image": image, "z_coord": ind}, ) .map(lambda data: partial(utils.save_scan, patient_id, **data)) .tap(lambda save_at: save_at(output_path=patient_dir)) .tap( lambda save_at: save_at( output_path=bin_patient_dir, file_format="npy" ) ) )
def merge_example(): Observable.range( 1, 5 ).merge( Observable.from_("abcde") ).subscribe( print )
class MyObserver(Observer): def on_next(self, value): print("Got: %s" % value) def on_error(self, error): print("Got error: %s" % error) def on_completed(self): print("Sequence completed") xs = Observable.from_iterable(range(10)) d = xs.subscribe(MyObserver()) print() xf = Observable.from_(range(10)) f = xf.subscribe(print) print() g = xf.filter(lambda x: x % 2).subscribe(print) print() h = xf.map(lambda x: x * 2).subscribe(print) print() astream = Observable.range(1, 5) bstream = Observable.from_('abcde') cstream = astream.merge(bstream).subscribe(print)
class MyObserver(Observer): def on_next(self, value): print('Got: {}'.format(value)) def on_error(self, error): print('Got: {}'.format(error)) def on_completed(self): print('Sequence completed') # === usage === from rx import Observable xs = Observable.from_iterable(range(10)) d = xs.subscribe(MyObserver()) print('-'*15) xs = Observable.from_(range(10)) d = xs.subscribe(print) print('-'*15) xs = Observable.from_(range(10)) d = xs.filter(lambda x: x % 2).subscribe(print) print('-'*15) xs = Observable.from_(range(10)) d = xs.map(lambda x: x * 2).subscribe(print) print('-'*15) xs = Observable.range(1, 5) ys = Observable.from_('abcde') zs = xs.merge(ys).subscribe(print)
def create(): return Observable.range(-10, 5, scheduler)
# calculate number of CPU's, then create a ThreadPoolScheduler with that number of threads optimal_thread_count = multiprocessing.cpu_count() pool_scheduler = ThreadPoolScheduler(optimal_thread_count) # Create Process 1 Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon") \ .map(lambda s: intense_calculation(s)) \ .subscribe_on(pool_scheduler) \ .subscribe(on_next=lambda s: print("PROCESS 1: {0} {1}".format(current_thread().name, s)), on_error=lambda e: print(e), on_completed=lambda: print("PROCESS 1 done!")) # Create Process 2 Observable.range(1, 10) \ .map(lambda s: intense_calculation(s)) \ .subscribe_on(pool_scheduler) \ .subscribe(on_next=lambda i: print("PROCESS 2: {0} {1}".format(current_thread().name, i)), on_error=lambda e: print(e), on_completed=lambda: print("PROCESS 2 done!")) # Create Process 3, which is infinite Observable.interval(1000) \ .map(lambda i: i * 100) \ .observe_on(pool_scheduler) \ .map(lambda s: intense_calculation(s)) \ .subscribe(on_next=lambda i: print("PROCESS 3: {0} {1}".format(current_thread().name, i)), on_error=lambda e: print(e)) input("Press any key to exit\n")
def create(): return Observable.range(0, 1, scheduler)
lambda x: x % 2 ).subscribe(MyObserver()) #Transforming a sequence xs = Observable.from_(range(10)) d = xs.map( lambda x: x * 2 ).subscribe(MyObserver()) xs = Observable.from_(range(10, 20, 2)) d = xs.map( lambda x, i: "%s: %s" % (i, x * 2) ).subscribe(MyObserver()) #merge xs = Observable.range(1, 5) ys = Observable.from_("abcde") zs = xs.merge(ys).subscribe(MyObserver()) #Subjects and Streams from rx.subjects import Subject stream = Subject() stream.on_next(41) d = stream.subscribe(MyObserver()) stream.on_next(42) d.dispose() stream.on_next(43)
def create(): return Observable.range(0, 10, 2)
def create(): return Observable.range(5)
def test_for_each_on_next_throws(self): ex = Exception() xs = Observable.range(0, 10) self.assertRaises(RxException, lambda: xs.to_blocking().for_each(lambda x: _raise(ex)))
from dumper import Dumper from rx import Observable, Observer from rx.internal import extensionmethod from rx.subjects import Subject in_ = '23432/XX428/X21X71' Observable.from_(in_) \ .flat_map(lambda q: Observable.range(1, 2) if q == 'X' else Observable.just(q)) \ .buffer_with_count(2) \ .map(lambda x, i: i) \ .take(10) \ .subscribe(Dumper('s'))
# separate time and value time = time_value_record.map(lambda pair: pair[0]) signal = time_value_record.map(lambda pair: pair[1]) # timebase synchronization sync1 = Subject() sync2 = Subject() # synchronize time samples to two timebases time_sync2_bp = time.to_backpressure().zip(sync2.repeat_first(), lambda t, sync_time: t + sync_time) time_sync1_bp = time.to_backpressure().zip(sync1.repeat_first(), lambda t, sync_time: t + sync_time) # pairing time value observables time_sync1_bp.to_observable().zip(signal, lambda t, v: (t, v)).unsafe_subscribe() time_sync2_bp.to_observable().zip(signal, lambda t, v: (t, v)).unsafe_subscribe( print, on_completed=lambda: print('completed')) # emulating hot observable Observable.range(0,100) \ .map(lambda v: (float(v)+3.2)/1000) \ .map(lambda t: (t, math.sin(t/0.05*2*math.pi))) \ .unsafe_subscribe(time_value_record) Observable.just(-3.2 / 1000).unsafe_subscribe(sync1) Observable.just(2 / 1000).unsafe_subscribe(sync2)
# # def on_error(self, error): # print('on_error', error) # # letters.subscribe(Subscribe()) letters.subscribe(on_next=lambda x: print(x), on_completed=lambda: print('Done')) filtered.subscribe(lambda x: print(x)) Observable.from_(['Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon'])\ .map(lambda s: len(s))\ .filter(lambda i: i >= 5)\ .subscribe(lambda x: print(x)) Observable.range(1, 10).subscribe(lambda x: print(x)) Observable.just('Test').subscribe(lambda x: print(x)) def push_numbers(observer): observer.on_next(300) observer.on_next(500) observer.on_next(700) observer.on_completed() Observable.create(push_numbers).subscribe( lambda x: print(x), on_completed=lambda: print('CCompleted!'))
from rx import Observable, Observer # Using Observable.range() letters = Observable.range(1, 10) letters.subscribe(lambda value: print(value)) # Using Observable.just() greeting = Observable.of("Hello World!") greeting.subscribe(lambda value: print(value))
from rx import Observable scheduler = ThreadPoolScheduler() xs = Observable.range(1, 5).flat_map(lambda x: Observable.just(x, scheduler=scheduler), mapper)
def test_for_each_some_data(self): lstX = [] Observable.range(10, 10).to_blocking().for_each(lambda x: lstX.append(x)) assert(lstX == [x for x in range(10, 20)])
from rx import Observable from threading import current_thread import time, random def simulate_calculation(value): time.sleep(random.randint(5, 20)*0.1) return value # task 1 Observable.from_(["one", "two", "three", "four", "five"]) \ .map(lambda s: simulate_calculation(s)) \ .subscribe(on_next=lambda s: print("thread {0}, value {1}".format(current_thread().name, s)), on_completed=lambda: print("finished task 1")) # task 2 Observable.range(1,5) \ .map(lambda s: simulate_calculation(s)) \ .subscribe(on_next=lambda s: print("thread {0}, value {1}".format(current_thread().name, s)), on_completed=lambda: print("finished task 2"))
""" Each Subscriber to an Observable often will receive a separate stream of emissions. For instance, having two subscribers to this Observable emitting three random integers will result in both subscribers getting different numbers. """ from rx import Observable from random import randint three_emissions = Observable.range(1, 3) three_random_ints = three_emissions.map(lambda i: randint(1, 100000)) three_random_ints.subscribe(lambda i: print("Subscriber 1 Received: {0}".format(i))) three_random_ints.subscribe(lambda i: print("Subscriber 2 Received: {0}".format(i)))
def read_request(i): response = session.get(hostname + '/api/v1/orders/{}'.format(i), timeout=3) # response = session.get(hostname + '/api/v1/orders/1092872', timeout=3) return Observable.from_(response) \ .map(lambda b: response.content) optimal_thread_count = multiprocessing.cpu_count() + 1 pool_scheduler = ThreadPoolScheduler(optimal_thread_count) print("We are using {0} threads".format(optimal_thread_count)) Observable.range(1, 20) \ .flat_map(lambda s: Observable.just(s).subscribe_on(pool_scheduler).map(lambda s: read_request(s))) \ .subscribe(on_next=lambda s: print("Process 1: {0} {1}".format(current_thread().name, s)), on_error=lambda e: print(e), on_completed=lambda: print('Process 1 is finished!')) Observable.range(1, 20) \ .flat_map(lambda s: read_request(s).subscribe_on(pool_scheduler)) \ .subscribe(on_next=lambda s: print("Process 1: {0} {1}".format(current_thread().name, s)), on_error=lambda e: print(e), on_completed=lambda: print('Process 1 is finished!')) Observable.range(1, 50) \ .flat_map(lambda s: Observable.just(s).subscribe_on(pool_scheduler).flat_map(lambda s: read_request(s))) \ .subscribe(on_next=lambda s: print("Process 1: {0} {1}".format(current_thread().name, s)), on_error=lambda e: print(e), on_completed=lambda: print('Process 1 is finished!')) # switch_map - try to use
def func(x, i): print(i, ":", end=' ') return x * 2 src = Observable.from_(range(5)) src.map(func).subscribe(print) print("--------") # Merge print("Merge") src1 = Observable.range(1, 5) src2 = Observable.from_("hello") src1.merge(src2).subscribe(print) print("--------") ## Subjects and Streams print("Subjects and Streams") ob = Subject() ob.on_next(1) sub1 = ob.subscribe(print) ob.on_next(2) sub2 = ob.subscribe(print) sub1.dispose()
return value optimal_thread_count = multiprocessing.cpu_count() + 1 pool_scheduler = ThreadPoolScheduler(optimal_thread_count) print("We are using {0} threads".format(optimal_thread_count)) Observable.from_(['Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon']) \ .map(lambda s: intense_calculation(s)) \ .subscribe_on(pool_scheduler) \ .subscribe(on_next=lambda s: print("Process 1: {0} {1}".format(current_thread().name, s)), on_error=lambda e: print(e), on_completed=lambda: print('Process 1 is finished!')) Observable.range(1, 10) \ .map(lambda s: intense_calculation(s)) \ .subscribe_on(pool_scheduler) \ .subscribe(on_next=lambda s: print("Process 2: {0} {1}".format(current_thread().name, s)), on_error=lambda e: print(e), on_completed=lambda: print('Process 2 is finished!')) # input('POU\n') disposable = Observable.interval(1000) \ .map(lambda i: i * 100) \ .observe_on(pool_scheduler) \ .map(lambda s: intense_calculation(s)) \ .subscribe(on_next=lambda i: print("Process 3: {0} {1}".format(current_thread().name, i))) disposable.dispose()
return value # calculate number of CPU's, then create a ThreadPoolScheduler with that number of threads optimal_thread_count = multiprocessing.cpu_count() pool_scheduler = NewThreadScheduler() # Create Process 1 # Observable.from_(["Alpha", "Beta", "Gamma", "Delta", "Epsilon"]) \ # .map(lambda s: intense_calculation(s)) \ # .subscribe_on(pool_scheduler) \ # .subscribe(on_next=lambda s: print_obj("PROCESS 1: {0} {1}".format(current_thread().name, s)), # on_error=lambda e: print_obj(e), # on_completed=lambda: print_obj("PROCESS 1 done!")) # Create Process 2 Observable.range(1, 100) \ .map(lambda s: intense_calculation(s)) \ .observe_on(pool_scheduler) \ .subscribe(on_next=lambda i: print_obj("PROCESS 2: {0} {1}".format(current_thread().name, i)), on_error=lambda e: print_obj(e), on_completed=lambda: print_obj("PROCESS 2 done,%s!" %(str(time.time()-start_time)))) # # Create Process 3, which is infinite # Observable.interval(1000) \ # .map(lambda i: i * 100) \ # .observe_on(pool_scheduler) \ # .map(lambda s: intense_calculation(s)) \ # .subscribe(on_next=lambda i: print_obj("PROCESS 3: {0} {1}".format(current_thread().name, i)), # on_error=lambda e: print_obj(e)) input("Press any key to exit\n")
from rx import Observable letters = Observable.from_(["A", "B", "C", "D", "E", "F"]) numbers = Observable.range(1, 5) Observable.zip(letters,numbers, lambda l,n: "{0}-{1}".format(l,n)) \ .subscribe(lambda i: print(i))
from rx import Observable from random import randint three_emissions = Observable.range(1, 3) print(three_emissions[0]) three_random_ints = three_emissions.map(lambda i: randint(1, 100000)) three_random_ints.subscribe( lambda i: print("Subscriber 1 Received: {0}".format(i))) three_random_ints.subscribe( lambda i: print("Subscriber 2 Received: {0}".format(i)))