def test_marbles(marble_list, continuation_fun): obs = Observable.from_(marble_list)\ .map(lambda m: Observable.from_marbles(m)) result = continuation_fun(obs).to_blocking()\ .to_marbles() for marble in marble_list: print(marble) print(result)
def main(): """ Will connect to a local flighgear via telnet and power up the engines. Works best with aircraft c172p. """ fg = RxFlightGear(FlightGear()) starter = Subject() fg.starter(starter) fg.starter(Observable.from_marbles("0-1----------0|").map(lambda x: bool(int(x)))) fg.flaps(Observable.from_([0.5])) fg.rudder(Observable.from_([0.1])) fg.throttle(Observable.from_([1.0]))
def test_send_receive(self): total_clients = 2 result = [] @fdebug def process(con: TCPServerConnection): con.as_observable(Schedulers.computation) \ .map(lambda e: e.decode('utf-8')) \ .map(map_debug) \ .map(lambda e: e.encode('utf-8')) \ .subscribe(con) server = TCPServer() server.connect(address, 0) port = server.port print('listening at %s:%d' % (address, port)) server.as_observable(Schedulers.computation) \ .subscribe(on_next=process, on_error=lambda e: print('error on server', e)) clients = [] Observable.from_(range(total_clients)) \ .map(lambda e: TCPClient()) \ .map(lambda e: e.connect('127.0.0.1', port)) \ .subscribe(clients.append) print(clients) Observable.from_(clients) \ .flat_map(lambda c: c.as_observable(Schedulers.computation)) \ .map(lambda e: e.decode('utf-8')) \ .subscribe(result.append) send_data = Observable.from_marbles('-a-b-c-|') \ .map(lambda e: e.encode('utf-8')) \ .subscribe Observable.from_(clients).subscribe(send_data) time.sleep(1) server.stop() self.assertEqual(['a', 'a', 'b', 'b', 'c', 'c'], result) del Schedulers.computation
def test_marbles(): s1 = Observable.from_marbles('1---2-3|') # s2 = Observable.from_marbles('-a-b-c-|') # print(s1.merge(s2).to_blocking().to_marbles()) print(s1.to_blocking().to_marbles()) input('press a key')
def test_marbles(): r = Observable.from_marbles('1-2-3-4-|').map( lambda x: str(x + 1)).to_blocking().to_marbles() print(r)
# We have to import the marbles module to activate this function and # we have to import the TestScheduler. from rx.testing import marbles, TestScheduler from rx.concurrency import timeout_scheduler, new_thread_scheduler test_scheduler = TestScheduler() # When you create an observable, you have the option of passing in a # scheduler. The scheduler can be the Python Async IO scheduler, the # scheduler from Tornado or the scheduler from Q. The scheduler # coordinates and executes events. # # In this case we use the test scheduler because it lets us control # when events are executed. Observable.from_marbles('--(a1)-(b2)---(c3)|', test_scheduler).subscribe(print_value) # To execute the events in the observable with the test scheduler, we # have to call the start method. test_scheduler.start() # Interval # Intervals are great for continuously emitting items at a set time, # for example every second, or every hour. # As an example, we'll print out hello every 100 milliseconds until 1 # second has passed. test_scheduler = TestScheduler() Observable.interval(10, test_scheduler).take_until( Observable.timer(30)).subscribe(print_value) test_scheduler.start()
from rx import Observable, Observer from rx.testing import marbles class PrintObserver(Observer): def on_next(self, value): print("Value :", value) def on_error(self, error): print("Error :", error) def on_completed(self): print("Completed") if __name__ == '__main__': source_1 = Observable.from_marbles("1-2-3-|") source_2 = Observable.from_marbles("-a-b-c|") print(source_1.merge(source_2).to_blocking().to_marbles())
from rx.testing import marbles from rx import Observable, Observer xs = Observable.from_marbles("a-b-c-|") xs.to_blocking().to_marbles()
def github_four(): from rx.testing import marbles for marble_string in ["1-2-3-x-5", "1-2-3-4-5"]: print("marble_string {}".format(marble_string)) xs = Observable.from_marbles(marble_string) print(xs.to_blocking().to_marbles())
def github_three(): from rx.testing import marbles xs = Observable.from_marbles("a-b-c-|") print(xs.to_blocking().to_marbles())