forked from fluentpython/example-code-2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coroaverager.py
43 lines (36 loc) · 888 Bytes
/
coroaverager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
A coroutine to compute a running average
# tag::CORO_AVERAGER_TEST[]
>>> coro_avg = averager() # <1>
>>> next(coro_avg) # <2>
0.0
>>> coro_avg.send(10) # <3>
10.0
>>> coro_avg.send(30)
20.0
>>> coro_avg.send(5)
15.0
# end::CORO_AVERAGER_TEST[]
# tag::CORO_AVERAGER_TEST_CONT[]
>>> coro_avg.send(20) # <1>
16.25
>>> coro_avg.close() # <2>
>>> coro_avg.close() # <3>
>>> coro_avg.send(5) # <4>
Traceback (most recent call last):
...
StopIteration
# end::CORO_AVERAGER_TEST_CONT[]
"""
# tag::CORO_AVERAGER[]
from collections.abc import Generator
def averager() -> Generator[float, float, None]: # <1>
total = 0.0
count = 0
average = 0.0
while True: # <2>
term = yield average # <3>
total += term
count += 1
average = total/count
# end::CORO_AVERAGER[]