def test_map_with_index_dispose_inside_mapper(self): scheduler = TestScheduler() xs = scheduler.create_hot_observable(on_next(100, 4), on_next(200, 3), on_next(500, 2), on_next(600, 1)) invoked = [0] results = scheduler.create_observer() d = SerialDisposable() def projection(x, index): invoked[0] += 1 if scheduler.clock > 400: d.dispose() return x + index * 10 d.disposable = xs.pipe(map_indexed(projection)).subscribe(results) def action(scheduler, state): return d.dispose() scheduler.schedule_absolute(disposed, action) scheduler.start() assert results.messages == [on_next(100, 4), on_next(200, 13)] assert xs.subscriptions == [subscribe(0, 500)] assert invoked[0] == 3
def create(): def mapper(ys, i): def proj(y): return "%s %s" % (i, y) return ys.pipe(ops.map(proj), ops.concat(rx.return_value('%s end' % i))) return xs.pipe(ops.window_with_time(100, 50), ops.map_indexed(mapper), ops.merge_all())
def factory(): def projection(x, index): invoked[0] += 1 if invoked[0] == 3: raise Exception(ex) return (x + 1) + (index * 10) return xs.pipe(map_indexed(projection))
def create(): def mapper(w, i): return w.pipe(ops.map(lambda x: str(i) + ' ' + str(x))) return xs.pipe( ops.window(ys), ops.map_indexed(mapper), ops.merge_all(), )
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 create(): def projection(w, i): def inner_proj(x): return "%s %s" % (i, x) return w.pipe(ops.map(inner_proj)) return xs.pipe(ops.window_with_time_or_count(70, 3), ops.map_indexed(projection), ops.merge_all())
def _flat_map_internal(source, mapper=None, mapper_indexed=None): def projection(x, i): mapper_result = mapper(x) if mapper else mapper_indexed(x, i) if isinstance(mapper_result, collections.abc.Iterable): result = from_(mapper_result) else: result = from_future(mapper_result) if is_future( mapper_result) else mapper_result return result return source.pipe(ops.map_indexed(projection), ops.merge_all())
def create(): def closing(): return rx.throw(ex) def mapper(w, i): return w.pipe(ops.map(lambda x: str(i) + ' ' + str(x))) return xs.pipe( ops.window(closing), ops.map_indexed(mapper), ops.merge_all(), )
def create(): def closing(x): raise Exception(ex) def mapper(w, i): return w.pipe(ops.map(lambda x: str(i) + ' ' + str(x))) return xs.pipe( ops.window(ys, closing), ops.map_indexed(mapper), ops.merge_all(), )
def _flat_map_internal(source, mapper=None, mapper_indexed=None): def projection(x, i): mapper_result = mapper(x) if mapper else mapper_indexed(x, i) if isinstance(mapper_result, collections.abc.Iterable): result = from_(mapper_result) else: result = from_future(mapper_result) if is_future( mapper_result) else mapper_result return result return source.pipe( ops.map_indexed(projection), ops.merge_all() )
def create(): def closing(): curr = window[0] window[0] += 1 return rx.timer(curr * 100) def mapper(w, i): return w.pipe(ops.map(lambda x: str(i) + ' ' + str(x))) return xs.pipe( ops.window(closing), ops.map_indexed(mapper), ops.merge_all(), )
def create(): def closings(): w = window[0] window[0] += 1 return rx.timer(w * 100) def mapper(w, i): return w.pipe(ops.map(lambda x: str(i) + ' ' + str(x))) return xs.pipe( ops.window(window_closing_mapper=closings), ops.map_indexed(mapper), ops.merge_all(), )
def test_map_with_index_throws(self): with self.assertRaises(RxException): mapper = map_indexed(lambda x, index: x) return return_value(1).pipe(mapper).subscribe( lambda x: _raise('ex')) with self.assertRaises(RxException): return throw('ex').pipe(mapper).subscribe(lambda x: x, lambda ex: _raise(ex)) with self.assertRaises(RxException): return empty().pipe(mapper).subscribe(lambda x: x, lambda ex: None, lambda: _raise('ex')) with self.assertRaises(RxException): return create(lambda o, s: _raise('ex')).pipe(mapper).subscribe()
def test_map_with_index_throws(self): with self.assertRaises(RxException): mapper = map_indexed(lambda x, index: x) return return_value(1).pipe( mapper ).subscribe(lambda x: _raise('ex')) with self.assertRaises(RxException): return throw('ex').pipe( mapper ).subscribe(lambda x: x, lambda ex: _raise(ex)) with self.assertRaises(RxException): return empty().pipe( mapper ).subscribe(lambda x: x, lambda ex: None, lambda: _raise('ex')) with self.assertRaises(RxException): return create(lambda o, s: _raise('ex')).pipe( mapper ).subscribe()
def factory(): return xs.pipe(map_indexed())
def projection(x): return rx.interval(10).pipe(ops.map_indexed(lambda a, b: x), ops.take(x))
def factory(): def projection(x, index): invoked[0] += 1 return (x + 1) + (index * 10) return xs.pipe(map_indexed(projection))
def create(): def mapper(w, i): return w.pipe(ops.map(lambda x: "%s %s" % (i, x))) return xs.pipe(ops.window_with_time(100, 70), ops.map_indexed(mapper), ops.merge_all())
def _skip_while_indexed(predicate: typing.PredicateIndexed) -> Callable[[Observable], Observable]: return pipe( ops.map_indexed(lambda x, i: (x, i)), ops.skip_while(lambda x: predicate(*x)), ops.map(lambda x: x[0]) )
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())
import os, glob import rx import rx.operators as op from rx.scheduler import ImmediateScheduler l = rx.from_iterable(glob.glob('./train/*.png')).pipe( op.map(os.path.basename), op.map(rx.just), op.flat_map( lambda x: x.pipe( op.repeat(3), op.subscribe_on(ImmediateScheduler()), op.map_indexed(lambda x, i: x.replace(".png", f"_{i}.png")), ), ), op.to_list()).run() print(l) l = rx.from_iterable(glob.glob('./train/*.png')).pipe( op.map(os.path.basename), op.map(rx.just), op.map(op.repeat(3)), op.map(op.subscribe_on(ImmediateScheduler())), # op.flat_map(lambda x : x.pipe(op.map_indexed(lambda x,i: x.replace(".png",f"_{i}.png")))), # op.flat_map(lambda x: op.map_indexed(lambda x,i: x.replace(".png",f"_{i}.png"))(x) ), # op.flat_map(op.map_indexed(lambda x,i: x.replace(".png",f"_{i}.png"))), # op.flat_map(lambda x: print('way',x) or x), op.map(lambda x: op.map_indexed(lambda x, i: x.replace( ".png", f"_{i}.png"))(x)), op.flat_map(lambda x: x), # op.flat_map_indexed(lambda x,i: print(x,i) or x), op.map(print)).run()
def _skip_while_indexed( predicate: typing.PredicateIndexed ) -> Callable[[Observable], Observable]: return pipe(ops.map_indexed(lambda x, i: (x, i)), ops.skip_while(lambda x: predicate(*x)), ops.map(lambda x: x[0]))