def test_filter_str(): def iseven(x): return x % 2 == 0 source = Stream() s = source.filter(iseven) assert str(s) == '<filter: iseven>'
def test_filter_none(): source = Stream() L = source.filter(None).sink_to_list() for i in range(10): source.emit(i % 3) assert L == [1, 2, 1, 2, 1, 2]
def test_filter(): source = Stream() L = source.filter(lambda x: x % 2 == 0).sink_to_list() for i in range(10): source.emit(i) assert L == [0, 2, 4, 6, 8]
def test_filter_kwargs(): source = Stream() L = source.filter(lambda x, n=1: x % n == 0, n=2).sink_to_list() for i in range(10): source.emit(i) assert L == [0, 2, 4, 6, 8]
from geostreams.client import redis_connection, read_from_redis from threading import Thread # Python interface to redis server r = redis_connection() # pubsub p = r.pubsub() p.subscribe("game_of_life") # make stream source = Stream() s = source.filter(lambda x: x['type'] == 'message')\ .map(lambda msg: msg['data'])\ .map(lambda key: read_from_redis(r, key)) class MyThread(Thread): def run(self): for key in p.listen(): source.emit(key) thread = MyThread() import time # curses import curses win = curses.initscr()
doc.title = "Streaming Conway's Game of Life" doc.add_root(gridplot([[p2d, p1d]])) return update update = make_document(curdoc()) # Python interface to redis server r = redis_connection() # pubsub p = r.pubsub() p.subscribe("game_of_life") source = Stream() s = source.filter(lambda x: x['type'] == 'message')\ .map(lambda msg: msg['data'])\ .map(lambda key: read_from_redis(r, key))\ .map(lambda arr: (arr, arr.sum()))\ .sink(frames.append) class MyThread(Thread): def run(self): for key in p.listen(): source.emit(key) MyThread().start()