Example #1
0
def _publish(mapper=None) -> ConnectableObservable:
    """Returns an observable sequence that is the result of invoking the
    mapper on a connectable observable sequence that shares a single
    subscription to the underlying sequence. This operator is a
    specialization of Multicast using a regular Subject.

    Example:
        >>> res = publish()
        >>> res = publish(lambda x: x)

    mapper: [Optional] Selector function which can use the
        multicasted source sequence as many times as needed, without causing
        multiple subscriptions to the source sequence. Subscribers to the
        given source will receive all notifications of the source from the
        time of the subscription on.

    Returns:
        An observable sequence that contains the elements of a sequence
        produced by multicasting the source sequence within a mapper
        function.
    """

    if mapper:
        return pipe(ops.multicast(subject_factory=lambda _: Subject(), mapper=mapper))

    return pipe(ops.multicast(subject=Subject()))
Example #2
0
def _reduce(accumulator: Callable[[Any, Any], Any],
            seed: Any = NotSet) -> Callable[[Observable], Observable]:
    """Applies an accumulator function over an observable sequence,
    returning the result of the aggregation as a single element in the
    result sequence. The specified seed value is used as the initial
    accumulator value.

    For aggregation behavior with incremental intermediate results, see
    `scan()`.

    Examples:
        >>> res = reduce(lambda acc, x: acc + x)
        >>> res = reduce(lambda acc, x: acc + x, 0)

    Args:
        accumulator: An accumulator function to be
            invoked on each element.
        seed: Optional initial accumulator value.

    Returns:
        An operator function that takes an observable source and returns
        an observable sequence containing a single element with the
        final accumulator value.
    """

    if seed is not NotSet:
        initial = ops.start_with(seed)
        scanner = ops.scan(accumulator, seed=seed)

        return pipe(scanner, initial, ops.last())

    return pipe(ops.scan(accumulator), ops.last())
Example #3
0
def _publish(
    mapper: Optional[Mapper] = None
) -> Callable[[Observable], ConnectableObservable]:
    """Returns an observable sequence that is the result of invoking the
    mapper on a connectable observable sequence that shares a single
    subscription to the underlying sequence. This operator is a
    specialization of Multicast using a regular Subject.

    Example:
        >>> res = publish()
        >>> res = publish(lambda x: x)

    mapper: [Optional] Selector function which can use the
        multicasted source sequence as many times as needed, without causing
        multiple subscriptions to the source sequence. Subscribers to the
        given source will receive all notifications of the source from the
        time of the subscription on.

    Returns:
        An observable sequence that contains the elements of a sequence
        produced by multicasting the source sequence within a mapper
        function.
    """

    if mapper:
        return pipe(
            ops.multicast(subject_factory=lambda _: Subject(), mapper=mapper))

    return pipe(ops.multicast(subject=Subject()))
Example #4
0
def _reduce(accumulator: Callable[[Any, Any], Any], seed: Any = NotSet) -> Callable[[Observable], Observable]:
    """Applies an accumulator function over an observable sequence,
    returning the result of the aggregation as a single element in the
    result sequence. The specified seed value is used as the initial
    accumulator value.

    For aggregation behavior with incremental intermediate results, see
    `scan()`.

    Examples:
        >>> res = reduce(lambda acc, x: acc + x)
        >>> res = reduce(lambda acc, x: acc + x, 0)

    Args:
        accumulator: An accumulator function to be
            invoked on each element.
        seed: Optional initial accumulator value.

    Returns:
        An operator function that takes an observable source and returns
        an observable sequence containing a single element with the
        final accumulator value.
    """


    if seed is not NotSet:
        initial = ops.start_with(seed)
        scanner = ops.scan(accumulator, seed=seed)

        return pipe(scanner, initial, ops.last())

    return pipe(ops.scan(accumulator), ops.last())
Example #5
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)
Example #6
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)
Example #7
0
def _contains(value: Any, comparer=None) -> Callable[[Observable], Observable]:
    comparer_ = comparer or default_comparer

    filtering = ops.filter(lambda v: comparer_(v, value))
    something = ops.some()

    return pipe(filtering, something)
def _single_or_default(predicate: Predicate = None, default_value: Any = None) -> Observable:
    """Returns the only element of an observable sequence that matches
    the predicate, or a default value if no such element exists this
    method reports an exception if there is more than one element in the
    observable sequence.

    Examples:
        >>> res = single_or_default()
        >>> res = single_or_default(lambda x: x == 42)
        >>> res = single_or_default(lambda x: x == 42, 0)
        >>> res = single_or_default(None, 0)

    Args:
        predicate -- [Optional] A predicate function to evaluate for
            elements in the source sequence.
        default_value -- [Optional] The default value if the index is
            outside the bounds of the source sequence.

    Returns:
        An observable Sequence containing the single element in the
    observable sequence that satisfies the condition in the predicate,
    or a default value if no such element exists.
    """

    if predicate:
        return pipe(ops.filter(predicate), ops.single_or_default(None, default_value))
    else:
        return _single_or_default_async(True, default_value)
Example #9
0
def _first(
    predicate: Optional[Predicate] = None
) -> Callable[[Observable], Observable]:
    """Returns the first element of an observable sequence that
    satisfies the condition in the predicate if present else the first
    item in the sequence.

    Examples:
        >>> res = res = first()(source)
        >>> res = res = first(lambda x: x > 3)(source)

    Args:
        predicate -- [Optional] A predicate function to evaluate for
            elements in the source sequence.

    Returns:
        A function that takes an observable source and returns an
        observable sequence containing the first element in the
        observable sequence that satisfies the condition in the predicate if
        provided, else the first item in the sequence.
    """

    if predicate:
        return pipe(ops.filter(predicate), ops.first())

    return _first_or_default_async(False)
def _first_or_default(
        predicate: Optional[Predicate] = None,
        default_value: Any = None) -> Callable[[Observable], Observable]:
    """Returns the first element of an observable sequence that
    satisfies the condition in the predicate, or a default value if no
    such element exists.

    Examples:
        >>> res = source.first_or_default()
        >>> res = source.first_or_default(lambda x: x > 3)
        >>> res = source.first_or_default(lambda x: x > 3, 0)
        >>> res = source.first_or_default(None, 0)

    Args:
        source -- Observable sequence.
        predicate -- [optional] A predicate function to evaluate for
            elements in the source sequence.
        default_value -- [Optional] The default value if no such element
            exists.  If not specified, defaults to None.

    Returns:
        A function that takes an observable source and reutrn an
        observable sequence containing the first element in the
        observable sequence that satisfies the condition in the
        predicate, or a default value if no such element exists.
    """

    if predicate:
        return pipe(ops.filter(predicate),
                    ops.first_or_default(None, default_value))
    return _first_or_default_async(True, default_value)
Example #11
0
def _single_or_default(predicate: Optional[Predicate] = None,
                       default_value: Any = None) -> Observable:
    """Returns the only element of an observable sequence that matches
    the predicate, or a default value if no such element exists this
    method reports an exception if there is more than one element in the
    observable sequence.

    Examples:
        >>> res = single_or_default()
        >>> res = single_or_default(lambda x: x == 42)
        >>> res = single_or_default(lambda x: x == 42, 0)
        >>> res = single_or_default(None, 0)

    Args:
        predicate -- [Optional] A predicate function to evaluate for
            elements in the source sequence.
        default_value -- [Optional] The default value if the index is
            outside the bounds of the source sequence.

    Returns:
        An observable Sequence containing the single element in the
    observable sequence that satisfies the condition in the predicate,
    or a default value if no such element exists.
    """

    if predicate:
        return pipe(ops.filter(predicate),
                    ops.single_or_default(None, default_value))
    else:
        return _single_or_default_async(True, default_value)
def _first_or_default(predicate: Predicate = None, default_value: Any = None) -> Callable[[Observable], Observable]:
    """Returns the first element of an observable sequence that
    satisfies the condition in the predicate, or a default value if no
    such element exists.

    Examples:
        >>> res = source.first_or_default()
        >>> res = source.first_or_default(lambda x: x > 3)
        >>> res = source.first_or_default(lambda x: x > 3, 0)
        >>> res = source.first_or_default(None, 0)

    Args:
        source -- Observable sequence.
        predicate -- [optional] A predicate function to evaluate for
            elements in the source sequence.
        default_value -- [Optional] The default value if no such element
            exists.  If not specified, defaults to None.

    Returns:
        A function that takes an observable source and reutrn an
        observable sequence containing the first element in the
        observable sequence that satisfies the condition in the
        predicate, or a default value if no such element exists.
    """

    if predicate:
        return pipe(
            ops.filter(predicate),
            ops.first_or_default(None, default_value)
        )
    return _first_or_default_async(True, default_value)
Example #13
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)
Example #14
0
def _is_empty() -> Callable[[Observable], Observable]:
    """Determines whether an observable sequence is empty.

    Returns:
        An observable sequence containing a single element
        determining whether the source sequence is empty.
    """

    return pipe(ops.some(), ops.map(lambda b: not b))
def _buffer_with_time(timespan: typing.RelativeTime, timeshift: typing.RelativeTime = None,
                      scheduler: typing.Scheduler = None) -> Callable[[Observable], Observable]:
    if not timeshift:
        timeshift = timespan

    return pipe(
        ops.window_with_time(timespan, timeshift, scheduler),
        ops.flat_map(lambda x: x.pipe(ops.to_iterable()))
    )
def _buffer_with_time(
    timespan: typing.RelativeTime,
    timeshift: Optional[typing.RelativeTime] = None,
    scheduler: Optional[typing.Scheduler] = None
) -> Callable[[Observable], Observable]:
    if not timeshift:
        timeshift = timespan

    return pipe(ops.window_with_time(timespan, timeshift, scheduler),
                ops.flat_map(lambda x: x.pipe(ops.to_iterable())))
Example #17
0
File: map.py Project: wolf937/RxPY
def _map_indexed(mapper_indexed: MapperIndexed = None) -> Callable[[Observable], Observable]:
    def _identity(value: Any, index: int) -> Any:
        return value

    _mapper_indexed = mapper_indexed or _identity

    return pipe(
        ops.zip_with_iterable(infinite()),
        ops.starmap(_mapper_indexed)
    )
Example #18
0
def _buffer(buffer_openings=None,
            buffer_closing_mapper=None) -> Callable[[Observable], Observable]:
    """Projects each element of an observable sequence into zero or more
    buffers.

    Args:
        buffer_openings -- Observable sequence whose elements denote the
            creation of windows.
        buffer_closing_mapper -- [optional] A function invoked to define
            the closing of each produced window. If a closing mapper
            function is specified for the first parameter, this parameter is
            ignored.

    Returns:
        A function that takes an observable source and retuerns an
        observable sequence of windows.
    """

    return pipe(ops.window(buffer_openings, buffer_closing_mapper),
                ops.flat_map(pipe(ops.to_iterable(), ops.map(list))))
Example #19
0
def _all(predicate: Predicate) -> Callable[[Observable], Observable]:

    filtering = ops.filter(lambda v: not predicate(v))
    mapping = ops.map(lambda b: not b)
    some = ops.some()

    return pipe(
        filtering,
        some,
        mapping
    )
Example #20
0
def _share() -> Callable[[Observable], Observable]:
    """Share a single subscription among multple observers.

    Returns a new Observable that multicasts (shares) the original
    Observable. As long as there is at least one Subscriber this
    Observable will be subscribed and emitting data. When all
    subscribers have unsubscribed it will unsubscribe from the source
    Observable.

    This is an alias for a composed publish() and ref_count().
    """
    return pipe(_publish(), ops.ref_count())
Example #21
0
def _buffer(buffer_openings=None, buffer_closing_mapper=None) -> Callable[[Observable], Observable]:
    """Projects each element of an observable sequence into zero or more
    buffers.

    Args:
        buffer_openings -- Observable sequence whose elements denote the
            creation of windows.
        buffer_closing_mapper -- [optional] A function invoked to define
            the closing of each produced window. If a closing mapper
            function is specified for the first parameter, this parameter is
            ignored.

    Returns:
        A function that takes an observable source and retuerns an
        observable sequence of windows.
    """

    return pipe(
        ops.window(buffer_openings, buffer_closing_mapper),
        ops.flat_map(pipe(ops.to_iterable(), ops.map(list)))
    )
Example #22
0
def _share() -> Callable[[Observable], Observable]:
    """Share a single subscription among multple observers.

    Returns a new Observable that multicasts (shares) the original
    Observable. As long as there is at least one Subscriber this
    Observable will be subscribed and emitting data. When all
    subscribers have unsubscribed it will unsubscribe from the source
    Observable.

    This is an alias for a composed publish() and ref_count().
    """
    return pipe(_publish(), ops.ref_count())
Example #23
0
def intensity_steps(steps: List[Step], flushes):
    """

    Given an intensity value, return the list of steps
    which should be played back. The logic indexes higher
    based on the intensity value until it is reset by the final step

    :param steps:
    """

    return pipe(
        ops.flat_map(lambda event: rx.from_iterable(steps).pipe(
            ops.map(lambda step: (step, event)))),
        ops.filter(lambda x: x[0] <= x[1]), ops.map(lambda x: x[0]),
        ops.distinct(flushes=flushes))
Example #24
0
File: max.py Project: wolf937/RxPY
def _max(
    comparer: Callable[[Any],
                       bool] = None) -> Callable[[Observable], Observable]:
    """Returns the maximum value in an observable sequence according to
    the specified comparer.

    Examples:
        >>> op = max()
        >>> op = max(lambda x, y:  x.value - y.value)

    Args:
        comparer: [Optional] Comparer used to compare elements.

    Returns:
        An operator function that takes an observable source and returns
        an observable sequence containing a single element with the
        maximum element in the source sequence.
    """

    return pipe(ops.max_by(identity, comparer), ops.map(first_only))
Example #25
0
def _max(comparer: Optional[Comparer] = None) -> Callable[[Observable], Observable]:
    """Returns the maximum value in an observable sequence according to
    the specified comparer.

    Examples:
        >>> op = max()
        >>> op = max(lambda x, y:  x.value - y.value)

    Args:
        comparer: [Optional] Comparer used to compare elements.

    Returns:
        An operator function that takes an observable source and returns
        an observable sequence containing a single element with the
        maximum element in the source sequence.
    """

    return pipe(
        ops.max_by(identity, comparer),
        ops.map(first_only)
    )
Example #26
0
def _single(predicate: Predicate = None) -> Callable[[Observable], Observable]:
    """Returns the only element of an observable sequence that satisfies the
    condition in the optional predicate, and reports an exception if there
    is not exactly one element in the observable sequence.

    Example:
        >>> res = single()
        >>> res = single(lambda x: x == 42)

    Args:
        predicate -- [Optional] A predicate function to evaluate for
            elements in the source sequence.

    Returns:
        An observable sequence containing the single element in the
        observable sequence that satisfies the condition in the predicate.
    """

    if predicate:
        return pipe(ops.filter(predicate), ops.single())
    else:
        return ops.single_or_default_async(False)
Example #27
0
def _min(comparer: Callable = None) -> Callable[[Observable], Observable]:
    """The `min` operator.

    Returns the minimum element in an observable sequence according to
    the optional comparer else a default greater than less than check.

    Examples:
        >>> res = source.min()
        >>> res = source.min(lambda x, y: x.value - y.value)

    Args:
        comparer: [Optional] Comparer used to compare elements.

    Returns:
        An observable sequence containing a single element
        with the minimum element in the source sequence.
    """

    return pipe(
        ops.min_by(identity, comparer),
        ops.map(first_only)
    )
Example #28
0
def _first(predicate=None) -> Callable[[Observable], Observable]:
    """Returns the first element of an observable sequence that
    satisfies the condition in the predicate if present else the first
    item in the sequence.

    Examples:
        >>> res = res = first()(source)
        >>> res = res = first(lambda x: x > 3)(source)

    Args:
        predicate -- [Optional] A predicate function to evaluate for
            elements in the source sequence.

    Returns:
        A function that takes an observable source and returns an
        observable sequence containing the first element in the
        observable sequence that satisfies the condition in the predicate if
        provided, else the first item in the sequence.
    """

    if predicate:
        return pipe(ops.filter(predicate), ops.first())

    return _first_or_default_async(False)
Example #29
0
def _buffer_with_time_or_count(timespan, count, scheduler = None) -> Callable[[Observable], Observable]:
    return pipe(
        ops.window_with_time_or_count(timespan, count, scheduler),
        ops.flat_map(lambda x: x.pipe(ops.to_iterable()))
    )
Example #30
0
def _skip_while_indexed(
        predicate: typing.PredicateIndexed
) -> Callable[[Observable], Observable]:
    return pipe(ops.map_indexed(lambda x, i: (x, i)),
                ops.skip_while(lambda x: predicate(*x)),
                ops.map(lambda x: x[0]))
Example #31
0
def _buffer(boundaries: Observable) -> Callable[[Observable], Observable]:
    return pipe(ops.window(boundaries),
                ops.flat_map(pipe(ops.to_iterable(), ops.map(list))))
Example #32
0
def _buffer_toggle(
    openings: Observable, closing_mapper: Callable[[Any], Observable]
) -> Callable[[Observable], Observable]:
    return pipe(ops.window_toggle(openings, closing_mapper),
                ops.flat_map(pipe(ops.to_iterable(), ops.map(list))))
Example #33
0
def _buffer_when(
    closing_mapper: Callable[[], Observable]
) -> Callable[[Observable], Observable]:
    return pipe(ops.window_when(closing_mapper),
                ops.flat_map(pipe(ops.to_iterable(), ops.map(list))))
Example #34
0
def _skip_while_indexed(predicate: typing.PredicateIndexed) -> Callable[[Observable], Observable]:
    return pipe(
        ops.map_indexed(lambda x, i: (x, i)),
        ops.skip_while(lambda x: predicate(*x)),
        ops.map(lambda x: x[0])
    )