def to_rx(self, *inputs): if len(inputs) == 0: raise ValueError if len(inputs) == 1: observable = inputs[0] else: observable = rx.zip(*inputs) return observable
def create_folder_with_path(credentials, path: str): def create_if_missing(parent, name_file): name = name_file[0] file = name_file[1] if file: return of(file) else: return create_folder(credentials, parent, name) return zip( _path_elements(path), _files_in_path(credentials, path), ).pipe(merge_scan(create_if_missing, _root_folder), last())
def zip(source: Observable) -> Observable: """Merges the specified observable sequences into one observable sequence by creating a tuple whenever all of the observable sequences have produced an element at a corresponding index. Example: >>> res = zip(source) Args: source: Source observable to zip. Returns: An observable sequence containing the result of combining elements of the sources as a tuple. """ return rx.zip(source, *args)
#A_check_stream = operation_maneuver.pipe( ops.map(天梁A), ops.do_action(决) ).subscribe(subject_dict['A']) operation_maneuver.pipe( ops.map(天梁B), ops.do_action(决) ).subscribe(subject_dict['B']) #C_trigger_stream.subscribe(rx.zip(*[A_check_stream, B_check_stream])) #临时的流不需要保存 rx.zip(*[subject_dict['A'], subject_dict['B']]).pipe( ops.map(天梁C), ops.do_action(决) ).subscribe(subject_dict['C']) #C之后增加D subject_dict['C'].pipe( ops.map(天梁D), ops.do_action(决) ).subscribe(subject_dict['D']) records_stream = Subject() #后面再次统一处理 过滤掉不变N的记录 rx.merge(*(subject_dict.values())).pipe( ops.filter(lambda x: x['kind'] != 'N'), ).subscribe(records_stream)
ops.map(lambda s: s[0])) isHealthyStream = statusStream.pipe( ops.map(lambda s: s.status.upper() in (StatusType.NORMAL, StatusType.BUSY))) assetStream = assetsAPI.pipe( ops.map(lambda res: list(filter(lambda x: x.asset == 'jpy', res.assets))), ops.filter(bool), ops.map(lambda a: a[0])) hasEnoughCollateralStream = assetStream.pipe( ops.map(lambda a: float(a.locked_amount) / float(a.onhand_amount)), ops.map(lambda use_rate: use_rate < 0.5),) canOrderStream = rx.zip( isHealthyStream, hasEnoughCollateralStream).pipe( ops.map(all),) PriceLastBuyStream = transactionsStream.pipe( ops.map(lambda res: res.transactions), ops.map(lambda ts: list(filter(lambda x: x.side == 'buy', ts))), ops.filter(bool), ops.map(lambda ts: sorted(ts, key=lambda x: x.executed_at)), ops.map(lambda ts: ts[-1].price),) PriceLastSellStream = transactionsStream.pipe( ops.map(lambda res: res.transactions), ops.map(lambda ts: list(filter(lambda x: x.side == 'sell', ts))), ops.filter(bool), ops.map(lambda ts: sorted(ts, key=lambda x: x.executed_at)), ops.map(lambda ts: ts[-1].price),)
def _zip_observables(*inputs: rx.Observable) -> rx.Observable: if len(inputs) == 0: raise ValueError("Requires at least one input.") if len(inputs) == 1: return inputs[0].pipe(ops.map(lambda x: (x, ))) return rx.zip(*inputs)
# flat 하면 리스트를 푼다. # [[1,2], [3]] -> [1,2,3] # 그리고 나서 map 한다. x -> x # [1, 2, 3] -> [1, 2, 3] print('-- window_with_time') test_scheduler = TestScheduler() rx.interval(0.05, test_scheduler).pipe(ops.take_until( rx.timer(0.1)), ops.window_with_time(0.01)).subscribe( lambda observable: observable.pipe(ops.count()).subscribe(print_value)) test_scheduler.start() time.sleep(0.5) # 결과가 이게 맞나?? 단위가 뭐지? print('-- combine_latest') rx.combine_latest( rx.interval(0.1).pipe(ops.map(lambda x: 'a- {}'.format(x))), rx.interval(0.2).pipe(ops.map(lambda x: 'b- {}'.format(x)))).pipe( ops.take_until(rx.timer(0.5))).subscribe(print_value) time.sleep(1) # 누가 먼저 결과를 배출할지 예상 불가 print('-- zip') rx.zip( rx.interval(0.1).pipe(ops.map(lambda x: 'a- {}'.format(x))), rx.interval(0.2).pipe(ops.map(lambda x: 'b- {}'.format(x)))).pipe( ops.take_until(rx.timer(1))).subscribe(print_value) time.sleep(1.2) # zip 은 양쪽 모두 값이 있어야 된다
from rx import from_, zip, interval letters = from_(["Alpha", "Beta", "Gamma", "Delta", "Epsilon"]) intervals = interval(1) # The code below includes l_n tuple unpacking (Python 3 doesn't support that out of the box, hence l_i[0]) zip(letters, intervals).subscribe(lambda l_i: print(l_i[0])) input("Press any key to quit\n")