def test_window_sum(self): res = [] rx.from_(range(6)).pipe( ops.window_with_count(count=3, skip=1), ops.flat_map(lambda i: i.pipe(ops.sum(), )), ).subscribe(on_next=res.append) assert res == [3, 6, 9, 12, 9, 5, 0]
def create(): def mapper(w, i): def mapping(x): return "%s %s" % (i, x) return w.pipe(ops.map(mapping)) return xs.pipe(ops.window_with_count(3, 2), ops.map_indexed(mapper), ops.merge_all())
def buffer_with_count(source: Observable) -> Observable: nonlocal skip if skip is None: skip = count def mapper(value): return value.pipe(ops.to_iterable(), ops.map(list)) def predicate(value): return len(value) > 0 return source.pipe(ops.window_with_count(count, skip), ops.flat_map(mapper), ops.filter(predicate))
def open(self): print("WebSocket aberto") # Um Sujeito é observável e observ5ador, portanto, podemos nos inscrever # para ele e também alimentá-lo (on_next) com novos valores self.subject = Subject() # Agora pegamos nossos óculos mágicos e projetamos o fluxo de bytes em query = self.subject.pipe( # 1. fluxo de códigos-chave ops.map(lambda obj: obj["keycode"]), # 2. fluxo de janelas (10 ints de comprimento) ops.window_with_count(10, 1), # 3. fluxo de booleanos, verdadeiro ou falso ops.flat_map(lambda win: win.pipe(ops.sequence_equal(codes))), # 4. fluxo de verdadeiras ops.filter(lambda equal: equal)) # 4. então, assinamos o Trues e sinalizamos para a Konami! se virmos algum query.subscribe(lambda x: self.write_message("Konami!"))
def open(self): print("WebSocket opened") # A Subject is both an observable and observer, so we can both subscribe # to it and also feed (on_next) it with new values self.subject = Subject() # Now we take on our magic glasses and project the stream of bytes into # a ... query = self.subject.pipe( # 1. stream of keycodes ops.map(lambda obj: obj["keycode"]), # 2. stream of windows (10 ints long) ops.window_with_count(10, 1), # 3. stream of booleans, True or False ops.flat_map(lambda win: win.pipe(ops.sequence_equal(codes))), # 4. stream of Trues ops.filter(lambda equal: equal)) # 4. we then subscribe to the Trues, and signal Konami! if we see any query.subscribe(lambda x: self.write_message("Konami!"))
def open(self): print("WebSocket opened") # A Subject is both an observable and observer, so we can both subscribe # to it and also feed (on_next) it with new values self.subject = Subject() # Now we take on our magic glasses and project the stream of bytes into # a ... query = self.subject.pipe( # 1. stream of keycodes ops.map(lambda obj: obj["keycode"]), # 2. stream of windows (10 ints long) ops.window_with_count(10, 1), # 3. stream of booleans, True or False ops.flat_map(lambda win: win.pipe(ops.sequence_equal(codes))), # 4. stream of Trues ops.filter(lambda equal: equal) ) # 4. we then subscribe to the Trues, and signal Konami! if we see any query.subscribe(lambda x: self.write_message("Konami!"))
def create(): def proj(w, i): return w.pipe(ops.map(lambda x: str(i) + ' ' + str(x))) return xs.pipe(ops.window_with_count(3, 2), ops.map_indexed(proj), ops.merge_all())
def read_last_line_from_file(filename): with open(filename) as file: lines = file.readlines() last_line = lines[-1] return rx.just(last_line) read_last_line_from_file('test.csv').pipe( ops.flat_map(lambda line: read_last_line_from_file('test2.csv')) ).subscribe(print_value) # 두번째 파일의 결과만 있다는것에 유의 해서 보라 print('-- window_with_count') # 버퍼와 비슷 rx.from_(range(3)).pipe(ops.window_with_count(2)).subscribe(print_value) # 두개의 옵져버블이 생성됨 # observable=[1, 2] # observable=[3] print('-- window_with_count | FlatMap') rx.from_(range(3)).pipe(ops.window_with_count(2), ops.flat_map(lambda x: x)).subscribe(print_value) # flatmap 은 # flat 하면 리스트를 푼다. # [[1,2], [3]] -> [1,2,3] # 그리고 나서 map 한다. x -> x # [1, 2, 3] -> [1, 2, 3] print('-- window_with_time')
rx.from_iterable([1, 2, 3, 4]).subscribe(print) # bufferはたまってから処理する rx.from_iterable(range(10)).pipe(op.map(lambda x: x * 2), op.buffer_with_count(3)).subscribe(print) rx.from_iterable(range(10)).pipe( op.map(lambda x: x * 2), op.buffer_with_count(3), # ここではリストになっている op.map(len)).subscribe(print) # windowもたまってから処理するがlistではなくObservableになる rx.from_iterable(range(10)).pipe( op.map(lambda x: x * 2), op.window_with_count(3), ).subscribe(print) # observableの中身を1つ1つ処理したい場合はflatmapが使える rx.from_iterable(range(10)).pipe( op.map(lambda x: x * 2), op.window_with_count(3), # ただしこの時点でxはObservable # 複数のObservableを1つにまとめる op.flat_map(lambda x: x), # flat_mapを抜けると要素の型になる op.map(lambda x: x / 2)).subscribe(print, print) def gen(count: int): for i in range(count):