def test_from_callback_single(self): res = Observable.from_callback(lambda file, cb: cb(file))('file.txt') def on_next(r): self.assertEqual(r, 'file.txt') def on_error(err): assert(False) def on_completed(): assert(True) res.subscribe(on_next, on_error, on_completed)
def test_from_callback(self): res = Observable.from_callback(lambda cb: cb(True))() def on_next(r): self.assertEqual(r, True) def on_error(err): assert (False) def on_completed(): assert (True) res.subscribe(on_next, on_error, on_completed)
def test_from_callback(self): res = Observable.from_callback(lambda cb: cb(True))() def on_next(r): self.assertEqual(r, True) def on_error(err): assert(False) def on_completed(): assert(True) res.subscribe(on_next, on_error, on_completed)
def test_from_node_callback_selector(self): res = Observable.from_callback(lambda f, s, t, cb: cb(f, s, t), lambda r: r[0])(1, 2, 3) def on_next(r): self.assertEqual(r, 1) def on_error(err): assert (False) def on_completed(): assert (True) res.subscribe(on_next, on_error, on_completed)
def test_from_callback_single(self): res = Observable.from_callback(lambda file, cb: cb(file))('file.txt') def on_next(r): self.assertEqual(r, 'file.txt') def on_error(err): print(err) assert (False) def on_completed(): assert (True) res.subscribe(on_next, on_error, on_completed)
def test_from_node_callback_selector(self): res = Observable.from_callback( lambda f,s,t,cb: cb(f,s,t), lambda r: r[0] )(1,2,3) def on_next(r): self.assertEqual(r, 1) def on_error(err): assert(False) def on_completed(): assert(True) res.subscribe(on_next, on_error, on_completed)
from rx import Observable def print_value(val): print(val) def say_hellop(name, callback): callback('Hello {}'.format(name)) if __name__ == '__main__': hello = Observable.from_callback(say_hellop) hello('Jack').subscribe(print_value) hello('Sam').subscribe(print_value)
from rx import Observable def foo(what, handler): print("foo: {}".format(what)) handler("hello " + what) callback = Observable.from_callback(foo) cbk_obs = callback("world") print("subscribing...") cbk_obs.subscribe( on_next=lambda i: print("item: {}".format(i)), on_error=lambda e: print("error: {}".format(e)), on_completed=lambda: print("completed") )
# Case 01 - Observe a List # Function - print the observed values def print_value(value): print('{} is the value'.format(value)) # Function - Create the observable and subscribe to the print value function # Covert list to observable # used the subscribe function to print the value Observable.from_(['abc', 'def', 'ghi']).subscribe(print_value) # Case 02 - Observe a callback def say_hello(name, callback): callback('hello {}!'.format(name)) # Factor programming # A factory is an object for creating other objects hello = Observable.from_callback( say_hello) # a factory to create new observable hello('Rudolf').subscribe(print_value) # actually creates an observable hello('observable').subscribe(print_value) # creates an observalbe # Case 03 - Observable from list Observable.from_list([1, 2, 3]).subscribe(print_value) # Case 04 - Observable from different objects in time Observable.of(1, 2, 3, 4, 'A', 'B', 'C').subscribe(print_value)