def test_from_marbles_reuse(self): string = "a--b---c--|" " 012345678901234567890" obs = rx.from_marbles(string) scheduler = TestScheduler() results = scheduler.start(self.create_factory(obs)).messages expected = [ ReactiveTest.on_next(200.0, 'a'), ReactiveTest.on_next(200.3, 'b'), ReactiveTest.on_next(200.7, 'c'), ReactiveTest.on_completed(201.0), ] assert results == expected scheduler = TestScheduler() results = scheduler.start(self.create_factory(obs)).messages expected = [ ReactiveTest.on_next(200.0, 'a'), ReactiveTest.on_next(200.3, 'b'), ReactiveTest.on_next(200.7, 'c'), ReactiveTest.on_completed(201.0), ] assert results == expected scheduler = TestScheduler() results = scheduler.start(self.create_factory(obs)).messages expected = [ ReactiveTest.on_next(200.0, 'a'), ReactiveTest.on_next(200.3, 'b'), ReactiveTest.on_next(200.7, 'c'), ReactiveTest.on_completed(201.0), ] assert results == expected
def test_from_marbles_on_next(self): string = "a" obs = rx.from_marbles(string) scheduler = TestScheduler() results = scheduler.start(self.create_factory(obs)).messages expected = [ReactiveTest.on_next(200.0, 'a')] assert results == expected
def main(): xs = rx.from_marbles("1-2-3-4-5-6-7-9-|").pipe(ops.publish()) xs.pipe(ops.map(failing), ops.retry()).subscribe(print) xs.connect() # Must connect. Cannot use ref_count() with publish() time.sleep(5)
def test_from_marbles_on_error(self): string = "#" obs = rx.from_marbles(string) scheduler = TestScheduler() results = scheduler.start(self.create_factory(obs)).messages expected = [ReactiveTest.on_error(200.0, Exception('error'))] assert results == expected
def test_cold(string: str, lookup: Dict = None, error: Exception = None) -> Observable: check() return rx.from_marbles( string, timespan=timespan, lookup=lookup, error=error, )
def test_from_marbles_on_error_specified(self): string = "#" ex = Exception('Foo') obs = rx.from_marbles(string, error=ex) scheduler = TestScheduler() results = scheduler.start(self.create_factory(obs)).messages expected = [ReactiveTest.on_error(200.0, ex)] assert results == expected
def test_from_marbles_marble_with_space(self): string = " -a b- c- - |" " 01 23 45 6 78901234567890" obs = rx.from_marbles(string) scheduler = TestScheduler() results = scheduler.start(self.create_factory(obs)).messages expected = [ ReactiveTest.on_next(200.1, 'ab'), ReactiveTest.on_next(200.4, 'c'), ReactiveTest.on_completed(200.7), ] assert results == expected
def test_from_marbles_marble_with_consecutive_symbols(self): string = "-ab(12)#--" " 012345678901234567890" ex = Exception('ex') obs = rx.from_marbles(string, error=ex) scheduler = TestScheduler() results = scheduler.start(self.create_factory(obs)).messages expected = [ ReactiveTest.on_next(200.1, 'ab'), ReactiveTest.on_next(200.3, 12), ReactiveTest.on_error(200.7, ex), ] assert results == expected
def test_from_marbles_timespan(self): string = "a--b---c" " 012345678901234567890" ts = 0.5 obs = rx.from_marbles(string, timespan=ts) scheduler = TestScheduler() results = scheduler.start(self.create_factory(obs)).messages expected = [ ReactiveTest.on_next(0 * ts + 200.0, 'a'), ReactiveTest.on_next(3 * ts + 200.0, 'b'), ReactiveTest.on_next(7 * ts + 200.0, 'c'), ] assert results == expected
def test_from_marbles_marble_with_group(self): string = "-(ab)-c-(12.5,def)--(6,|)" " 012345678901234567890" obs = rx.from_marbles(string) scheduler = TestScheduler() results = scheduler.start(self.create_factory(obs)).messages expected = [ ReactiveTest.on_next(200.1, 'ab'), ReactiveTest.on_next(200.6, 'c'), ReactiveTest.on_next(200.8, str(12.5)), ReactiveTest.on_next(200.8, 'def'), ReactiveTest.on_next(202.0, 6), ReactiveTest.on_completed(202.0), ] assert results == expected
def test_from_marbles_marble_lookup(self): string = "-ab-c-12-3-|" " 012345678901234567890" lookup = { 'ab': 'aabb', 'c': 'cc', 12: '1122', 3: 33, } obs = rx.from_marbles(string, lookup=lookup) scheduler = TestScheduler() results = scheduler.start(self.create_factory(obs)).messages expected = [ ReactiveTest.on_next(200.1, 'aabb'), ReactiveTest.on_next(200.4, 'cc'), ReactiveTest.on_next(200.6, '1122'), ReactiveTest.on_next(200.9, 33), ReactiveTest.on_completed(201.1), ] assert results == expected
# from_list 이용한 옵저버블 생성 -> 3.0 버전부터는 from_ 을 주로 사용한다. rx.from_list([1, 2, 3]).subscribe(print_value) print('--') # of 를 이용한 옵저버블 생성 rx.of(1, 2, 3, 'a', 'b', 'c').subscribe(print_value) # 위와 다른 점: args 로 생성한다. print('--') # 마블에서 옵저버블 생성 하기 # 타이밍을 제어 할 수 있다. from rx.testing import marbles, TestScheduler scheduler = TestScheduler() ts = 0.1 rx.from_marbles('--(a1)-(b2)---(c3)|', timespan=ts).subscribe(print_value) rx.from_marbles('(a6)---(b5)(c4)|', timespan=ts).subscribe(print_value) time.sleep(2) print('--') # Interval 을 이용한 옵저버블 생성 rx.interval(0.3).pipe(ops.take_until(rx.timer(3))).subscribe(print_value) time.sleep(4) print('--') # 버퍼 print('-- buffer') rx.from_(range(2000)).pipe(ops.buffer( rx.interval(0.001))).subscribe(on_next=lambda buffer: print( '# of items in buffer {}'.format(len(buffer)))) time.sleep(2)
import rx from rx import operators as ops """ Specify the error to be raised in place of the # symbol. """ err = ValueError("I don't like 5!") src0 = rx.from_marbles('12-----4-----67--|', timespan=0.2) src1 = rx.from_marbles('----3----5-# ', timespan=0.2, error=err) source = rx.merge(src0, src1).pipe(ops.do_action(print)) source.run()