Exemplo n.º 1
0
def skip(self, count):
    """Bypasses a specified number of elements in an observable sequence
    and then returns the remaining elements.

    Keyword arguments:
    count -- The number of elements to skip before returning the remaining
        elements.

    Returns an observable sequence that contains the elements that occur
    after the specified index in the input sequence.
    """

    if count < 0:
        raise ArgumentOutOfRangeException()

    observable = self

    def subscribe(observer):
        remaining = [count]

        def on_next(value):
            if remaining[0] <= 0:
                observer.on_next(value)
            else:
                remaining[0] -= 1

        return observable.subscribe(on_next, observer.on_error, observer.on_completed)

    return AnonymousObservable(subscribe)
    def advance_to(self, time):
        """Advances the schedulers clock to the specified time, running all
        work til that point.

        Keyword arguments:
        time -- Absolute time to advance the schedulers clock to."""

        due_to_clock = self.comparer(self.clock, time)
        if due_to_clock > 0:
            raise ArgumentOutOfRangeException()

        if not due_to_clock:
            return

        if not self.is_enabled:
            self.is_enabled = True
            while self.is_enabled:
                next = self.get_next()
                if next and self.comparer(next.duetime, time) <= 0:
                    if self.comparer(next.duetime, self.clock) > 0:
                        self.clock = next.duetime

                    next.invoke()
                else:
                    self.is_enabled = False

            self.clock = time
Exemplo n.º 3
0
    def advance_to(self, time):
        """Advances the schedulers clock to the specified time, running all
        work til that point.

        Keyword arguments:
        time -- Absolute time to advance the schedulers clock to."""

        if self.clock > time:
            raise ArgumentOutOfRangeException()

        if self.clock == time:
            return

        if self.is_enabled:
            return

        self.is_enabled = True

        while self.is_enabled:
            next = self.get_next()
            if not next:
                break

            if next.duetime > time:
                self.queue.enqueue(next)
                break

            if next.duetime > self.clock:
                self.clock = next.duetime

            next.invoke()

        self.is_enabled = False
        self.clock = time
Exemplo n.º 4
0
def _skip(count: int) -> Callable[[Observable], Observable]:
    if count < 0:
        raise ArgumentOutOfRangeException()

    def skip(source: Observable) -> Observable:
        """The skip operator.

        Bypasses a specified number of elements in an observable sequence
        and then returns the remaining elements.

        Args:
            source: The source observable.

        Returns:
            An observable sequence that contains the elements that occur
            after the specified index in the input sequence.
        """
        def subscribe(observer, scheduler=None):
            remaining = count

            def on_next(value):
                nonlocal remaining

                if remaining <= 0:
                    observer.on_next(value)
                else:
                    remaining -= 1

            return source.subscribe_(on_next, observer.on_error,
                                     observer.on_completed, scheduler)

        return Observable(subscribe)

    return skip
Exemplo n.º 5
0
    def advance_to(self, time: float) -> None:
        """Advances the schedulers clock to the specified time,
        running all work til that point.

        Args:
            time: Absolute time to advance the schedulers clock to.
        """

        if self.clock > time:
            raise ArgumentOutOfRangeException()

        if self.clock == time:
            return

        if self.is_enabled:
            return

        self.is_enabled = True

        while self.is_enabled:
            item = self.get_next()
            if not item:
                break

            if item.duetime > time:
                self.queue.enqueue(item)
                break

            if item.duetime > self.clock:
                self.clock = item.duetime

            item.invoke()

        self.is_enabled = False
        self.clock = time
Exemplo n.º 6
0
def skip(count: int, source: ObservableBase) -> ObservableBase:
    """Bypasses a specified number of elements in an observable sequence
    and then returns the remaining elements.

    Keyword arguments:
    count -- The number of elements to skip before returning the remaining
        elements.

    Returns an observable sequence that contains the elements that occur
    after the specified index in the input sequence.
    """

    if count < 0:
        raise ArgumentOutOfRangeException()

    observable = source

    def subscribe(observer, scheduler=None):
        remaining = count

        def on_next(value):
            nonlocal remaining

            if remaining <= 0:
                observer.on_next(value)
            else:
                remaining -= 1

        return observable.subscribe_(on_next, observer.on_error,
                                     observer.on_completed, scheduler)

    return AnonymousObservable(subscribe)
Exemplo n.º 7
0
    def sleep(self, time):
        """Advances the schedulers clock by the specified relative time.

        Keyword arguments:
        time -- Relative time to advance the schedulers clock by."""

        dt = self.add(self.clock, time)

        if self.clock > dt:
            raise ArgumentOutOfRangeException()

        self.clock = dt
Exemplo n.º 8
0
    def advance_by(self, time):
        """Advances the schedulers clock by the specified relative time,
        running all work scheduled for that timespan.

        Keyword arguments:
        time -- Relative time to advance the schedulers clock by."""

        log.debug("VirtualTimeScheduler.advance_by(time=%s)", time)

        dt = self.add(self.clock, time)
        if self.clock > dt:
            raise ArgumentOutOfRangeException()
        return self.advance_to(dt)
Exemplo n.º 9
0
    def sleep(self, time: float) -> None:
        """Advances the schedulers clock by the specified relative time.

        Args:
            time: Relative time to advance the schedulers clock by.
        """

        dt = self.add(self.clock, time)

        if self.clock > dt:
            raise ArgumentOutOfRangeException()

        self.clock = dt
Exemplo n.º 10
0
    def advance_to(self, time: typing.AbsoluteTime) -> None:
        """Advances the schedulers clock to the specified absolute time,
        running all work til that point.

        Args:
            time: Absolute time to advance the schedulers clock to.
        """

        dt: datetime = self.to_datetime(time)
        with self._lock:
            if self.now > dt:
                raise ArgumentOutOfRangeException()

            if self.now == dt or self._is_enabled:
                return

            self._is_enabled = True

        while True:
            with self._lock:
                if not self._is_enabled or not self._queue:
                    break

                item: ScheduledItem = self._queue.peek()

                if item.duetime > dt:
                    break

                if item.duetime > self.now:
                    if isinstance(self._clock, datetime):
                        self._clock = item.duetime
                    else:
                        self._clock = self.to_seconds(item.duetime)

                self._queue.dequeue()

            if not item.is_cancelled():
                item.invoke()

        with self._lock:
            self._is_enabled = False
            if isinstance(self._clock, datetime):
                self._clock = dt
            else:
                self._clock = self.to_seconds(dt)
Exemplo n.º 11
0
    def sleep(self, time: typing.RelativeTime) -> None:
        """Advances the schedulers clock by the specified relative time.

        Args:
            time: Relative time to advance the schedulers clock by.
        """

        absolute = self.add(self.now, self.to_timedelta(time))
        dt: datetime = self.to_datetime(absolute)

        if self.now > dt:
            raise ArgumentOutOfRangeException()

        with self._lock:
            if isinstance(self._clock, datetime):
                self._clock = dt
            else:
                self._clock = self.to_seconds(dt)
Exemplo n.º 12
0
    def take(self, count, scheduler=None):
        """Returns a specified number of contiguous elements from the start of
        an observable sequence, using the specified scheduler for the edge case
        of take(0).
        
        1 - source.take(5)
        2 - source.take(0, rx.Scheduler.timeout)
        
        Keyword arguments:
        count -- The number of elements to return.
        scheduler -- [Optional] Scheduler used to produce an OnCompleted 
            message in case count is set to 0.

        Returns an observable sequence that contains the specified number of 
        elements from the start of the input sequence.
        """

        if count < 0:
            raise ArgumentOutOfRangeException()

        if not count:
            return Observable.Empty(scheduler)

        observable = self

        def subscribe(observer):
            remaining = count

            def on_next(value):
                nonlocal remaining

                if remaining > 0:
                    remaining -= 1
                    observer.on_next(value)
                    if not remaining:
                        observer.on_completed()

            return observable.subscribe(on_next, observer.on_error,
                                        observer.on_completed)

        return AnonymousObservable(subscribe)
Exemplo n.º 13
0
    def advance_to(self, time: float) -> None:
        """Advances the schedulers clock to the specified absolute time,
        running all work til that point.

        Args:
            time: Absolute time to advance the schedulers clock to.
        """

        with self._lock:
            if self._clock > time:
                raise ArgumentOutOfRangeException()

            if self._clock == time or self._is_enabled:
                return

            self._is_enabled = True

        while True:
            with self._lock:
                if not self._is_enabled or not self._queue:
                    break

                item: ScheduledItem[typing.TState] = self._queue.peek()

                if item.duetime > time:
                    break

                if item.duetime > self._clock:
                    self._clock = item.duetime

                self._queue.dequeue()

            if not item.is_cancelled():
                item.invoke()

        with self._lock:
            self._is_enabled = False
            self._clock = time
Exemplo n.º 14
0
def _take(count: int) -> Callable[[Observable], Observable]:
    if count < 0:
        raise ArgumentOutOfRangeException()

    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)

    return take