Online Aggregators

Overview

An online aggregator is a function that takes a generator (or iterator) and computes some aggregated value fomr the values of the generator. However, the online aggregator can compute intermediate values as the values in the generator are iterated over.

Usage

In order to use an online aggregator, you need to use the generator function from the online script to wrap your generator or iterator with a function that can register online aggregators. Then, you create aggregators by calling the aggregator constructor, passing in the generator and map function.

Once you iterate through the items, you can call each aggregator and get intermediate results.
from lib.online import generator, average, sum, length list2 = [{"value":1},{"value":2},{"value":3},{"value":4},{"value":5}] list2 = generator(list2) av = average(list2, lambda x:x['value']) sm = sum(list2, lambda x:x['value']) len = length(list2, lambda x:x['value']) for i in list2(): print(av()) print(sm()) print(len()) pass

Samples

def average(list1, prop): total = 0 count = 0 def update(value): nonlocal total, count if prop is not None: value = prop(value) total += value count += 1 pass def result(): nonlocal total, count return total / count if count > 0 else 0 list1.on(update) return result

Samples

Full Script

def generator(list1): updates = [] def result(): nonlocal updates for value in list1: for update in updates: update(value) pass yield value pass def listen(update): nonlocal updates updates.append(update) pass result.on = listen return result def average(list1, prop): total = 0 count = 0 def update(value): nonlocal total, count if prop is not None: value = prop(value) total += value count += 1 pass def result(): nonlocal total, count return total / count if count > 0 else 0 list1.on(update) return result def length(list1,prop): count = 0 def update(value): nonlocal count if prop is not None: value = prop(value) count += 1 pass def result(): nonlocal count return count list1.on(update) return result def sum(list1, prop): total = 0 def update(value): nonlocal total if prop is not None: value = prop(value) total += value pass def result(): nonlocal total return total list1.on(update) return result if __name__ == "__main__": list1 = [1,2,3,4,5] list1 = generator(list1) list2 = [{"value":1},{"value":2},{"value":3},{"value":4},{"value":5}] list2 = generator(list2) av = average(list2, lambda x:x['value']) sm = sum(list2, lambda x:x['value']) len = length(list2, lambda x:x['value']) for i in list2(): print(av()) print(sm()) print(len()) pass print(av()) print(sm()) print(len()) pass