def average(source: Observable) -> Observable: """Partially applied average operator. Computes the average of an observable sequence of values that are in the sequence or obtained by invoking a transform function on each element of the input sequence if present. Examples: >>> res = average(source) Args: source: Source observable to average. Returns: An observable sequence containing a single element with the average of the sequence of values. """ if key_mapper: return source.pipe(operators.map(key_mapper), operators.average()) def accumulator(prev, cur): return AverageValue(sum=prev.sum + cur, count=prev.count + 1) def mapper(s): if s.count == 0: raise Exception('The input sequence was empty') return s.sum / float(s.count) seed = AverageValue(sum=0, count=0) return source.pipe(operators.scan(accumulator, seed), operators.last(), operators.map(mapper))
def test_average_int32_empty(self): scheduler = TestScheduler() msgs = [on_next(150, 1), on_completed(250)] xs = scheduler.create_hot_observable(msgs) res = scheduler.start(create=lambda: xs.pipe(_.average())).messages assert(len(res) == 1) assert(res[0].value.kind == 'E' and res[0].value.exception != None) assert(res[0].time == 250)
def test_average_int32_empty(self): scheduler = TestScheduler() msgs = [on_next(150, 1), on_completed(250)] xs = scheduler.create_hot_observable(msgs) res = scheduler.start(create=lambda: xs.pipe(_.average())).messages assert (len(res) == 1) assert (res[0].value.kind == 'E' and res[0].value.exception != None) assert (res[0].time == 250)
def average(source: Observable) -> Observable: """Partially applied average operator. Computes the average of an observable sequence of values that are in the sequence or obtained by invoking a transform function on each element of the input sequence if present. Examples: >>> res = average(source) Args: source: Source observable to average. Returns: An observable sequence containing a single element with the average of the sequence of values. """ if key_mapper: return source.pipe( operators.map(key_mapper), operators.average() ) def accumulator(prev, cur): return AverageValue(sum=prev.sum+cur, count=prev.count+1) def mapper(s): if s.count == 0: raise Exception('The input sequence was empty') return s.sum / float(s.count) seed = AverageValue(sum=0, count=0) return source.pipe( operators.scan(accumulator, seed), operators.last(), operators.map(mapper) )
from rx import of, operators as op """this operator will calculate the average from the source observable given and output an observable that will have the average value. """ my_of = of(1,2,3,4,5,6,7,8,9,10) my_of.pipe( op.average() ).subscribe(lambda i: print("average is {}".format(i))) # result # value is 5.5
import rx import rx.operators as ops numbers = rx.from_([1, 2, 3, 4]) numbers.pipe(ops.average()).subscribe( on_next=lambda i: print("on_next {}".format(i)), on_error=lambda e: print("on_error: {}".format(e)), on_completed=lambda: print("on_completed") )
def create(): return xs.pipe(_.average(len))
import rx from rx import operators as ops pipe = rx.range(1, 11) \ .pipe(ops.filter(lambda i: i % 2 == 0), ops.filter(lambda i: i < 3), ops.map(lambda i: i + i), ops.map(lambda i: i ** i), ops.average(lambda x: x + x), ops.sum()) pipe.subscribe(lambda x: print("value is {0}".format(x)))
def stream(self): return self.subject_.pipe( ops.window_with_time(self.window), ops.map(lambda x: x.pipe(ops.average(), ops.first())), ops.merge_all() )