def time_interval(self, scheduler): """Records the time interval between consecutive values in an observable sequence. 1 - res = source.time_interval(); 2 - res = source.time_interval(Scheduler.timeout) Keyword arguments: scheduler -- [Optional] Scheduler used to compute time intervals. If not specified, the timeout scheduler is used. Return An observable sequence with time interval information on values. """ source = self scheduler = scheduler or timeout_scheduler def defer(): last = [scheduler.now()] def selector(x): now = scheduler.now() span = now - last[0] last[0] = now return TimeInterval(value=x, interval=span) return source.map(selector) return Observable.defer(defer)
def time_interval(self, scheduler): """Records the time interval between consecutive values in an observable sequence. 1 - res = source.time_interval(); 2 - res = source.time_interval(Scheduler.timeout) Keyword arguments: scheduler -- [Optional] Scheduler used to compute time intervals. If not specified, the timeout scheduler is used. Return An observable sequence with time interval information on values. """ source = self scheduler = scheduler or timeout_scheduler def defer(): last = [scheduler.now()] def selector(x): now = scheduler.now() span = now - last[0] last[0] = now return TimeInterval(value=x, interval=span) return source.select(selector) return Observable.defer(defer)
def observable_timer_timespan_and_period(duetime, period, scheduler): if duetime == period: def subscribe(observer): def action(count): observer.on_next(count) return count + 1 return scheduler.schedule_periodic(period, action, state=0) return AnonymousObservable(subscribe) def defer(): return observable_timer_date_and_period(scheduler.now() + scheduler.to_timedelta(duetime), period, scheduler) return Observable.defer(defer)
def observable_timer_timespan_and_period(cls, duetime, period, scheduler): if duetime == period: def subscribe(observer): def action(count): observer.on_next(count) count += 1 return count return scheduler.schedule_periodic(period, action, 0) return AnonymousObservable(subscribe) def deferred(): return cls.observable_timer_date_and_period(scheduler.now() + duetime, period, scheduler) return Observable.defer(deferred)
def many_select(self, selector, scheduler=None): """Comonadic bind operator. Internally projects a new observable for each value, and it pushes each observable into the user-defined selector function that projects/queries each observable into some result. Keyword arguments: selector -- {Function} A transform function to apply to each element. scheduler -- {Object} [Optional] Scheduler used to execute the operation. If not specified, defaults to the ImmediateScheduler. Returns {Observable} An observable sequence which results from the comonadic bind operation. """ scheduler = scheduler or immediate_scheduler source = self def factory(): chain = [None] def mapper(x): curr = ChainObservable(x) chain[0] and chain[0].on_next(x) chain[0] = curr return curr def on_error(e): if chain[0]: chain[0].on_error(e) def on_completed(): if chain[0]: chain[0].on_completed() return source.map( mapper ).tap( noop, on_error, on_completed ).observe_on( scheduler ).map( selector ) return Observable.defer(factory)
def observable_timer_timespan_and_period(duetime, period, scheduler): if duetime == period: def subscribe(observer): def action(count): observer.on_next(count) return count + 1 return scheduler.schedule_periodic(period, action, state=0) return AnonymousObservable(subscribe) def defer(): return observable_timer_date_and_period(scheduler.now() + duetime, period, scheduler) return Observable.defer(defer)
def observable_timer_timespan_and_period(cls, duetime, period, scheduler): log.debug("ObservableTime.observable_timer_timespan_and_period()") if duetime == period: def subscribe(observer): def action(count): observer.on_next(count) count += 1 return count return scheduler.schedule_periodic(period, action, 0) return AnonymousObservable(subscribe) def deferred(): return cls.observable_timer_date_and_period( scheduler.now() + duetime, period, scheduler) return Observable.defer(deferred)
def many_select(self, selector, scheduler=None): """Comonadic bind operator. Internally projects a new observable for each value, and it pushes each observable into the user-defined selector function that projects/queries each observable into some result. Keyword arguments: selector -- {Function} A transform function to apply to each element. scheduler -- {Object} [Optional] Scheduler used to execute the operation. If not specified, defaults to the ImmediateScheduler. Returns {Observable} An observable sequence which results from the comonadic bind operation. """ scheduler = scheduler or immediate_scheduler source = self def factory(): chain = [None] def mapper(x): curr = ChainObservable(x) chain[0] and chain[0].on_next(x) chain[0] = curr return curr def on_error(e): if chain[0]: chain[0].on_error(e) def on_completed(): if chain[0]: chain[0].on_completed() return source.map(mapper).tap( noop, on_error, on_completed).observe_on(scheduler).map(selector) return Observable.defer(factory)
def scan(self, seed=None, accumulator=None): """Applies an accumulator function over an observable sequence and returns each intermediate result. The optional seed value is used as the initial accumulator value. For aggregation behavior with no intermediate results, see Observable.aggregate. 1 - scanned = source.scan(lambda acc, x: acc + x) 2 - scanned = source.scan(0, lambda acc, x: acc + x) Keyword arguments: seed -- [Optional] The initial accumulator value. accumulator -- An accumulator function to be invoked on each element. Returns an observable sequence containing the accumulated values.</returns> """ has_seed = False if not seed is None: has_seed = True source = self def defer(): has_accumulation = False accumulation = None def projection(x): nonlocal accumulation, has_accumulation if has_accumulation: accumulation = accumulator(accumulation, x) else: accumulation = accumulator(seed, x) if has_seed else x has_accumulation = True return accumulation return source.select(projection) return Observable.defer(defer)
def scan(self, accumulator, seed=None): """Applies an accumulator function over an observable sequence and returns each intermediate result. The optional seed value is used as the initial accumulator value. For aggregation behavior with no intermediate results, see Observable.aggregate. 1 - scanned = source.scan(lambda acc, x: acc + x) 2 - scanned = source.scan(0, lambda acc, x: acc + x) Keyword arguments: seed -- [Optional] The initial accumulator value. accumulator -- An accumulator function to be invoked on each element. Returns an observable sequence containing the accumulated values. """ has_seed = False if not seed is None: has_seed = True source = self def defer(): has_accumulation = False accumulation = None def projection(x): nonlocal accumulation, has_accumulation if has_accumulation: accumulation = accumulator(accumulation, x) else: accumulation = accumulator(seed, x) if has_seed else x has_accumulation = True return accumulation return source.select(projection) return Observable.defer(defer)
def observable_delay_date(self, duetime, scheduler): def defer(): timespan = scheduler.to_datetime(duetime) - scheduler.now() return self.observable_delay_timespan(timespan, scheduler) return Observable.defer(defer)