Пример #1
0
def collect(messages: Observable[IbApiMessage]) -> Observable[List[Position]]:
    return messages.pipe(
        _.filter(lambda m: _is_position(m) or _is_position_end(m)),
        _.take_while(lambda m: not _is_position_end(m)),
        _.map(_unpack_position),
        _.reduce(lambda positions, position: [*positions, position], []),
    )
Пример #2
0
def _count(predicate: Predicate = None) -> Callable[[Observable], Observable]:

    if predicate:
        filtering = ops.filter(predicate)
        return pipe(filtering, ops.count())

    counter = ops.reduce(lambda n, _: n + 1, seed=0)
    return pipe(counter)
Пример #3
0
def _count(predicate: Optional[Predicate] = None) -> Callable[[Observable], Observable]:

    if predicate:
        filtering = ops.filter(predicate)
        return pipe(filtering, ops.count())

    counter = ops.reduce(lambda n, _: n + 1, seed=0)
    return pipe(counter)
Пример #4
0
def _sum(key_mapper: Optional[Mapper] = None) -> Callable[[Observable], Observable]:
    if key_mapper:
        return pipe(
            ops.map(key_mapper),
            ops.sum()
        )

    return ops.reduce(seed=0, accumulator=lambda prev, curr: prev + curr)
Пример #5
0
def collect(messages: Observable[IbApiMessage],
            request_id: int) -> Observable[AccountSummary]:
    return messages.pipe(
        _.filter(
            lambda m: _is_account_summary(m) or _is_account_summary_end(m)),
        _.filter(lambda m: _request_id(m) == request_id),
        _.take_while(lambda m: not _is_account_summary_end(m)),
        _.map(_unpack_account_summary),
        _.reduce(lambda summary, data: _add_data_to_summary(data, summary),
                 AccountSummary()))
Пример #6
0
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())))
Пример #7
0
def collect(
        messages: Observable[IbApiMessage], request_id: int,
        data_type: types.HistoricalDataType) -> Observable[List[types.BarData]]:
    return messages.pipe(
        _.filter(
            lambda m: _is_historical_data(m) or _is_historical_data_end(m)),
        _.filter(lambda m: _request_id(m) == request_id),
        _.take_while(lambda m: not _is_historical_data_end(m)),
        _.map(lambda m: _unpack_historical_data(m, data_type)),
        _.reduce(lambda bars, bar: [*bars, bar], []),
    )
Пример #8
0
def list_folder_recursively(folder: dict) -> Observable:
    def recurse(file):
        if is_folder(file):
            return concat(
                of([file]),
                list_folder_recursively(file),
            )
        else:
            return of([file])

    return list_folder(folder).pipe(
        flat_map(lambda files: from_list(files)),
        flat_map(recurse),
        reduce(lambda acc, files: acc + files, []),
    )
Пример #9
0
def file_by_path(path: str) -> Observable:
    root_stream = of({'id': 'root', 'path': '/'})

    def find_with_parent(parent_stream, name):
        return parent_stream.pipe(
            flat_map(lambda parent: list_folder(parent, name_filter=name)),
            map(lambda files: files[0] if len(files) else None),
            flat_map(lambda file: of(file) if file else throw(
                Exception('File {} does not exist.'.format(path)))))

    return from_list(path.split('/')).pipe(
        filter(lambda name: name and name.strip()
               ),  # Allows double // and training /
        reduce(find_with_parent, root_stream),
        flat_map(lambda file_stream: file_stream.pipe(map(lambda file: file))),
        first())
Пример #10
0
def list_folder_recursively(
        credentials,
        folder: dict,
        retries: int = 5
) -> Observable:
    def recurse(file):
        if is_folder(file):
            return concat(
                of([file]),
                list_folder_recursively(credentials, file, retries),
            )
        else:
            return of([file])

    return list_folder(credentials, folder, retries=retries).pipe(
        flat_map(lambda files: from_list(files)),
        flat_map(recurse),
        reduce(lambda acc, files: acc + files, []),
    )
Пример #11
0
    def test_type(self):
        def reduce_to_list(dst: Iterable[int], src: int) -> Iterable:
            return (*dst, src)

        store = create_store()
        store.add_feature_module(create_counter_feature())

        result = BehaviorSubject(None)

        store_: Observable[ReduxRootStore] = store.as_observable()
        store_.pipe(operators.map(select_counter_feature),
                    operators.reduce(reduce_to_list, ()),
                    operators.first()).subscribe(result)

        store.dispatch(INCREMENT_ACTION)
        store.dispatch(DECREMENT_ACTION)

        store.on_completed()

        assert result.value == (0, 1, 0)
Пример #12
0
def general_node_grouper(observable: rx.Observable):
    return observable.pipe(
        op.map(lambda dic: raw_node_dict_to_formatted_node_dict(dic)),
        op.reduce(lambda acc, act: reduce_node_dict(acc, act)),
        op.map(lambda dic: node_dict_to_node_dict_with_list(dic)),
    )
Пример #13
0
import rx
from rx import operators as ops
from random import randint

three_emissions = rx.range(0, 3)

three_emissions_ints = three_emissions.pipe(
    ops.map(lambda i: randint(1, 10000)), ops.publish())

three_emissions_ints.subscribe(lambda s: print("Subscriber 1: {0}".format(s)))
three_emissions_ints.pipe(
    ops.reduce(lambda total, item: total + item)).subscribe(
        lambda s: print("Subscriber 2: {0}".format(s)))

three_emissions_ints.connect()

# Subscriber 1과 2는 같은 랜덤 데이타에 대해 다른 작업 결과를 만들어 낼 수 있다.
Пример #14
0
def general_edge_grouper(observable: rx.Observable):
    return observable.pipe(
        op.map(raw_edge_dict_to_formatted_edge_dict),
        op.reduce(reduce_edge_dict),
        op.map(edge_dict_to_edge_dict_with_list),
    )
Пример #15
0
 def test_reduce_seed_none_does_not_crash(self):
     rx.empty().pipe(ops.reduce(lambda acc, v: v, seed=None)).subscribe()
from rx import from_, operators as ops

from_([4, 76, 22, 66, 881, 13,
       35]).pipe(ops.filter(lambda i: i < 100),
                 ops.reduce(lambda total, value: total + value)).subscribe(
                     lambda s: print(s))
Пример #17
0
 def test_reduce_seed_none_does_not_crash(self):
     rx.empty().pipe(
         ops.reduce(lambda acc, v: v, seed=None)
     ).subscribe()
Пример #18
0
 def create():
     return xs.pipe(ops.reduce(accumulator=lambda acc, x: acc + x))
    accumulator_func: A function that is used on the values coming from the source observable,
    and it returns the accumulated values in the form of an observable.

    seed: optional. the default value is not set. it is the initial value,
    to be used inside the accumulator function

:return
    it returns an observable, with a single value as output from
    the accumulator function applied on each value of the source observable.
"""

from rx import of, operators as op

of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\
    .pipe(
    op.reduce(lambda acc, x: acc + x)
    # acc: 1, x: 2 -> 3
    # acc: 3, x: 3 -> 6
    # acc: 6, x: 4 -> 10
    # acc: 10, x: 5 -> 15
    # acc: 15, x: 6 -> 21
    # acc: 21, x: 7 -> 28
    # acc: 28, x: 8 -> 36
    # acc: 36, x: 9 -> 45
    # acc: 45, x: 10 -> 55
).subscribe(lambda x: print("value is {}".format(x)))
# result
# value is 55


of("my", "body", "is", "string", "flux")\
Пример #20
0
 def create():
     return xs.pipe(ops.reduce(lambda acc, x: acc + x, 42))
Пример #21
0
 def create():
     return xs.pipe(ops.reduce(accumulator=lambda acc, x: acc + x))
from rx import range, operators as op

test = range(1, 11)

sub1 = test.pipe(
    op.filter(lambda s: s % 2 == 0), # next x -> 2, 4, 6, 8, 10
    op.reduce(lambda acc, x: acc + x, 3)
    # acc: 3, x: 2 -> 5
    # acc: 5, x: 4 -> 9
    # acc: 9, x: 6 -> 15
    # acc: 15, x: 8 -> 23
    # acc: 23, x: 10 -> 33
)

sub1.subscribe(lambda x: print("This value {}".format(x)))
rx.interval()
rx.from_()
rx.interval()
rx.just()
rx.range()
rx.repeat_value()
rx.start()
rx.timer()

"""Mathematical"""
op.average()
op.concat()
op.count()
op.max()
op.min()
op.reduce()
op.sum()

"""Transformation"""
op.buffer()
op.group_by()
op.map()
op.scan()
# ...

"""Filtering"""
op.debounce()
op.distinct()
op.filter()
op.element_at()
op.first()
Пример #24
0
def extract_word(word, filter_word):
    return rx.from_(word).pipe(
        ops.filter(lambda tup: tup[1] == filter_word and len(tup[0]) > 1),
        ops.map(lambda tup: tup[0]),
        ops.reduce(lambda acc, t1: acc + " " + t1))
Пример #25
0
 def create():
     return xs.pipe(ops.reduce(lambda acc, x: acc + x, 42))
import rx
import rx.operators as ops

numbers = rx.from_([1, 2, 3, 4])

numbers.pipe(ops.reduce(lambda acc, i: acc + i, seed=0)).subscribe(
    on_next=lambda i: print("on_next {}".format(i)),
    on_error=lambda e: print("on_error: {}".format(e)),
    on_completed=lambda: print("on_completed"))
Пример #27
0
import rx
from rx import operators as op
from rx.subject import Subject
import datetime

print('1-100求和')
rx.range(1, 101).pipe(
    op.reduce(lambda acc, i: acc + i, 0)
).subscribe(lambda i: print(i))

# 操作数据流
print('求所有偶数')
some_data = rx.of(1, 2, 3, 4, 5, 6, 7, 8)
some_data2 = rx.from_iterable(range(10, 20))
some_data.pipe(
    op.merge(some_data2),
    op.filter(lambda i: i % 2 == 0),
    # op.map(lambda i: i * 2)
).subscribe(lambda i: print(i))

# debounce操作符,仅在时间间隔之外的可以发射
print('防止重复发送')
ob = Subject()
ob.pipe(
    op.throttle_first(3)
    # op.debounce(3)
).subscribe(
    on_next=lambda i: print(i),
    on_completed=lambda: print('Completed')
)