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 on_next(item): nonlocal client nonlocal bucket if type(item) is Configure: session = Session(aws_access_key_id=item.access_key, aws_secret_access_key=item.secret_key) client = session.client('s3', endpoint_url=item.endpoint_url, region_name=item.region_name) bucket = item.bucket elif type(item) is UploadObject: try: data = BytesIO(item.data) client.upload_fileobj(data, bucket, item.key) loop.call_soon_threadsafe( observer.on_next, rx.just(UploadReponse(key=item.key, id=item.id))) except: loop.call_soon_threadsafe( observer.on_next, rx.throw( Exception(UploadError(key=item.key, id=item.id)))) else: loop.call_soon_threadsafe( observer.on_error, "unknown item: {}".format(type(item)))
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 _start_async(function_async) -> Observable: try: future = function_async() except Exception as ex: # pylint: disable=broad-except return throw(ex) return from_future(future)
async def go(): nonlocal result source = rx.throw(error) try: result = await source except Exception as ex: result = ex
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_deepspeech_request(item): nonlocal model if type(item) is SpeechToText: if model is not None: try: audio = decoding.decode_audio(io.BytesIO(item.data)) text = model.stt(audio) log("STT result: {}".format(text)) observer.on_next(rx.just(TextResult( text=text, context=item.context, ))) except Exception as e: log("STT error: {}".format(e), level=logging.ERROR) observer.on_next(rx.throw(TextError( error=e, context=item.context, ))) elif type(item) is Initialize: log("initialize: {}".format(item)) model = setup_model( item.model, item.scorer, item.beam_width) else: log("unknown item: {}".format(item), level=logging.CRITICAL) observer.on_error( "Unknown item type: {}".format(type(item)))
def on_deepspeech_request(item): nonlocal model if type(item) is SpeechToText: if model is not None: try: _, audio = wav.read(io.BytesIO(item.data)) # convert to mono. # todo: move to a component or just a function here if len(audio.shape) > 1: audio = audio[:, 0] text = model.stt(audio) log("STT result: {}".format(text)) observer.on_next(rx.just(TextResult( text=text, context=item.context, ))) except Exception as e: log("STT error: {}".format(e), level=logging.ERROR) observer.on_next(rx.throw(TextError( error=e, context=item.context, ))) elif type(item) is Initialize: log("initialize: {}".format(item)) model = setup_model( item.model, item.scorer, item.beam_width) else: log("unknown item: {}".format(item), level=logging.CRITICAL) observer.on_error( "Unknown item type: {}".format(type(item)))
def subscribe(observer, scheduler=None): try: result = factory(scheduler) except Exception as ex: # By design. pylint: disable=W0703 return throw(ex).subscribe(observer) result = from_future(result) if is_future(result) else result return result.subscribe(observer, scheduler=scheduler)
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 subscribe(observer, scheduler=None): try: result = observable_factory(scheduler) except Exception as ex: # By design. pylint: disable=W0703 return throw(ex).subscribe(observer) result = from_future(result) if is_future(result) else result return result.subscribe(observer, scheduler=scheduler)
def on_next(i): if type(i) is Read: try: with open(i.path, i.mode) as content_file: content = content_file.read(i.size) data = rx.just(content) except Exception as e: data = rx.throw(e) observer.on_next( ReadResponse(id=i.id, path=i.path, data=data)) elif type(i) is ReadLine: try: with open(i.path) as content_file: ''' from_ does not work with ImmediateScheduler def on_data_subscribe(data_observer): for line in content_file: data_observer.on_next(line) data_observer.on_completed() data = Observable.create(on_data_subscribe) ''' data = rx.from_(content_file) except Exception as e: data = rx.throw(e) observer.on_next( ReadResponse(id=i.id, path=i.path, data=data)) elif type(i) is Write: try: if i.mkdirs is True: os.makedirs(os.path.split(i.path)[0], exist_ok=True) with open(i.path, i.mode) as content_file: size = content_file.write(i.data) status = 0 if size == len(i.data) else -1 except Exception as e: status = e observer.on_next( WriteResponse(id=i.id, path=i.path, status=status)) else: observer.on_error("file unknown command: {}".format(i))
def do_retry(source, tries, exception): if tries <= retries: logging.warning( 'retry_with_backoff(tries={}, retries={}, exception={}, description={})' .format(tries, retries, exception, description)) return of(None).pipe( delay(backoff(tries), TimeoutScheduler()), flat_map(source), catch(handler=lambda e, src: do_retry(src, tries + 1, e))) else: return throw(exception)
def create(): def mapper(xy): x, y = xy return "{}{}".format(x.value, y.value) return xs.pipe( ops.join( ys, lambda x: rx.timer(x.interval), lambda y: rx.timer(y.interval).pipe(ops.flat_map(rx.throw(ex) if y.value == "tin" else rx.empty())), ), ops.map(mapper), )
def create(): def mapper(x_yy): x, yy = x_yy return yy.pipe(ops.map(lambda y: '{}{}'.format(x.value, y.value))) return xs.pipe( ops.group_join( ys, lambda x: rx.timer(x.interval), lambda y: rx.timer(y.interval).pipe( ops.flat_map(rx.throw(ex) if y.value == "tin" else rx.empty())), ), ops.flat_map(mapper), )
def subscribe(observer, scheduler=None): disp = Disposable() try: resource = resource_factory() if resource is not None: disp = resource source = observable_factory(resource) except Exception as exception: # pylint: disable=broad-except d = rx.throw(exception).subscribe(observer, scheduler=scheduler) return CompositeDisposable(d, disp) return CompositeDisposable(source.subscribe(observer, scheduler=scheduler), disp)
def test_repeat_observable_repeat_count_throws(self): scheduler1 = TestScheduler() xs = rx.return_value(1).pipe(ops.repeat(3)) xs.subscribe(lambda x: _raise('ex'), scheduler=scheduler1) with self.assertRaises(RxException): scheduler1.start() scheduler2 = TestScheduler() ys = rx.throw('ex1').pipe(ops.repeat(3)) ys.subscribe(on_error=lambda ex: _raise('ex2'), scheduler=scheduler2) with self.assertRaises(RxException): scheduler2.start()
def create(): def mapper(xy): x, y = xy return "{}{}".format(x.value, y.value) return xs.pipe( ops.join( ys, lambda x: rx.timer(x.interval), lambda y: rx.timer(y.interval).pipe( ops.flat_map( rx.throw(ex) if y.value == "tin" else rx.empty())), ), ops.map(mapper), )
def create(): def mapper(x_yy): x, yy = x_yy return yy.pipe( ops.map(lambda y: '{}{}'.format(x.value, y.value))) return xs.pipe( ops.group_join( ys, lambda x: rx.timer(x.interval), lambda y: rx.timer(y.interval).pipe( ops.flat_map( rx.throw(ex) if y.value == "tin" else rx.empty())), ), ops.flat_map(mapper), )
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_forward_error(self): numbers = rx.throw(ValueError()) expected_numbers = [] actual_numbers = [] actual_error = None def on_next(i): actual_numbers.append(i) def on_error(e): nonlocal actual_error actual_error = e integer_passthrough(numbers).subscribe(on_next=on_next, on_error=on_error) self.assertIsInstance(actual_error, ValueError) self.assertEqual(expected_numbers, actual_numbers)
def test_route_error(self): actual_sequence = [] def on_chain_item(i): nonlocal actual_sequence actual_sequence.append(i) sink, route_error = make_error_router() origin = rx.from_([rx.just(1), rx.throw(-1)]).pipe( route_error(error_map=lambda e: e.args[0] * 100), ) result = rx.merge(origin, sink) disposable = result.subscribe(on_chain_item, scheduler=CurrentThreadScheduler()) disposable.dispose() expected_sequence = [1, -100] self.assertEqual(actual_sequence, expected_sequence)
def test_repeat_observable_throws(self): scheduler1 = TestScheduler() xs = rx.return_value(11).pipe(ops.repeat()) xs.subscribe(lambda x: _raise('ex'), scheduler=scheduler1) with self.assertRaises(RxException): scheduler1.start() scheduler2 = TestScheduler() ys = rx.throw('ex').pipe(ops.repeat()) ys.subscribe(lambda ex: _raise('ex'), scheduler=scheduler2) with self.assertRaises(Exception): scheduler2.start() scheduler3 = TestScheduler() zs = rx.return_value(1).pipe(ops.repeat()) d = zs.subscribe(on_completed=lambda: _raise('ex'), scheduler=scheduler3) scheduler3.schedule_absolute(210, lambda sc, st: d.dispose()) scheduler3.start()
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 on_deepspeech_request(item): nonlocal ds_model if type(item) is SpeechToText: if ds_model is not None: out = None try: # _, audio = wav.read(io.BytesIO(item.data)) unique_filename = str(uuid.uuid4()) out='/tmp/' + unique_filename + '.wav' convert(io.BytesIO(item.data),out,'s16','pcm_s16le',"mono",16000) _, audio = wav.read(out) # convert to mono. # todo: move to a component or just a function here if len(audio.shape) > 1: audio = audio[:, 0] text = ds_model.stt(audio) log("STT result: {}".format(text)) observer.on_next(rx.just(TextResult( text=text, context=item.context, ))) except Exception as e: log("STT error: {}".format(e), level=logging.ERROR) observer.on_next(rx.throw(TextError( error=e, context=item.context, ))) finally: if os.path.exists(out): os.remove(out) elif type(item) is Initialize: log("initialize: {}".format(item)) ds_model = setup_model( item.model, item.lm, item.trie, item.features or FeaturesParameters()) else: log("unknown item: {}".format(item), level=logging.CRITICAL) observer.on_error( "Unknown item type: {}".format(type(item)))
def test_retry_observable_throws(self): scheduler1 = TestScheduler() xs = rx.return_value(1).pipe(ops.retry()) xs.subscribe(lambda x: _raise('ex'), scheduler=scheduler1) with pytest.raises(RxException): scheduler1.start() scheduler2 = TestScheduler() ys = rx.throw('ex').pipe(ops.retry()) d = ys.subscribe(on_error=lambda ex: _raise('ex'), scheduler=scheduler2) scheduler2.schedule_absolute(210, lambda sc, st: d.dispose()) scheduler2.start() scheduler3 = TestScheduler() zs = rx.return_value(1).pipe(ops.retry()) zs.subscribe(on_completed=lambda: _raise('ex'), scheduler=scheduler3) with pytest.raises(RxException): scheduler3.start()
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: try: encoded_data = mp3_to_flac(item.data, samplerate, bitdepth) observer.on_next( rx.just( EncodeResult(id=item.id, key=item.key, data=encoded_data))) except: observer.on_next( rx.throw( Exception(EncodeError(key=item.key, id=item.id)))) else: observer.on_error("unknown item: {}".format(type(item)))
def connect(self): if self.instance.is_connected: error_message = "SimexIO is already connected." self.logger.warning(error_message) return rx.throw(RuntimeError(error_message)) def subscribe(observer: rx.typing.Observer, scheduler=None): try: self.logger.debug("connecting to SimexIO") self.instance.connect() except Exception as ex: self.logger.error( f"Failed to connect to SimexIO. Exception: {ex}") return self.instance.verify_compatibility().pipe( operators.flat_map(self._simex_verify_version_cb), operators.map(self._simex_enforce_port_configuration_cb), operators.map(self._simex_configure_subscription), ).subscribe(observer) return Disposable(lambda: None) return rx.create(subscribe)
def create(): return rx.throw(ex).pipe(ops.time_interval())
def create(): return throw(ex).pipe(_.debounce(10))
def create(): return rx.throw(ex).pipe(ops.timestamp())
def create(): def handler(e, source): handler_called[0] = True return o2 return rx.throw('ex').pipe(ops.catch(handler))
def factory(): return throw(ex)
def _timeout_with_mapper(first_timeout: Optional[Observable] = None, timeout_duration_mapper: Optional[Callable[[Any], Observable]] = None, other: Optional[Observable] = None ) -> Callable[[Observable], Observable]: """Returns the source observable sequence, switching to the other observable sequence if a timeout is signaled. res = timeout_with_mapper(rx.timer(500)) res = timeout_with_mapper(rx.timer(500), lambda x: rx.timer(200)) res = timeout_with_mapper(rx.timer(500), lambda x: rx.timer(200)), rx.return_value(42)) Args: first_timeout -- [Optional] Observable sequence that represents the timeout for the first element. If not provided, this defaults to rx.never(). timeout_duration_mapper -- [Optional] Selector to retrieve an observable sequence that represents the timeout between the current element and the next element. other -- [Optional] Sequence to return in case of a timeout. If not provided, this is set to rx.throw(). Returns: The source sequence switching to the other sequence in case of a timeout. """ first_timeout = first_timeout or rx.never() other = other or rx.throw(Exception('Timeout')) def timeout_with_mapper(source: Observable) -> Observable: def subscribe(observer, scheduler=None): subscription = SerialDisposable() timer = SerialDisposable() original = SingleAssignmentDisposable() subscription.disposable = original switched = False _id = [0] def set_timer(timeout: Observable) -> None: my_id = _id[0] def timer_wins(): return _id[0] == my_id d = SingleAssignmentDisposable() timer.disposable = d def on_next(x): if timer_wins(): subscription.disposable = other.subscribe(observer, scheduler=scheduler) d.dispose() def on_error(e): if timer_wins(): observer.on_error(e) def on_completed(): if timer_wins(): subscription.disposable = other.subscribe(observer) d.disposable = timeout.subscribe_(on_next, on_error, on_completed, scheduler) set_timer(first_timeout) def observer_wins(): res = not switched if res: _id[0] += 1 return res def on_next(x): if observer_wins(): observer.on_next(x) timeout = None try: timeout = timeout_duration_mapper(x) except Exception as e: observer.on_error(e) return set_timer(timeout) def on_error(error): if observer_wins(): observer.on_error(error) def on_completed(): if observer_wins(): observer.on_completed() original.disposable = source.subscribe_(on_next, on_error, on_completed, scheduler) return CompositeDisposable(subscription, timer) return Observable(subscribe) return timeout_with_mapper
def test_run_error(self): with pytest.raises(RxException): rx.throw(RxException()).run()
def _timeout(duetime: typing.AbsoluteTime, other: Optional[Observable] = None, scheduler: Optional[typing.Scheduler] = None ) -> Callable[[Observable], Observable]: other = other or throw(Exception("Timeout")) other = from_future(other) if is_future(other) else other def timeout(source: Observable) -> Observable: """Returns the source observable sequence or the other observable sequence if duetime elapses. Examples: >>> res = timeout(source) Args: source: Source observable to timeout Returns: An obserable sequence switching to the other sequence in case of a timeout. """ def subscribe(observer, scheduler_=None): _scheduler = scheduler or scheduler_ or timeout_scheduler if isinstance(duetime, datetime): scheduler_method = _scheduler.schedule_absolute else: scheduler_method = _scheduler.schedule_relative switched = [False] _id = [0] original = SingleAssignmentDisposable() subscription = SerialDisposable() timer = SerialDisposable() subscription.disposable = original def create_timer(): my_id = _id[0] def action(scheduler, state=None): switched[0] = (_id[0] == my_id) timer_wins = switched[0] if timer_wins: subscription.disposable = other.subscribe(observer, scheduler=scheduler) timer.disposable = scheduler_method(duetime, action) create_timer() def on_next(value): send_wins = not switched[0] if send_wins: _id[0] += 1 observer.on_next(value) create_timer() def on_error(error): on_error_wins = not switched[0] if on_error_wins: _id[0] += 1 observer.on_error(error) def on_completed(): on_completed_wins = not switched[0] if on_completed_wins: _id[0] += 1 observer.on_completed() original.disposable = source.subscribe_(on_next, on_error, on_completed, scheduler_) return CompositeDisposable(subscription, timer) return Observable(subscribe) return timeout
def throw(e): # noinspection PyBroadException try: raise e except Exception: return rx.throw(e)
def test_throw_observer_throws(self): scheduler = TestScheduler() xs = throw('ex') xs.subscribe(lambda x: None, lambda ex: _raise('ex'), lambda: None, scheduler=scheduler) self.assertRaises(RxException, scheduler.start)
def _timeout_with_mapper( first_timeout: Optional[Observable] = None, timeout_duration_mapper: Optional[Callable[[Any], Observable]] = None, other: Optional[Observable] = None ) -> Callable[[Observable], Observable]: """Returns the source observable sequence, switching to the other observable sequence if a timeout is signaled. res = timeout_with_mapper(rx.timer(500)) res = timeout_with_mapper(rx.timer(500), lambda x: rx.timer(200)) res = timeout_with_mapper(rx.timer(500), lambda x: rx.timer(200)), rx.return_value(42)) Args: first_timeout -- [Optional] Observable sequence that represents the timeout for the first element. If not provided, this defaults to rx.never(). timeout_duration_mapper -- [Optional] Selector to retrieve an observable sequence that represents the timeout between the current element and the next element. other -- [Optional] Sequence to return in case of a timeout. If not provided, this is set to rx.throw(). Returns: The source sequence switching to the other sequence in case of a timeout. """ first_timeout = first_timeout or rx.never() other = other or rx.throw(Exception('Timeout')) def timeout_with_mapper(source: Observable) -> Observable: def subscribe(observer, scheduler=None): subscription = SerialDisposable() timer = SerialDisposable() original = SingleAssignmentDisposable() subscription.disposable = original switched = False _id = [0] def set_timer(timeout: Observable) -> None: my_id = _id[0] def timer_wins(): return _id[0] == my_id d = SingleAssignmentDisposable() timer.disposable = d def on_next(x): if timer_wins(): subscription.disposable = other.subscribe( observer, scheduler=scheduler) d.dispose() def on_error(e): if timer_wins(): observer.on_error(e) def on_completed(): if timer_wins(): subscription.disposable = other.subscribe(observer) d.disposable = timeout.subscribe_(on_next, on_error, on_completed, scheduler) set_timer(first_timeout) def observer_wins(): res = not switched if res: _id[0] += 1 return res def on_next(x): if observer_wins(): observer.on_next(x) timeout = None try: timeout = timeout_duration_mapper(x) except Exception as e: observer.on_error(e) return set_timer(timeout) def on_error(error): if observer_wins(): observer.on_error(error) def on_completed(): if observer_wins(): observer.on_completed() original.disposable = source.subscribe_(on_next, on_error, on_completed, scheduler) return CompositeDisposable(subscription, timer) return Observable(subscribe) return timeout_with_mapper
def factory(): return throw('ex')
def extract_state(status): state = status['state'] if state == 'FAILED': return throw(ee.EEException(status.get('error_message'))) else: return of(state)
def closing(): return rx.throw(ex)