Skip to content

Commit 304d628

Browse files
committed
updated from atlas
1 parent e910ec5 commit 304d628

File tree

4 files changed

+57
-26
lines changed

4 files changed

+57
-26
lines changed

control/coroaverager0.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
55
>>> coro_avg = averager() # <1>
66
>>> next(coro_avg) # <2>
7-
0.0
87
>>> coro_avg.send(10) # <3>
98
10.0
109
>>> coro_avg.send(30)
@@ -15,8 +14,9 @@
1514
"""
1615

1716
def averager():
18-
total = average = 0.0
17+
total = 0.0
1918
count = 0
19+
average = None
2020
while True:
2121
term = yield average # <4>
2222
total += term

control/coroaverager1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919

2020
@coroutine # <5>
2121
def averager(): # <6>
22-
total = average = 0.0
22+
total = 0.0
2323
count = 0
24+
average = None
2425
while True:
2526
term = yield average
2627
total += term

control/coroaverager2.py

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,61 @@
11
"""
2-
A coroutine to compute a running average
3-
4-
>>> coro_avg = averager() # <1>
5-
>>> from inspect import getgeneratorstate
6-
>>> getgeneratorstate(coro_avg) # <2>
7-
'GEN_SUSPENDED'
8-
>>> coro_avg.send(10) # <3>
9-
10.0
2+
A coroutine to compute a running average.
3+
4+
Testing ``averager`` by itself::
5+
6+
# BEGIN RETURNING_AVERAGER_DEMO1
7+
8+
>>> coro_avg = averager()
9+
>>> next(coro_avg) # <1>
10+
>>> coro_avg.send(10)
1011
>>> coro_avg.send(30)
11-
20.0
12-
>>> coro_avg.send(5)
13-
15.0
12+
>>> coro_avg.send(6.5)
13+
>>> coro_avg.send(None) # <2>
14+
Traceback (most recent call last):
15+
...
16+
StopIteration: Result(count=3, average=15.5)
17+
18+
# END RETURNING_AVERAGER_DEMO1
19+
20+
Catching `StopIteration` to extract the value returned by
21+
the coroutine::
22+
23+
# BEGIN RETURNING_AVERAGER_DEMO2
24+
25+
>>> coro_avg = averager()
26+
>>> next(coro_avg)
27+
>>> coro_avg.send(10)
28+
>>> coro_avg.send(30)
29+
>>> coro_avg.send(6.5)
30+
>>> try:
31+
... coro_avg.send(None)
32+
... except StopIteration as exc:
33+
... result = exc.value
34+
...
35+
>>> result
36+
Result(count=3, average=15.5)
37+
38+
# END RETURNING_AVERAGER_DEMO2
39+
1440
1541
"""
1642

43+
# BEGIN RETURNING_AVERAGER
1744
from collections import namedtuple
1845

19-
Result = namedtuple('Result', 'count total average')
46+
Result = namedtuple('Result', 'count average')
47+
2048

21-
def averager(): # <6>
22-
total = average = 0.0
49+
def averager():
50+
total = 0.0
2351
count = 0
24-
try:
25-
while True:
26-
term = yield average
27-
total += term
28-
count += 1
29-
average = total/count
30-
finally:
31-
return Result(count, total, average)
52+
average = None
53+
while True:
54+
term = yield
55+
if term is None:
56+
break # <1>
57+
total += term
58+
count += 1
59+
average = total/count
60+
return Result(count, average) # <2>
61+
# END RETURNING_AVERAGER

control/simulation/taxi_sim.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def taxi_process(ident, trips, start_time=0): # <1>
5757
time = yield Event(trip_ends, ident, 'drop off passenger') # <7>
5858

5959
yield Event(time + 1, ident, 'going home') # <8>
60-
# <9>
60+
# end of taxi process # <9>
6161
# END TAXI_PROCESS
6262

6363
# BEGIN TAXI_SIMULATOR

0 commit comments

Comments
 (0)