def test_starmap_subscription_error(self): mapper = ops.starmap(lambda x, y: (x, y)) with self.assertRaises(RxException): return_value((1, 10)).pipe( mapper ).subscribe(lambda x: _raise("ex")) with self.assertRaises(RxException): throw('ex').pipe( mapper ).subscribe(on_error=lambda ex: _raise(ex)) with self.assertRaises(RxException): empty().pipe( mapper ).subscribe(lambda x: x, lambda ex: ex, lambda: _raise('ex')) def subscribe(observer, scheduler=None): _raise('ex') with self.assertRaises(RxException): create(subscribe).pipe( mapper ).subscribe()
def basic_sample_1(): """ Basic usage of 'rx.create' :return: """ def push_five_strings(observer, scheduler): print(scheduler) observer.on_next("Alpha") time.sleep(random.randint(5, 20) * 0.1) observer.on_next("Beta") time.sleep(random.randint(5, 20) * 0.1) observer.on_next("Gamma") time.sleep(random.randint(5, 20) * 0.1) observer.on_next("Delta") time.sleep(random.randint(5, 20) * 0.1) observer.on_next("Epsilon") time.sleep(random.randint(5, 20) * 0.1) observer.on_completed() source1 = create(push_five_strings) source1.subscribe( on_next=on_success, on_error=on_error, on_completed=on_complete, )
def _to_deque(source): def on_subscribe(observer, scheduler): acc = deque() def on_next(i): nonlocal acc if extend is True: acc.extend(i) else: acc.append(i) def on_completed(): print("to_deque now flushing") try: while True: observer.on_next(acc.popleft()) except IndexError: pass observer.on_completed() source.subscribe(on_next=on_next, on_completed=on_completed, on_error=observer.on_error) return rx.create(on_subscribe)
def from_aiter(iterator, feedback: Optional[Observable] = None): # noinspection PyUnusedLocal def on_subscribe(observer: Observer, scheduler): async def _aio_next(): try: event = await iterator.__anext__() if isinstance(event, OnNext): observer.on_next(event.value) elif isinstance(event, OnError): observer.on_error(event.exception) elif isinstance(event, OnCompleted): observer.on_completed() except StopAsyncIteration: pass except Exception as exception: logger().error(str(exception), exc_info=True) observer.on_error(exception) def create_next_task(): asyncio.create_task(_aio_next()) return feedback.subscribe(on_next=lambda i: create_next_task()) return rx.create(on_subscribe)
def run(): parser = argparse.ArgumentParser( prog='entityfactspicturesmetadataharvester', description= 'Reads depiction information (images URLs) from given EntityFacts sheets (as line-delimited JSON records) and retrieves the (Wikimedia Commons file) metadata of these pictures (as line-delimited JSON records).', epilog= 'example: entityfactspicturesmetadataharvester < [INPUT LINE-DELIMITED JSON FILE WITH ENTITYFACTS SHEETS] > [OUTPUT PICTURES METADATA LINE-DELIMITED JSON FILE]', formatter_class=argparse.ArgumentDefaultsHelpFormatter) args = parser.parse_args() if hasattr(args, 'help') and args.help: parser.print_usage(sys.stderr) exit(-1) source = rx.create(push_input) source_connectable_obs = source.pipe(op.publish()) # Wikimedia Commons file metadata harvesting do_harvesting(source_connectable_obs, get_metadata_url, METADATA_CONTENT_TYPE, WIKIMEDIA_COMMONS_FILE_METADATA_HARVESTING, METADATA_THREAD_POOL_SCHEDULER) source_connectable_obs.connect()
def _unframe(source): def on_subscribe(observer, scheduler): acc = b'' def on_next(i): nonlocal acc offset = 0 bio = io.BytesIO() bio.write(acc) bio.write(i) bio_len = len(bio.getbuffer()) bio.seek(offset, io.SEEK_SET) while bio_len - offset >= prefix_size: size = int.from_bytes(bio.read(prefix_size), byteorder=byteorder) if bio_len - offset - prefix_size >= size: data = bio.read(size) offset += size + prefix_size observer.on_next(data) else: break bio.seek(offset, io.SEEK_SET) acc = bio.read() bio.close() return source.subscribe( on_next=on_next, on_completed=observer.on_completed, on_error=observer.on_error ) return rx.create(on_subscribe)
def _add_observer(self, path: str): # noinspection PyUnusedLocal def _f(observer: Observer, scheduler: typing.Optional[Scheduler]): self.observers.setdefault(path, []).append(observer) # noinspection PyTypeChecker self.observables[path] = rx.create(_f)
def _calc(source): def subscribe(observer, scheduler): def on_next(obj): if not "records" in obj.keys(): observer.on_error( "Input tuple must contain pandas data frame.") voltage = np.array( obj["records"]['voltage'].values).flatten() current = np.array( obj["records"]['current'].values).flatten() if not len(voltage) == len(current): observer.on_error( f"Invalid current and voltage input for data point {obj['i']}" ) else: rms_v = self.calculate_rms(voltage) rms_c = self.calculate_rms(current) obj['s'] = rms_v * rms_c observer.on_next(obj) return source.subscribe(on_next, observer.on_error, observer.on_completed, scheduler) return rx.create(subscribe)
def _split(source): def on_subscribe(observer, scheduler): current_predicate = None split_observable = Subject() def on_next(i): nonlocal current_predicate nonlocal split_observable new_predicate = predicate(i) if current_predicate is None: current_predicate = new_predicate observer.on_next(split_observable) if new_predicate != current_predicate: current_predicate = new_predicate split_observable.on_completed() split_observable = Subject() observer.on_next(split_observable) split_observable.on_next(i) def on_completed(): split_observable.on_completed() observer.on_completed() return source.subscribe( on_next=on_next, on_completed=on_completed, on_error=observer.on_error, ) return rx.create(on_subscribe)
def _on_subscribe(source): def subscribe(observer, scheduler): disposable = source.subscribe(observer, scheduler=scheduler) post_action() return disposable return rx.create(subscribe)
def observe_signal(self, service_name='*', object_path='*', interface='*', member='*'): match_rule = _create_match_rule('signal', service_name, object_path, interface, member) @op.map def resolve_signal(s: UnresolvedSignal) -> bool: return s.resolve(service_name, object_path, interface, member) @op.filter def not_none(i): return i is not None def subscribe(observer: Observer[Message], scheduler=None): scheduler = scheduler or self._scheduler subs = self._signals.pipe(resolve_signal, not_none).subscribe(observer, scheduler=scheduler) self._connection.bus_add_match(match_rule) def dispose(): self._connection.bus_remove_match(match_rule) subs.dispose() return Disposable(dispose) return rx.create(subscribe)
async def tcp_client(host, port): def on_connection_subscribe(observer, reader, writer): async def handle_connection(observer, reader, writer): while True: try: data = await reader.read(100) if data == b'': break loop.call_soon(observer.on_next, Data(data=data)) except Exception as e: loop.call_soon(observer.on_error(e)) break loop.call_soon(observer.on_completed) writer.close() asyncio.ensure_future( handle_connection(observer, reader, writer)) try: reader, writer = await asyncio.open_connection(host, port, loop=loop) connection = rx.create( lambda o, s: on_connection_subscribe( o, reader, writer)) observer.on_next( Connection(id=writer, observable=connection)) except Exception as e: loop.call_soon(observer.on_error(e))
def _create(): def subscribe(o, scheduler=None): is_stopped = [False] o.on_next(1) o.on_next(2) def action1(scheduler, state): if not is_stopped[0]: return o.on_next(3) scheduler.schedule_relative(600, action1) def action2(scheduler, state): if not is_stopped[0]: return o.on_next(4) scheduler.schedule_relative(700, action2) def action3(scheduler, state): if not is_stopped[0]: return o.on_next(5) scheduler.schedule_relative(900, action3) def action4(scheduler, state): if not is_stopped[0]: return o.on_next(6) scheduler.schedule_relative(1100, action4) def dispose(): is_stopped[0] = True return dispose return rx.create(subscribe)
def test_retry_observable_retry_count_throws(self): scheduler1 = TestScheduler() xs = rx.return_value(1).pipe(ops.retry(3)) xs.subscribe(lambda x: _raise('ex'), scheduler=scheduler1) self.assertRaises(RxException, scheduler1.start) scheduler2 = TestScheduler() ys = rx.throw('ex').pipe(ops.retry(100)) d = ys.subscribe(on_error=lambda ex: _raise('ex'), scheduler=scheduler2) def dispose(_, __): d.dispose() scheduler2.schedule_absolute(0, dispose) scheduler2.start() scheduler3 = TestScheduler() zs = rx.return_value(1).pipe(ops.retry(100)) zs.subscribe(on_completed=lambda: _raise('ex'), scheduler=scheduler3) with pytest.raises(RxException): scheduler3.start() xss = rx.create(lambda o: _raise('ex')).pipe(ops.retry(100)) with pytest.raises(Exception): xss.subscribe()
def _calc(source): def subscribe(observer, scheduler): def on_next(obj): if not "records" in obj.keys(): observer.on_error( "Input tuple must contain pandas data frame.") voltage = np.array( obj["records"]['voltage'].values).flatten() current = np.array( obj["records"]['current'].values).flatten() if not len(voltage) == len(current): observer.on_error( f"Invalid current and voltage input for data point {obj['i']}" ) else: power = voltage * current p_len = settings.PERIOD_LENGTH p = [] for i in range(0, len(power), p_len): if i + p_len <= len(power): p_sig = power[i:int(i + p_len)] p.append(np.mean(p_sig)) obj['p'] = np.array(p) obj['p_log'] = np.log(p) observer.on_next(obj) return source.subscribe(on_next, observer.on_error, observer.on_completed, scheduler) return rx.create(subscribe)
def _mm(source): def subscribe(observer, scheduler): window = [ Queue(maxsize=q_size), ] def on_next(obj): nonlocal window if type(obj) != dict: observer.on_error( "Error occurred when applying SMA de-noising. Make sure to pass dict " "elements.") else: if window[0].full(): window[0].get_nowait() window[0].put_nowait((obj['i'], obj)) else: window[0].put_nowait((obj['i'], obj)) dl = [] for el in window[0].queue: dl.append(el[1]) data = pd.DataFrame(dl, columns=['p', 's', 'q']) obj['p_smooth'] = np.median(np.array(data['p'])) obj['q_smooth'] = np.median(np.array(data['q'])) observer.on_next(obj) return source.subscribe(on_next, observer.on_error, observer.on_completed, scheduler) return rx.create(subscribe)
def _mark(source): def subscribe(observer, scheduler): predecessor = [0] last_event = [0] def on_next(obj): nonlocal predecessor nonlocal last_event current_sign = np.sign(obj['diff_smooth']) if predecessor[0] == 0: predecessor[0] = current_sign if predecessor[0] != current_sign and ( obj["i"] > last_event[0] + self.window_size or last_event[0] == 0): obj["change_point"] = True last_event[0] = obj["i"] + 1 else: obj["change_point"] = False predecessor[0] = current_sign observer.on_next(obj) return source.subscribe(on_next, observer.on_error, observer.on_completed, scheduler) return rx.create(subscribe)
def _transform(source): def subscribe(observer, scheduler=None): def on_next(obj): if obj.status_code != 200 and obj.status_code != 404: err = f"Error transforming request due to code {obj.status_code}.", obj.status_code print(err) observer.on_error(err) observer.on_completed() elif obj.status_code == 404: self.result = obj.content observer.on_completed() else: try: obj = json.loads(obj.content)["records"] idx = int(obj[0]['i'] / 1000) res = {'i': idx, 'records': obj} self.start_time_dict[idx] = time.time() observer.on_next(res) except: observer.on_completed() return source.subscribe(on_next, observer.on_error, observer.on_completed, scheduler) return rx.create(subscribe)
def encoder(sink): def on_subscribe(observer, scheduler): samplerate = None bitdepth = None def on_next(item): nonlocal samplerate nonlocal bitdepth if type(item) is Configure: print("configure: {}".format(item)) samplerate = item.samplerate bitdepth = item.bitdepth elif type(item) is EncodeMp3: encoded_data = mp3_to_flac(item.data, samplerate, bitdepth) observer.on_next( EncodeResult(id=item.id, key=item.key, data=encoded_data)) else: observer.on_error("unknown item: {}".format(type(item))) sink.request.subscribe( on_next=on_next, on_error=lambda e: observer.on_error(e), on_completed=lambda: observer.on_completed(), ) return Source(response=rx.create(on_subscribe))
def flow_control(should_stop: subject.BehaviorSubject) -> rx.Observable: """ Flow control operator. Buffer the previous items and emit them gracefully (respecting the given should_stop/back pressure) :param should_stop: :return: """ buffer = [] _stop = subject.Subject() _upstream_completed = False def on_error(ex): _stop.on_next(0) raise ex def on_upstream_completed(): _upstream_completed = True def upstream(source: rx.Observable): source.pipe(operators.take_until(_stop)).subscribe( lambda x: buffer.append(x), on_error, on_upstream_completed) def downstream_subscribe(observer: rx.core.Observer, sch: rx.typing.Scheduler = None): def emit_next(): if len(buffer): observer.on_next(buffer.pop(0)) else: if _upstream_completed: observer.on_completed() def schedule_emit_next_until(until: subject.Subject): stop_emitting = False def _action(sch: rx.typing.Scheduler, state=None): emit_next() def until_on_next(v): nonlocal stop_emitting stop_emitting = True until.pipe(operators.take_until(_stop)).subscribe(until_on_next, scheduler=sch) if not stop_emitting: sch.schedule(_action) def should_stop_updated(val: bool): if val: # should stop, do nothing until next message pass else: # normal operation # Cannot guarantee that the should_stop will emit every time the value is received. schedule_emit_next_until(should_stop) should_stop.pipe( operators.take_until(_stop)).subscribe(should_stop_updated) return rx.create(downstream_subscribe)
def from_sensor(sensor, idx): def subscribe(observer, scheduler): def callback(image): #print('from_sensor', idx, threading.get_ident()) observer.on_next(image_carla2numpy(image)) sensor.listen(callback) return rx.create(subscribe)
def main(): source = create(push_temperature_factory) source.subscribe( on_next=lambda event: print(event), on_error=lambda e: print("Error Occurred: {0}".format(e)), on_completed=lambda: print("Done!"), )
def _create(): def subscribe(o, scheduler=None): o.on_error(ex) o.on_next(100) o.on_error('foo') o.on_completed() return lambda: None return rx.create(subscribe)
def test_map_throws(self): mapper = map(lambda x: x) with self.assertRaises(RxException): return_value(1).pipe(mapper).subscribe(lambda x: _raise("ex")) with self.assertRaises(RxException): throw('ex').pipe(mapper).subscribe(on_error=lambda ex: _raise(ex)) with self.assertRaises(RxException): empty().pipe(mapper).subscribe(lambda x: x, lambda ex: ex, lambda: _raise('ex')) def subscribe(observer, scheduler=None): _raise('ex') with self.assertRaises(RxException): create(subscribe).pipe(map(lambda x: x)).subscribe()
def main(): origin = rx.create(source) origin.pipe( ops.skip(20), ops.map(lambda x: x % 7), ops.filter(lambda x: x > 3), ).subscribe_(on_next=print)
def _process(source): def subscribe(observer, scheduler): def on_next(obj): nonlocal self if "cluster_combination" in obj.keys(): X = obj["X"] self.backward_cluster_structure = self.clustering cluster_combination_balanced = obj[ "cluster_combination"] for i in range(1, len(X) - 1): X_new = X[i:] self._update_clustering( X_new ) # Pushes the new clustering structure into self.clustering checked_clusters = self._check_model_constraints( obj=obj, temp_eps=0.8) if not checked_clusters: status = "break" cluster_combination_balanced = self._rollback( status=status, cluster_combination_balanced= cluster_combination_balanced, i=i) break else: cluster_combination_below_loss = self._compute_loss( obj, loss_threshold=40) if not cluster_combination_below_loss: status = "break" cluster_combination_balanced = self._rollback( status=status, cluster_combination_balanced= cluster_combination_balanced, i=i) break else: status = "continue" cluster_combination_balanced = self._rollback( status=status, cluster_combination_balanced= cluster_combination_balanced, cluster_combination_below_loss= cluster_combination_below_loss, i=i) continue event_start = cluster_combination_balanced[2][0] event_end = cluster_combination_balanced[2][-1] obj["event_edges"] = (event_start, event_end) observer.on_next(obj) return source.subscribe(on_next, observer.on_error, observer.on_completed, scheduler) return rx.create(subscribe)
def cozmo_driver(sink): def subscribe(observer, scheduler=None): action_handlers = {} def on_next(command): if "type" not in command or command["type"] == "start": factory = getattr(robot, command["name"]) if type(command["value"]) is dict: if "in_parallel" in command["value"]: logging.warning( 'Overwriting "in_parallel" field to "True"') command["value"]["in_parallel"] = True command["value"]["in_parallel"] = True action_handlers[command["name"]] = factory( **command["value"]) elif type(command["value"]) is list: action_handlers[command["name"]] = factory( **command["value"], in_parallel=True) else: action_handlers[command["name"]] = factory( command["value"], in_parallel=True) if "id" in command: action_handlers[command["name"]].id = command["id"] else: action_handlers[command["name"]].id = str(uuid.uuid4()) def on_complete_cb(evt, **kwargs): observer.on_next( dict( { "id": action_handlers[command["name"]].id, "evt": evt, }, **kwargs, )) action_handlers[command["name"]].add_event_handler( cozmo.action.EvtActionCompleted, on_complete_cb) elif command["type"] == "abort": if action_handlers[command["name"]].is_running: if ("id" in command and "id" in action_handlers and command["id"] is not action_handlers["id"]): logging.warning( f'Action id does not match {action_handlers["id"]} {command["id"]}' ) return action_handlers[command["name"]].abort() else: logging.warning( f'Action {command["name"]} is not running') else: logging.warning('Unknown command["type"]', command["type"]) sink.subscribe(on_next=on_next, scheduler=scheduler) source = rx.create(subscribe) return source
def test_concat_forward_none_scheduler(self): subscribe_schedulers = {'e1': 'unknown', 'e2': 'unknown'} def subscribe_e1(observer, scheduler='not_set'): subscribe_schedulers['e1'] = scheduler observer.on_completed() def subscribe_e2(observer, scheduler='not_set'): subscribe_schedulers['e2'] = scheduler observer.on_completed() e1 = rx.create(subscribe_e1) e2 = rx.create(subscribe_e2) stream = e1.pipe(ops.concat(e2)) stream.subscribe() assert subscribe_schedulers['e1'] is None assert subscribe_schedulers['e2'] is None
def _fn_top(source): def subscribe(observer, scheduler=None): def on_next(value): observer.on_next(fn(value)) return source.subscribe(on_next, observer.on_error, observer.on_completed, scheduler) return rx.create(subscribe)
def driver(sink): ''' Routes must be configured before starting the server. ''' session = None def on_response_subscribe(observer, scheduler): async def _request(request): nonlocal session if session is None: session = aiohttp.ClientSession() try: response = await session.request( request.method, request.url, params=request.params, data=request.data, headers=request.headers, allow_redirects=request.allow_redirects, max_redirects=request.max_redirects) data = await response.read() observer.on_next( Response(id=request.id, response=rx.just( HttpResponse( status=response.status, reason=response.status, method=response.method, url=response.url, data=data, cookies=response.cookies, headers=response.headers, content_type=response.content_type)))) except Exception as e: #print("exception: {}, {}".format(e, traceback.format_exc())) observer.on_next( Response(id=request.id, response=rx.throw(e))) pass def on_request_item(i): if type(i) is Request: asyncio.ensure_future(_request(i), loop=loop) else: print("received unknown item: {}".format(type(i))) def on_request_error(e): print("http sink error: {}, {}".format(e, traceback.format_exc())) return sink.request.subscribe(on_next=on_request_item, on_error=on_request_error) return Source(response=rx.create(on_response_subscribe))
def from_sensor(sensor): def subscribe(observer, scheduler): def callback(measurement): #print('from_sensor', idx, threading.get_ident()) observer.on_next(lidar_carla2numpy(measurement)) sensor.listen(callback) return rx.create(subscribe)
def factory(scheduler): count[0] += 1 def create(obs, scheduler=None): def func(): disconnected[0] = True return func return rx.create(create)
def to_double(val): def subscribe(observer, dispose): try: time.sleep(val) observer.on_next(val*2) except TypeError: observer.on_error("Error doubling val:", val) return rx.create(subscribe)
def test_observable_to_list(): _list = [1, 2, 3] stream = from_list(_list) assert _list == Combination.observable_to_list(stream) with pytest.raises(Exception): stream = create( lambda observer, scheduler: observer.on_error(Exception)) Combination.observable_to_list(stream)
def create(): def predicate(x): n[0] += 1 return n[0] < 100 def subscribe(o, scheduler=None): o.on_next(1) o.on_completed() return lambda: None return rx.create(subscribe).pipe(ops.while_do(predicate))
def test_observe_on_forward_subscribe_scheduler(self): scheduler = ImmediateScheduler() expected_subscribe_scheduler = ImmediateScheduler() actual_subscribe_scheduler = None def subscribe(observer, scheduler): nonlocal actual_subscribe_scheduler actual_subscribe_scheduler = scheduler observer.on_completed() xs = rx.create(subscribe) xs.pipe(ops.observe_on(scheduler)).subscribe( scheduler=expected_subscribe_scheduler) assert expected_subscribe_scheduler == actual_subscribe_scheduler
def test_as_observable_isnoteager(self): scheduler = TestScheduler() subscribed = [False] def subscribe(obs, scheduler=None): subscribed[0] = True disp = scheduler.create_hot_observable(on_next(150, 1), on_next(220, 2), on_completed(250)).subscribe(obs) def func(): return disp.dispose() return func xs = rx.create(subscribe) xs.pipe(ops.as_observable()) assert not subscribed[0] def create(): return xs.pipe(ops.as_observable()) scheduler.start(create) assert subscribed[0]
def test_map_with_index_throws(self): with self.assertRaises(RxException): mapper = map_indexed(lambda x, index: x) return return_value(1).pipe( mapper ).subscribe(lambda x: _raise('ex')) with self.assertRaises(RxException): return throw('ex').pipe( mapper ).subscribe(lambda x: x, lambda ex: _raise(ex)) with self.assertRaises(RxException): return empty().pipe( mapper ).subscribe(lambda x: x, lambda ex: None, lambda: _raise('ex')) with self.assertRaises(RxException): return create(lambda o, s: _raise('ex')).pipe( mapper ).subscribe()
def test_create_observer_throws(self): def subscribe(o, scheduler=None): o.on_next(1) return lambda: None with self.assertRaises(RxException): rx.create(subscribe).subscribe(lambda x: _raise('ex')) def subscribe2(o, scheduler=None): o.on_error('exception') return lambda: None with self.assertRaises(RxException): rx.create(subscribe2).subscribe(on_error=lambda ex: _raise('ex')) def subscribe3(o, scheduler=None): o.on_completed() return lambda: None with self.assertRaises(RxException): rx.create(subscribe3).subscribe(on_completed=lambda: _raise('ex'))
def test_create_exception(self): with self.assertRaises(RxException): rx.create(lambda o, s: _raise('ex')).subscribe()
def _create(): def subscribe(o, scheduler=None): o.on_next(1) o.on_next(2) return lambda: None return rx.create(subscribe)