def _element_at_or_default(index, has_default=False, default_value=None):
    if index < 0:
        raise ArgumentOutOfRangeException()

    def element_at_or_default(source: Observable) -> Observable:
        def subscribe(observer, scheduler=None):
            i = [index]

            def on_next(x):
                found = False
                with source.lock:
                    if i[0]:
                        i[0] -= 1
                    else:
                        found = True

                if found:
                    observer.on_next(x)
                    observer.on_completed()

            def on_completed():
                if not has_default:
                    observer.on_error(ArgumentOutOfRangeException())
                else:
                    observer.on_next(default_value)
                    observer.on_completed()

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

        return Observable(subscribe)

    return element_at_or_default
def window_with_count(self, count, skip=None):
    """Projects each element of an observable sequence into zero or more
    windows which are produced based on element count information.

    1 - xs.window_with_count(10)
    2 - xs.window_with_count(10, 1)

    count -- Length of each window.
    skip -- [Optional] Number of elements to skip between creation of
        consecutive windows. If not specified, defaults to the count.

    Returns an observable sequence of windows.
    """

    source = self
    if count <= 0:
        raise ArgumentOutOfRangeException()

    if skip is None:
        skip = count

    if skip <= 0:
        raise ArgumentOutOfRangeException()

    def subscribe(observer):
        m = SingleAssignmentDisposable()
        refCountDisposable = RefCountDisposable(m)
        n = [0]
        q = []

        def create_window():
            s = Subject()
            q.append(s)
            observer.on_next(add_ref(s, refCountDisposable))

        create_window()

        def on_next(x):
            for item in q:
                item.on_next(x)

            c = n[0] - count + 1
            if c >= 0 and c % skip == 0:
                s = q.pop(0)
                s.on_completed()

            n[0] += 1
            if (n[0] % skip) == 0:
                create_window()

        def on_error(exception):
            while len(q):
                q.pop(0).on_error(exception)
            observer.on_error(exception)

        def on_completed():
            while len(q):
                q.pop(0).on_completed()
            observer.on_completed()

        m.disposable = source.subscribe(on_next, on_error, on_completed)
        return refCountDisposable

    return AnonymousObservable(subscribe)
 def on_completed():
     if not has_default:
         observer.on_error(ArgumentOutOfRangeException())
     else:
         observer.on_next(default_value)
         observer.on_completed()
Esempio n. 4
0
def _window_with_count(
        count: int,
        skip: Optional[int] = None) -> Callable[[Observable], Observable]:
    """Projects each element of an observable sequence into zero or more
    windows which are produced based on element count information.

    Examples:
        >>> window_with_count(10)
        >>> window_with_count(10, 1)

    Args:
        count: Length of each window.
        skip: [Optional] Number of elements to skip between creation of
            consecutive windows. If not specified, defaults to the
            count.

    Returns:
        An observable sequence of windows.
    """

    if count <= 0:
        raise ArgumentOutOfRangeException()

    if skip is None:
        skip = count

    if skip <= 0:
        raise ArgumentOutOfRangeException()

    def window_with_count(source: Observable) -> Observable:
        def subscribe(observer, scheduler=None):
            m = SingleAssignmentDisposable()
            refCountDisposable = RefCountDisposable(m)
            n = [0]
            q = []

            def create_window():
                s = Subject()
                q.append(s)
                observer.on_next(add_ref(s, refCountDisposable))

            create_window()

            def on_next(x):
                for item in q:
                    item.on_next(x)

                c = n[0] - count + 1
                if c >= 0 and c % skip == 0:
                    s = q.pop(0)
                    s.on_completed()

                n[0] += 1
                if (n[0] % skip) == 0:
                    create_window()

            def on_error(exception):
                while q:
                    q.pop(0).on_error(exception)
                observer.on_error(exception)

            def on_completed():
                while q:
                    q.pop(0).on_completed()
                observer.on_completed()

            m.disposable = source.subscribe_(on_next, on_error, on_completed,
                                             scheduler)
            return refCountDisposable

        return Observable(subscribe)

    return window_with_count