Пример #1
0
    def slice(source: Observable) -> Observable:
        """The partially applied slice operator.

        Slices the given observable. It is basically a wrapper around the operators
        :func:`skip <rx.operators.skip>`,
        :func:`skip_last <rx.operators.skip_last>`,
        :func:`take <rx.operators.take>`,
        :func:`take_last <rx.operators.take_last>` and
        :func:`filter <rx.operators.filter>`.

        The following diagram helps you remember how slices works with streams.
        Positive numbers are relative to the start of the events, while negative
        numbers are relative to the end (close) of the stream.

        .. code::

            r---e---a---c---t---i---v---e---!
            0   1   2   3   4   5   6   7   8
           -8  -7  -6  -5  -4  -3  -2  -1   0

        Examples:
            >>> result = source.slice(1, 10)
            >>> result = source.slice(1, -2)
            >>> result = source.slice(1, -1, 2)

        Args:
            source: Observable to slice

        Returns:
            A sliced observable sequence.
        """

        if has_stop and _stop >= 0:
            pipeline.append(ops.take(_stop))

        if has_start and _start > 0:
            pipeline.append(ops.skip(_start))

        if has_start and _start < 0:
            pipeline.append(ops.take_last(abs(_start)))

        if has_stop and _stop < 0:
            pipeline.append(ops.skip_last(abs(_stop)))

        if has_step:
            if _step > 1:
                pipeline.append(
                    ops.filter_indexed(lambda x, i: i % _step == 0))
            elif _step < 0:
                # Reversing events is not supported
                raise TypeError('Negative step not supported.')

        return source.pipe(*pipeline)
Пример #2
0
    def slice(source: Observable) -> Observable:
        """The partially applied slice operator.

        Slices the given observable. It is basically a wrapper around the
        operators skip(), skip_last(), take(), take_last() and filter().

        This marble diagram helps you remember how slices works with streams.
        Positive numbers is relative to the start of the events, while negative
        numbers are relative to the end (close) of the stream.

         r---e---a---c---t---i---v---e---|
         0   1   2   3   4   5   6   7   8
        -8  -7  -6  -5  -4  -3  -2  -1   0

        Examples:
            >>> result = source.slice(1, 10)
            >>> result = source.slice(1, -2)
            >>> result = source.slice(1, -1, 2)

        Args:
            source: Observable to slice

        Returns:
            A sliced observable sequence.
        """

        if has_stop and stop >= 0:
            pipeline.append(ops.take(stop))

        if has_start and start > 0:
            pipeline.append(ops.skip(start))

        if has_start and start < 0:
            pipeline.append(ops.take_last(abs(start)))

        if has_stop and stop < 0:
            pipeline.append(ops.skip_last(abs(stop)))

        if has_step:
            if step > 1:
                pipeline.append(ops.filter_indexed(lambda x, i: i % step == 0))
            elif step < 0:
                # Reversing events is not supported
                raise TypeError("Negative step not supported.")

        return source.pipe(*pipeline)
Пример #3
0
    def slice(source: Observable) -> Observable:
        """The partially applied slice operator.

        Slices the given observable. It is basically a wrapper around the
        operators skip(), skip_last(), take(), take_last() and filter().

        This marble diagram helps you remember how slices works with streams.
        Positive numbers is relative to the start of the events, while negative
        numbers are relative to the end (close) of the stream.

         r---e---a---c---t---i---v---e---|
         0   1   2   3   4   5   6   7   8
        -8  -7  -6  -5  -4  -3  -2  -1   0

        Examples:
            >>> result = source.slice(1, 10)
            >>> result = source.slice(1, -2)
            >>> result = source.slice(1, -1, 2)

        Args:
            source: Observable to slice

        Returns:
            A sliced observable sequence.
        """

        if has_stop and stop >= 0:
            pipeline.append(ops.take(stop))

        if has_start and start > 0:
            pipeline.append(ops.skip(start))

        if has_start and start < 0:
            pipeline.append(ops.take_last(abs(start)))

        if has_stop and stop < 0:
            pipeline.append(ops.skip_last(abs(stop)))

        if has_step:
            if step > 1:
                pipeline.append(ops.filter_indexed(lambda x, i: i % step == 0))
            elif step < 0:
                # Reversing events is not supported
                raise TypeError("Negative step not supported.")

        return source.pipe(*pipeline)
import rx
import rx.operators as ops

numbers = rx.from_([1, 2, 3, 4, 5, 6])
numbers.pipe(ops.take_last(2)).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"))
op.map()
op.scan()
# ...

"""Filtering"""
op.debounce()
op.distinct()
op.filter()
op.element_at()
op.first()
op.ignore_elements()
op.last()
op.skip()
op.skip_last()
op.take()
op.take_last()
# ...

"""Error Handling"""
op.catch()
op.retry()

"""Utility"""
op.delay()
op.materialize()
op.time_interval()
op.timeout()
op.timestamp()

"""Conditional and Boolean"""
op.all()
Пример #6
0
 def create():
     return xs.pipe(ops.take_last(1))
Пример #7
0
    op.map(print),
).run()

# takeした場合はtake分だけしか呼ばれない
print('call take !!!!!!!!!!')
rx.from_iterable((print('generator call', i) or i for i in range(10))).pipe(
    op.take(1),
    op.map(lambda x: print('map call', x) or x),
    op.buffer_with_count(2),
    op.map(print),
).run()

# take lastした場合はもちろん最後まで呼ばれる
print('call take2 !!!!!!!!!!')
rx.from_iterable((print('generator call', i) or i for i in range(10))).pipe(
    op.take_last(2),
    op.map(lambda x: print('map call', x) or x),
    op.buffer_with_count(2),
    op.map(print),
).run()

# 3こためて先頭の2こを処理
print('call take3 !!!!!!!!!!')
rx.from_iterable((print('generator call', i) or i for i in range(10))).pipe(
    op.window_with_count(3),
    op.flat_map(lambda x: x.pipe(op.take(2))),
    op.map(print),
).run()

# delayはobservableを引数に取りobservableを返す関数
# 引数にobservable以外を渡しても関数は通るが、呼び出すときにエラーになる