Exemplo n.º 1
0
    def test_empty_observer_throw_exception(self):
        scheduler = TestScheduler()
        xs = Observable.empty(scheduler)
        xs.subscribe(lambda x: None, lambda ex: None, lambda: _raise('ex'))

        with self.assertRaises(RxException):
            scheduler.start()
Exemplo n.º 2
0
def case(mapper, sources, default_source=None) -> ObservableBase:
    """Uses mapper to determine which source in sources to use.

    Example:
    1 - res = rx.Observable.case(mapper, { '1': obs1, '2': obs2 })
    2 - res = rx.Observable.case(mapper, { '1': obs1, '2': obs2 }, obs0)

    Keyword arguments:
    mapper -- The function which extracts the value for to test in a
        case statement.
    sources -- An object which has keys which correspond to the case
        statement labels.
    default_source -- The observable sequence or Future that will be run
        if the sources are not matched. If this is not provided, it
        defaults to rx.Observabe.empty.

    Returns an observable sequence which is determined by a case statement.
    """

    default_source = default_source or Observable.empty()

    def factory(_) -> ObservableBase:
        try:
            result = sources[mapper()]
        except KeyError:
            result = default_source

        result = Observable.from_future(result) if is_future(
            result) else result

        return result

    return Observable.defer(factory)
Exemplo n.º 3
0
def if_then(condition: Callable[[], bool],
            then_source: ObservableBase,
            else_source: ObservableBase = None) -> ObservableBase:
    """Determines whether an observable collection contains values.

    Example:
    1 - res = rx.Observable.if(condition, obs1)
    2 - res = rx.Observable.if(condition, obs1, obs2)

    Keyword parameters:
    condition -- The condition which determines if the then_source or
        else_source will be run.
    then_source -- The observable sequence or Promise that
        will be run if the condition function returns true.
    else_source -- [Optional] The observable sequence or
        Promise that will be run if the condition function returns
        False. If this is not provided, it defaults to
        rx.Observable.empty

    Returns an observable sequence which is either the
    then_source or else_source.
    """

    else_source = else_source or Observable.empty()

    then_source = Observable.from_future(then_source) if is_future(
        then_source) else then_source
    else_source = Observable.from_future(else_source) if is_future(
        else_source) else else_source

    def factory(_: abc.Scheduler):
        return then_source if condition() else else_source

    return Observable.defer(factory)
Exemplo n.º 4
0
    def test_finally_only_called_once_empty(self):
        invasserte_count = [0]

        def action():
            invasserte_count[0] += 1
            return invasserte_count
        some_observable = Observable.empty().finally_action(action)

        d = some_observable.subscribe()
        d.dispose()
        d.dispose()
        self.assertEqual(1, invasserte_count[0])
Exemplo n.º 5
0
    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()
Exemplo n.º 6
0
    def test_map_throws(self):
        with self.assertRaises(RxException):
            Observable.return_value(1) \
                .map(mapper_indexed=lambda x, y: x) \
                .subscribe_(lambda x: _raise("ex"))

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

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

        def subscribe(observer, scheduler=None):
            _raise('ex')

        with self.assertRaises(RxException):
            Observable.create(subscribe) \
                .map(lambda x: x) \
                .subscribe()
Exemplo n.º 7
0
    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()
Exemplo n.º 8
0
    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()
Exemplo n.º 9
0
    def test_map_with_index_throws(self):
        with self.assertRaises(RxException):
            return Observable.return_value(1) \
                .map(mapper_indexed=lambda x, index: x) \
                .subscribe_(lambda x: _raise('ex'))

        with self.assertRaises(RxException):
            return Observable.throw('ex') \
                .map(mapper_indexed=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(mapper_indexed=lambda x, index: x) \
                .subscribe()
    def __init__(self, id, local_setvelocity_publisher, xoffset, yoffset,
                 leader):
        self.local_setvelocity_publisher = local_setvelocity_publisher
        self.id = id
        self.xoffset = xoffset
        self.yoffset = yoffset
        self.leader = leader
        self.started = False
        self.ros_subscriptions = []

        self.flock_coordinates = {}
        self.leaderposition_subject = Subject()
        self.selfposition_subject = Subject()
        self.leadervelocity_subject = Subject()
        self.selfvelocity_subject = Subject()
        self.flock_repulsion = Subject()

        formation_position_attraction = Observable.combine_latest(
            self.leaderposition_subject, self.selfposition_subject,
            lambda leaderposition, selfposition: self.formation_position(
                leaderposition, selfposition))

        leaderVelocity = self.leadervelocity_subject \
            .map(lambda twist: self.format_velocities(twist))

        leaderVelocityDampening = Observable.combine_latest(
            self.leadervelocity_subject, self.selfvelocity_subject,
            lambda leadertwist, selftwist: self.velocity_dampening(
                leadertwist, selftwist))

        # self.navigate_subscription = Observable.combine_latest([leaderVelocity, formation_position_attraction, self.flock_repulsion], lambda vectors: self.navigate(vectors))

        self.navigate_subscription = Observable.combine_latest([leaderVelocity, leaderVelocityDampening, formation_position_attraction, self.flock_repulsion], lambda *positions: positions) \
            .sample(self.SAMPLE_RATE) \
            .subscribe(lambda vectors: self.navigate(vectors))

        # self.navigate_subscription = self.flock_repulsion \
        #     .subscribe(lambda vectors: self.navigate([vectors]))

        self.flockSubscription = Observable.empty().subscribe()
Exemplo n.º 11
0
def take(count: int, source: ObservableBase):
    """Returns a specified number of contiguous elements from the start of
    an observable sequence.

    1 - source.take(5)

    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 count < 0:
        raise ArgumentOutOfRangeException()

    if not count:
        return Observable.empty()

    observable = source

    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 observable.subscribe_(on_next, observer.on_error,
                                     observer.on_completed, scheduler)

    return AnonymousObservable(subscribe)
Exemplo n.º 12
0
 def factory():
     return Observable.empty(scheduler)
Exemplo n.º 13
0
 def on_completed(self):
     self.on_next(Observable.empty())
Exemplo n.º 14
0
 def on_completed(self):
     self.on_next(Observable.empty())
Exemplo n.º 15
0
def observable_window_with_openings(self, window_openings, window_closing_mapper):
    return window_openings.group_join(self, window_closing_mapper, lambda _: Observable.empty(), lambda _, window: window)
Exemplo n.º 16
0
 def test_for_each_empty(self):
     lst = []
     Observable.empty().to_blocking().for_each(lambda x: lst.append(x))
     assert (lst == [])
Exemplo n.º 17
0
 def mapper(_) -> ObservableBase:
     return Observable.empty()
Exemplo n.º 18
0
 def create():
     return Observable.empty(scheduler).time_interval(scheduler)
Exemplo n.º 19
0
 def test_as_observable_hides(self):
     some_observable = Observable.empty()
     assert(some_observable.as_observable() != some_observable)
Exemplo n.º 20
0
 def create():
     return Observable.empty().time_interval()
Exemplo n.º 21
0
 def selector(_):
     return Observable.empty()
Exemplo n.º 22
0
 def test_For_each_index_empty(self):
     lst_x = []
     Observable.empty().to_blocking().for_each(lambda x: lst_x.append(x))
     assert (lst_x == [])
Exemplo n.º 23
0
 def test_for_each_empty(self):
     lst = []
     Observable.empty().to_blocking().for_each(lambda x: lst.append(x))
     assert(lst == [])
Exemplo n.º 24
0
 def test_For_each_index_empty(self):
     lstX = []
     Observable.empty().to_blocking().for_each(lambda x, i: lstX.append(x))
     assert(lstX == [])
Exemplo n.º 25
0
 def selector(_):
     return Observable.empty()
Exemplo n.º 26
0
 def test_as_observable_hides(self):
     some_observable = Observable.empty()
     assert (some_observable.as_observable() != some_observable)
Exemplo n.º 27
0
 def factory():
     return Observable.empty()