当前位置: 首页>>代码示例>>Python>>正文


Python Observable.throw_exception方法代码示例

本文整理汇总了Python中rx.core.Observable.throw_exception方法的典型用法代码示例。如果您正苦于以下问题:Python Observable.throw_exception方法的具体用法?Python Observable.throw_exception怎么用?Python Observable.throw_exception使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rx.core.Observable的用法示例。


在下文中一共展示了Observable.throw_exception方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_repeat_observable_repeat_count_throws

# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import throw_exception [as 别名]
    def test_repeat_observable_repeat_count_throws(self):
        scheduler1 = TestScheduler()
        xs = Observable.return_value(1, scheduler1).repeat(3)
        xs.subscribe(lambda x: _raise('ex'))

        with self.assertRaises(RxException):
            scheduler1.start()

        scheduler2 = TestScheduler()
        ys = Observable.throw_exception('ex1', scheduler2).repeat(3)
        ys.subscribe(on_error=lambda ex: _raise('ex2'))

        with self.assertRaises(RxException):
            scheduler2.start()

        scheduler3 = TestScheduler()
        zs = Observable.return_value(1, scheduler3).repeat(100)
        d = zs.subscribe(on_completed=lambda: _raise('ex3'))

        scheduler3.schedule_absolute(10, lambda sc, st: d.dispose())
        scheduler3.start()

        xss = Observable.create(lambda o: _raise('ex4')).repeat(3)
        with self.assertRaises(RxException):
            xss.subscribe()
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:27,代码来源:test_repeat.py

示例2: subscribe

# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import throw_exception [as 别名]
    def subscribe(observer):
        try:
            result = observable_factory()
        except Exception as ex:
            return Observable.throw_exception(ex).subscribe(observer)

        result = Observable.from_future(result)
        return result.subscribe(observer)
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:10,代码来源:defer.py

示例3: subscribe

# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import throw_exception [as 别名]
    def subscribe(observer):
        disposable = Disposable.empty()
        try:
            resource = resource_factory()
            if resource:
                disposable = resource

            source = observable_factory(resource)
        except Exception as exception:
            d = Observable.throw_exception(exception).subscribe(observer)
            return CompositeDisposable(d, disposable)

        return CompositeDisposable(source.subscribe(observer), disposable)
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:15,代码来源:using.py

示例4: test_select_throws

# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import throw_exception [as 别名]
    def test_select_throws(self):
        with self.assertRaises(RxException):
            Observable.return_value(1) \
                .map(lambda x, y: x) \
                .subscribe(lambda x: _raise("ex"))

        with self.assertRaises(RxException):
            Observable.throw_exception('ex') \
                .map(lambda x, y: x) \
                .subscribe(on_error=lambda ex: _raise(ex))

        with self.assertRaises(RxException):
            Observable.empty() \
                .map(lambda x, y: x) \
                .subscribe(lambda x: x, lambda ex: ex, lambda: _raise('ex'))

        def subscribe(observer):
            _raise('ex')

        with self.assertRaises(RxException):
            Observable.create(subscribe) \
                .map(lambda x: x) \
                .subscribe()
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:25,代码来源:test_select.py

示例5: go

# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import throw_exception [as 别名]
        def go():
            error = Exception('woops')

            source = Observable.throw_exception(error)
            future = source.to_future(asyncio.Future)

            def done(future):
                try:
                    future.result()
                except Exception as ex:
                    success[1] = str(ex) == str(error)
                else:
                    success[0] = False

            future.add_done_callback(done)
开发者ID:riccardomarotti,项目名称:RxPY,代码行数:17,代码来源:py2_3_tofuture.py

示例6: test_retry_observable_throws

# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import throw_exception [as 别名]
    def test_retry_observable_throws(self):
        scheduler1 = TestScheduler()
        xs = Observable.return_value(1, scheduler1).retry()
        xs.subscribe(lambda x: _raise('ex'))

        self.assertRaises(RxException, scheduler1.start)

        scheduler2 = TestScheduler()
        ys = Observable.throw_exception('ex', scheduler2).retry()
        d = ys.subscribe(on_error=lambda ex: _raise('ex'))

        scheduler2.schedule_absolute(210, lambda sc, st: d.dispose())
        scheduler2.start()

        scheduler3 = TestScheduler()
        zs = Observable.return_value(1, scheduler3).retry()
        zs.subscribe(on_completed=lambda: _raise('ex'))

        self.assertRaises(RxException, scheduler3.start)
开发者ID:JohnWowUs,项目名称:RxPY,代码行数:21,代码来源:test_retry.py

示例7: test_select_with_index_throws

# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import throw_exception [as 别名]
    def test_select_with_index_throws(self):
        with self.assertRaises(RxException):
            return Observable.return_value(1) \
                .map(lambda x, index: x) \
                .subscribe(lambda x: _raise('ex'))

        with self.assertRaises(RxException):
            return Observable.throw_exception('ex') \
                .map(lambda x, index: x) \
                .subscribe(lambda x: x, lambda ex: _raise(ex))

        with self.assertRaises(RxException):
            return Observable.empty() \
                .map(lambda x, index: x) \
                .subscribe(lambda x: x, lambda ex: _, lambda : _raise('ex'))

        with self.assertRaises(RxException):
            return Observable.create(lambda o: _raise('ex')) \
                .map(lambda x, index: x) \
                .subscribe()
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:22,代码来源:test_select.py

示例8: timeout

# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import throw_exception [as 别名]
def timeout(self, duetime, other=None, scheduler=None):
    """Returns the source observable sequence or the other observable
    sequence if duetime elapses.

    1 - res = source.timeout(new Date()); # As a date
    2 - res = source.timeout(5000); # 5 seconds
    # As a date and timeout observable
    3 - res = source.timeout(datetime(), rx.Observable.return_value(42))
    # 5 seconds and timeout observable
    4 - res = source.timeout(5000, rx.Observable.return_value(42))
    # As a date and timeout observable
    5 - res = source.timeout(datetime(), rx.Observable.return_value(42),
                             rx.Scheduler.timeout)
    # 5 seconds and timeout observable
    6 - res = source.timeout(5000, rx.Observable.return_value(42),
                             rx.Scheduler.timeout)

    Keyword arguments:
    :param datetime|int duetime: Absolute (specified as a datetime object) or
        relative time (specified as an integer denoting milliseconds) when a
        timeout occurs.
    :param Observable other: Sequence to return in case of a timeout. If not
        specified, a timeout error throwing sequence will be used.
    :param Scheduler scheduler: Scheduler to run the timeout timers on. If not
        specified, the timeout scheduler is used.

    :returns: The source sequence switching to the other sequence in case of
        a timeout.
    :rtype: Observable
    """

    scheduler_method = None
    source = self

    other = other or Observable.throw_exception(Exception("Timeout"))
    other = Observable.from_future(other)

    scheduler = scheduler or timeout_scheduler

    if isinstance(duetime, datetime):
        scheduler_method = scheduler.schedule_absolute
    else:
        scheduler_method = scheduler.schedule_relative

    def subscribe(observer):
        switched = [False]
        _id = [0]

        original = SingleAssignmentDisposable()
        subscription = SerialDisposable()
        timer = SerialDisposable()
        subscription.disposable = original

        def create_timer():
            my_id = _id[0]

            def action(scheduler, state=None):
                switched[0] = (_id[0] == my_id)
                timer_wins = switched[0]
                if timer_wins:

                    subscription.disposable = other.subscribe(observer)

            timer.disposable = scheduler_method(duetime, action)

        create_timer()
        def on_next(x):
            on_next_wins = not switched[0]
            if on_next_wins:
                _id[0] += 1
                observer.on_next(x)
                create_timer()

        def on_error(e):
            on_error_wins = not switched[0]
            if on_error_wins:
                _id[0] += 1
                observer.on_error(e)

        def on_completed():
            on_completed_wins = not switched[0]
            if on_completed_wins:
                _id[0] += 1
                observer.on_completed()

        original.disposable = source.subscribe(on_next, on_error, on_completed)
        return CompositeDisposable(subscription, timer)
    return AnonymousObservable(subscribe)
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:90,代码来源:timeout.py

示例9: create

# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import throw_exception [as 别名]
 def create():
     return Observable.throw_exception(ex, scheduler).time_interval(scheduler)
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:4,代码来源:test_timeinterval.py

示例10: on_error

# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import throw_exception [as 别名]
 def on_error(self, e):
     self.on_next(Observable.throw_exception(e))
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:4,代码来源:manyselect.py

示例11: timeout_with_selector

# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import throw_exception [as 别名]
def timeout_with_selector(self, first_timeout=None,
                          timeout_duration_selector=None, other=None):
    """Returns the source observable sequence, switching to the other
    observable sequence if a timeout is signaled.

    1 - res = source.timeout_with_selector(rx.Observable.timer(500))
    2 - res = source.timeout_with_selector(rx.Observable.timer(500),
                lambda x: rx.Observable.timer(200))
    3 - res = source.timeout_with_selector(rx.Observable.timer(500),
                lambda x: rx.Observable.timer(200)),
                rx.Observable.return_value(42))

    first_timeout -- [Optional] Observable sequence that represents the
        timeout for the first element. If not provided, this defaults to
        Observable.never().
    timeout_Duration_selector -- [Optional] Selector to retrieve an
        observable sequence that represents the timeout between the current
        element and the next element.
    other -- [Optional] Sequence to return in case of a timeout. If not
        provided, this is set to Observable.throw_exception().

    Returns the source sequence switching to the other sequence in case of
    a timeout.
    """

    first_timeout = first_timeout or Observable.never()
    other = other or Observable.throw_exception(Exception('Timeout'))
    source = self

    def subscribe(observer):
        subscription = SerialDisposable()
        timer = SerialDisposable()
        original = SingleAssignmentDisposable()

        subscription.disposable = original

        switched = False
        _id = [0]

        def set_timer(timeout):
            my_id = _id[0]

            def timer_wins():
                return _id[0] == my_id

            d = SingleAssignmentDisposable()
            timer.disposable = d

            def on_next(x):
                if timer_wins():
                    subscription.disposable = other.subscribe(observer)

                d.dispose()

            def on_error(e):
                if timer_wins():
                    observer.on_error(e)

            def on_completed():
                if timer_wins():
                    subscription.disposable = other.subscribe(observer)

            d.disposable = timeout.subscribe(on_next, on_error, on_completed)

        set_timer(first_timeout)

        def observer_wins():
            res = not switched
            if res:
                _id[0] += 1

            return res

        def on_next(x):
            if observer_wins():
                observer.on_next(x)
                timeout = None
                try:
                    timeout = timeout_duration_selector(x)
                except Exception as e:
                    observer.on_error(e)
                    return

                set_timer(timeout)

        def on_error(e):
            if observer_wins():
                observer.on_error(e)

        def on_completed():
            if observer_wins():
                observer.on_completed()

        original.disposable = source.subscribe(on_next, on_error, on_completed)
        return CompositeDisposable(subscription, timer)
    return AnonymousObservable(subscribe)
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:98,代码来源:timeoutwithselector.py


注:本文中的rx.core.Observable.throw_exception方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。