updated from atlas
This commit is contained in:
@@ -4,7 +4,6 @@ A coroutine to compute a running average
|
|||||||
|
|
||||||
>>> coro_avg = averager() # <1>
|
>>> coro_avg = averager() # <1>
|
||||||
>>> next(coro_avg) # <2>
|
>>> next(coro_avg) # <2>
|
||||||
0.0
|
|
||||||
>>> coro_avg.send(10) # <3>
|
>>> coro_avg.send(10) # <3>
|
||||||
10.0
|
10.0
|
||||||
>>> coro_avg.send(30)
|
>>> coro_avg.send(30)
|
||||||
@@ -15,8 +14,9 @@ A coroutine to compute a running average
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def averager():
|
def averager():
|
||||||
total = average = 0.0
|
total = 0.0
|
||||||
count = 0
|
count = 0
|
||||||
|
average = None
|
||||||
while True:
|
while True:
|
||||||
term = yield average # <4>
|
term = yield average # <4>
|
||||||
total += term
|
total += term
|
||||||
|
|||||||
@@ -19,8 +19,9 @@ from coroutil import coroutine # <4>
|
|||||||
|
|
||||||
@coroutine # <5>
|
@coroutine # <5>
|
||||||
def averager(): # <6>
|
def averager(): # <6>
|
||||||
total = average = 0.0
|
total = 0.0
|
||||||
count = 0
|
count = 0
|
||||||
|
average = None
|
||||||
while True:
|
while True:
|
||||||
term = yield average
|
term = yield average
|
||||||
total += term
|
total += term
|
||||||
|
|||||||
@@ -1,31 +1,61 @@
|
|||||||
"""
|
"""
|
||||||
A coroutine to compute a running average
|
A coroutine to compute a running average.
|
||||||
|
|
||||||
>>> coro_avg = averager() # <1>
|
Testing ``averager`` by itself::
|
||||||
>>> from inspect import getgeneratorstate
|
|
||||||
>>> getgeneratorstate(coro_avg) # <2>
|
# BEGIN RETURNING_AVERAGER_DEMO1
|
||||||
'GEN_SUSPENDED'
|
|
||||||
>>> coro_avg.send(10) # <3>
|
>>> coro_avg = averager()
|
||||||
10.0
|
>>> next(coro_avg) # <1>
|
||||||
|
>>> coro_avg.send(10)
|
||||||
>>> coro_avg.send(30)
|
>>> coro_avg.send(30)
|
||||||
20.0
|
>>> coro_avg.send(6.5)
|
||||||
>>> coro_avg.send(5)
|
>>> coro_avg.send(None) # <2>
|
||||||
15.0
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
StopIteration: Result(count=3, average=15.5)
|
||||||
|
|
||||||
|
# END RETURNING_AVERAGER_DEMO1
|
||||||
|
|
||||||
|
Catching `StopIteration` to extract the value returned by
|
||||||
|
the coroutine::
|
||||||
|
|
||||||
|
# BEGIN RETURNING_AVERAGER_DEMO2
|
||||||
|
|
||||||
|
>>> coro_avg = averager()
|
||||||
|
>>> next(coro_avg)
|
||||||
|
>>> coro_avg.send(10)
|
||||||
|
>>> coro_avg.send(30)
|
||||||
|
>>> coro_avg.send(6.5)
|
||||||
|
>>> try:
|
||||||
|
... coro_avg.send(None)
|
||||||
|
... except StopIteration as exc:
|
||||||
|
... result = exc.value
|
||||||
|
...
|
||||||
|
>>> result
|
||||||
|
Result(count=3, average=15.5)
|
||||||
|
|
||||||
|
# END RETURNING_AVERAGER_DEMO2
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# BEGIN RETURNING_AVERAGER
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
Result = namedtuple('Result', 'count total average')
|
Result = namedtuple('Result', 'count average')
|
||||||
|
|
||||||
def averager(): # <6>
|
|
||||||
total = average = 0.0
|
def averager():
|
||||||
|
total = 0.0
|
||||||
count = 0
|
count = 0
|
||||||
try:
|
average = None
|
||||||
while True:
|
while True:
|
||||||
term = yield average
|
term = yield
|
||||||
|
if term is None:
|
||||||
|
break # <1>
|
||||||
total += term
|
total += term
|
||||||
count += 1
|
count += 1
|
||||||
average = total/count
|
average = total/count
|
||||||
finally:
|
return Result(count, average) # <2>
|
||||||
return Result(count, total, average)
|
# END RETURNING_AVERAGER
|
||||||
@@ -57,7 +57,7 @@ def taxi_process(ident, trips, start_time=0): # <1>
|
|||||||
time = yield Event(trip_ends, ident, 'drop off passenger') # <7>
|
time = yield Event(trip_ends, ident, 'drop off passenger') # <7>
|
||||||
|
|
||||||
yield Event(time + 1, ident, 'going home') # <8>
|
yield Event(time + 1, ident, 'going home') # <8>
|
||||||
# <9>
|
# end of taxi process # <9>
|
||||||
# END TAXI_PROCESS
|
# END TAXI_PROCESS
|
||||||
|
|
||||||
# BEGIN TAXI_SIMULATOR
|
# BEGIN TAXI_SIMULATOR
|
||||||
|
|||||||
Reference in New Issue
Block a user