Generator Online Calculations
Overview
An online calculation is a calculation for which you may wish to retrieve the calcualtion
results prior to having finished iterating the entire generator. As an example, a moving
average may be calculated for each point in a time, not just the last. The davinci online generator
script provides a simple mechanism to achieve this.
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