def test_fork_join_empty_empty(self): scheduler = TestScheduler() e1 = rx.empty() e2 = rx.empty() results = scheduler.start(lambda: rx.fork_join(e1, e2)) assert results.messages == [on_completed(200)]
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 test_merge_empty2(self): scheduler = TestScheduler() e1 = rx.empty() e2 = rx.empty() def create(): return rx.merge(e1, e2) results = scheduler.start(create) assert results.messages == [on_completed(200)]
def test_stddev_completed_on_empty(): actual_result = [] actual_completed = [] rx.empty().pipe(rs.math.formal.stddev(reduce=True)).subscribe( on_next=actual_result.append, on_completed=lambda: actual_completed.append(True)) assert actual_completed == [True] assert actual_result == [0.0]
def delete_downloaded(f): if delete_after_download: try: return delete_file(credentials, f).pipe(flat_map(lambda _: empty())) except HttpError: logging.warning( 'Failed to delete downloaded file {}'.format(file)) return empty() else: return empty()
def next_token(self, token): if token == b"(": self.stack.append([]) return rx.empty() elif token == b")": part = self.stack.pop() if self.stack: self.stack[-1].append(part) return rx.empty() else: return rx.of(part) else: self.stack[-1].append(token) return rx.empty()
def take(source: Observable) -> Observable: """Returns a specified number of contiguous elements from the start of an observable sequence. >>> take(source) Keyword arguments: count -- The number of elements to return. Returns an observable sequence that contains the specified number of elements from the start of the input sequence. """ if not count: return empty() def subscribe(observer, scheduler=None): remaining = count def on_next(value): nonlocal remaining if remaining > 0: remaining -= 1 observer.on_next(value) if not remaining: observer.on_completed() return source.subscribe_(on_next, observer.on_error, observer.on_completed, scheduler) return Observable(subscribe)
def _if_then(condition: Callable[[], bool], then_source: Observable, else_source: Observable = None) -> Observable: """Determines whether an observable collection contains values. Example: 1 - res = rx.if_then(condition, obs1) 2 - res = rx.if_then(condition, obs1, obs2) Args: condition: The condition which determines if the then_source or else_source will be run. then_source: The observable sequence or Promise that will be run if the condition function returns true. else_source: [Optional] The observable sequence or Promise that will be run if the condition function returns False. If this is not provided, it defaults to rx.empty Returns: An observable sequence which is either the then_source or else_source. """ else_source = else_source or rx.empty() then_source = rx.from_future(then_source) if is_future(then_source) else then_source else_source = rx.from_future(else_source) if is_future(else_source) else else_source def factory(_: abc.Scheduler): return then_source if condition() else else_source return rx.defer(factory)
def test_empty_observer_throw_exception(self): scheduler = TestScheduler() xs = empty() xs.subscribe(lambda x: None, lambda ex: None, lambda: _raise('ex'), scheduler=scheduler) with self.assertRaises(RxException): scheduler.start()
def delete(file): try: return delete_file(credentials, file, retries) except HttpError: logging.warning('Failed to delete file {} with path {}'.format( file, path)) return empty()
def _build_vrt(): return concat( progress(default_message='Building VRT...', message_key='tasks.retrieve.image_to_sepal.building_vrt'), build_vrt(destination=destination_path + '/' + description + '.vrt', files=destination_path + '/*.tif').pipe( flat_map(lambda _: empty())))
def _create_drive_folder(): return concat( progress( default_message='Creating Google Drive download folder...', message_key= 'tasks.retrieve.time_series_to_sepal.creating_drive_folder'), create_folder_with_path(credentials, drive_folder_path).pipe( flat_map(lambda _: empty())))
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 _transfer_file(path): result = subprocess.run([f"mv '{path}' {paths.KINDLE_DOCUMENTS}"], shell=True, stderr=subprocess.DEVNULL) if (result.returncode == 0): return rx.empty() else: return rx.of(actions.FailedTransfer(path))
def _delete_drive_folder(): return concat( progress( default_message='Deleting Google Drive download folder...', message_key= 'tasks.retrieve.image_to_sepal.deleting_drive_folder'), delete_file_with_path(credentials, path=drive_folder_path).pipe( flat_map(lambda _: empty())))
def get_values(self) -> Observable: # return rx.create(lambda observer, scheduler: scheduler.schedule_periodic( # period=0, # action=lambda *_: self.__read_all_values(observer) # )) print("GET VALUES") return rx.empty() pass
def _export_dates(dates, drive_description, folder): date_table = ee.FeatureCollection( dates.map(lambda d: ee.Feature(None, {'date': d}))) return export_table_to_drive(credentials, date_table, description=drive_description, folder=folder, file_format='CSV').pipe( flat_map(lambda _: empty()))
def test_fork_join_empty_non_empty(self): scheduler = TestScheduler() e1 = rx.empty() e2 = scheduler.create_hot_observable( [on_next(150, 1), on_next(230, 2), on_completed(300)]) results = scheduler.start(lambda: rx.fork_join(e1, e2)) assert results.messages == [on_completed(200)]
def collect(messages: Observable[IbApiMessage]) -> Observable[List[OpenOrder]]: # OpenOrders might emit (effective synchronously) something like: # 1, (end), 0, 1, (end) # Simply ending on the first 'end' would miss order 0, so wait up 1 sec. # Note how OPEN_ORDER_END is ignored as a result. return messages.pipe( _.timeout(0.1), _.on_error_resume_next(empty()), _.filter(lambda m: _is_open_order(m)), _.reduce(lambda orders, data: _add_data_to_orders(data, orders), {}), _.map(lambda orders_map: list(orders_map.values())))
def next_character(self, char): if self.in_string: if char == b'"': stored = b"".join(self.stored).decode("utf8") self.stored = [] self.in_string = False return rx.of(stored) else: self.stored.append(char) return rx.empty() elif char == b'"': self.in_string = True return rx.empty() elif char in b"() \r\n\t": stored = b"".join(self.stored) self.stored = [] return rx.of(stored, char.strip()) else: self.stored.append(char) return rx.empty()
def run_metaepoch(self) -> Observable: if self.alive: epoch_job = StepsRun(self.metaepoch_len) return epoch_job.create_job(self.driver).pipe( ops.map(lambda message: self.fill_node_info(message)), ops.do_action( lambda message: self.update_current_cost(message)), ops.do_action( on_completed=lambda: self._after_metaepoch()), ) return rx.empty()
def cancel(): if task.state in [ Task.State.COMPLETED, Task.State.CANCEL_REQUESTED, Task.State.CANCELLED, Task.State.FAILED ]: return ee.InitializeThread(credentials) try: task.cancel() except Exception: logging.exception('{}: cancelling failed'.format(task)) return empty()
def _export_and_download_stack(stack, export_description, year_dir): stack_drive_description = 'stack_' + export_description stack_drive_folder = '{}-{}'.format(stack_drive_description, str(uuid.uuid4())) stack_drive_path = '{}/{}'.format(drive_folder_path, stack_drive_folder) create_stack_drive_folder = create_folder_with_path( credentials, stack_drive_path).pipe(flat_map(lambda _: empty())) export_stack = _export_stack(stack, stack_drive_description, stack_drive_folder).pipe( flat_map(lambda _: empty())) download_stack_from_drive = _download_from_drive( path=stack_drive_path, destination=year_dir).pipe( map(lambda p: {'stack_bytes': p.downloaded_bytes})) return concat( create_stack_drive_folder, export_stack, of({'exported': 1}), download_stack_from_drive, of({'downloaded': 1}), )
def test_finally_only_called_once_empty(self): invasserte_count = [0] def action(): invasserte_count[0] += 1 return invasserte_count some_observable = rx.empty().pipe(ops.finally_action(action)) d = some_observable.subscribe() d.dispose() d.dispose() self.assertEqual(1, invasserte_count[0])
def _case(mapper, sources, default_source=None) -> Observable: default_source = default_source or empty() def factory(_) -> Observable: try: result = sources[mapper()] except KeyError: result = default_source result = from_future(result) if is_future(result) else result return result return defer(factory)
def window_toggle(source: Observable) -> Observable: def mapper(args): _, window = args return window return openings.pipe( ops.group_join( source, closing_mapper, lambda _: empty(), ), ops.map(mapper), )
def observable_window_with_openings(self, window_openings, window_closing_mapper): def mapper(args): _, window = args return window return window_openings.pipe( ops.group_join( self, window_closing_mapper, lambda _: empty(), ), ops.map(mapper), )
def _set_band_names(): band_names_stream = of(band_names) if band_names else get_band_names( credentials, image) return concat( progress( default_message='Setting band names...', message_key='tasks.retrieve.image_to_sepal.setting_band_names' ), band_names_stream.pipe( flat_map(lambda names: set_band_names( band_names=names, files= [destination_path + '/*.tif', destination_path + '/*.vrt']) ), flat_map(lambda _: empty())))
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 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 to_progress(state): if state == 'PENDING': return progress(default_message= 'Submitting export task to Google Earth Engine...', message_key='tasks.ee.export.pending') elif state == 'READY': return progress( default_message= 'Waiting for Google Earth Engine to start export...', message_key='tasks.ee.export.ready') elif state == 'RUNNING': return progress( default_message='Google Earth Engine is exporting...', message_key='tasks.ee.export.running') else: return empty()
def _export_and_download_dates(stack, export_description, year_dir): table_drive_description = 'dates_' + export_description dates_drive_folder = '{}-{}'.format(table_drive_description, str(uuid.uuid4())) dates_drive_path = '{}/{}'.format(drive_folder_path, dates_drive_folder) create_dates_drive_folder = create_folder_with_path( credentials, dates_drive_path).pipe(flat_map(lambda _: empty())) export_dates = _export_dates(stack.bandNames(), table_drive_description, dates_drive_folder) download_dates_from_drive = _download_from_drive( path=dates_drive_path, destination=year_dir).pipe( map(lambda p: {'dates_bytes': p.downloaded_bytes})) return concat(create_dates_drive_folder, export_dates, download_dates_from_drive)
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()
async def go(): nonlocal result source = rx.empty() result = await source
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(): return empty().pipe(_.debounce(10))
def mapper(_) -> Observable: return rx.empty()
def test_reduce_seed_none_does_not_crash(self): rx.empty().pipe( ops.reduce(lambda acc, v: v, seed=None) ).subscribe()
def test_as_observable_hides(self): some_observable = rx.empty() assert some_observable.pipe(ops.as_observable()) != some_observable
def test_run_empty(self): with pytest.raises(SequenceContainsNoElementsError): rx.empty().run()
def create(): return rx.empty().pipe(ops.timestamp())
def factory(): return empty()
def right_duration_mapper(y): if len(y.value) >= 0: raise Exception(ex) else: return rx.empty()
def left_duration_mapper(x): if x.value >= 0: raise Exception(ex) else: return rx.empty()
def create(): return rx.empty().pipe(ops.time_interval())