Skip to content

Commit a786180

Browse files
committed
update from Atlas with major reorg
1 parent 57902d3 commit a786180

File tree

134 files changed

+369
-520
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+369
-520
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ concurrency/charfinder/charfinder_index.pickle
44
metaprog/oscon-schedule/data/schedule?_db
55
concurrency/wikipedia/fixture/docroot/
66
17-futures/countries/flags/
7+
attic/futures/countries/flags/
78

89
# Byte-compiled / optimized / DLL files
910
__pycache__/
File renamed without changes.
File renamed without changes.
File renamed without changes.

05-1class-func/bingo.py renamed to 05-1class-func/bingocall.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
# BEGIN BINGO_DEMO
33
44
>>> bingo = BingoCage(range(3))
5-
>>> bingo()
6-
2
5+
>>> bingo.pick()
6+
1
77
>>> bingo()
88
0
99
>>> callable(bingo)
1010
True
11+
1112
# END BINGO_DEMO
1213
1314
"""
@@ -22,9 +23,13 @@ def __init__(self, items):
2223
self._items = list(items) # <1>
2324
random.shuffle(self._items) # <2>
2425

25-
def __call__(self):
26-
if not self._items: # <3>
27-
raise IndexError('pop from empty BingoCage')
28-
return self._items.pop()
26+
def pick(self): # <3>
27+
try:
28+
return self._items.pop()
29+
except IndexError:
30+
raise LookupError('pick from empty BingoCage') # <4>
31+
32+
def __call__(self): # <5>
33+
return self.pick()
2934

3035
# END BINGO
File renamed without changes.

07-closure-deco/clockdeco_cls.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# clockdeco_param.py
2+
3+
"""
4+
>>> snooze(.1) # doctest: +ELLIPSIS
5+
[0.101...s] snooze(0.1) -> None
6+
>>> clock('{name}: {elapsed}')(time.sleep)(.2) # doctest: +ELLIPSIS
7+
sleep: 0.20...
8+
>>> clock('{name}({args}) dt={elapsed:0.3f}s')(time.sleep)(.2)
9+
sleep(0.2) dt=0.201s
10+
"""
11+
12+
# BEGIN CLOCKDECO_CLS
13+
import time
14+
15+
DEFAULT_FMT = '[{elapsed:0.8f}s] {name}({args}) -> {result}'
16+
17+
class clock:
18+
19+
def __init__(self, fmt=DEFAULT_FMT):
20+
self.fmt = fmt
21+
22+
def __call__(self, func):
23+
def clocked(*_args):
24+
t0 = time.time()
25+
_result = func(*_args)
26+
elapsed = time.time() - t0
27+
name = func.__name__
28+
args = ', '.join(repr(arg) for arg in _args)
29+
result = repr(_result)
30+
print(self.fmt.format(**locals()))
31+
return _result
32+
return clocked
33+
34+
if __name__ == '__main__':
35+
36+
@clock()
37+
def snooze(seconds):
38+
time.sleep(seconds)
39+
40+
for i in range(3):
41+
snooze(.123)
42+
43+
# END CLOCKDECO_CLS

11-iface-abc/bingo.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# BEGIN TOMBOLA_BINGO
2+
13
import random
24

35
from tombola import Tombola
@@ -6,14 +8,21 @@
68
class BingoCage(Tombola): # <1>
79

810
def __init__(self, items):
9-
self._balls = list(items) # <2>
11+
self._randomizer = random.SystemRandom() # <2>
12+
self._items = []
13+
self.load(items) # <3>
1014

1115
def load(self, items):
12-
self._balls.extend(items)
16+
self._items.extend(items)
17+
self._randomizer.shuffle(self._items) # <4>
1318

14-
def pick(self):
19+
def pick(self): # <5>
1520
try:
16-
position = random.randrange(len(self._balls)) # <3>
17-
except ValueError:
18-
raise LookupError('pop from empty BingoCage')
19-
return self._balls.pop(position) # <4>
21+
return self._items.pop()
22+
except IndexError:
23+
raise LookupError('pick from empty BingoCage')
24+
25+
def __call__(self): # <7>
26+
self.pick()
27+
28+
# END TOMBOLA_BINGO
File renamed without changes.

11-iface-abc/lotto.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# BEGIN LOTTERY_BLOWER
2+
13
import random
24

35
from tombola import Tombola
@@ -6,19 +8,23 @@
68
class LotteryBlower(Tombola):
79

810
def __init__(self, iterable):
9-
self.randomizer = random.SystemRandom() # <1>
10-
self.clear()
11-
self.load(iterable)
12-
13-
def clear(self):
14-
self._balls = []
11+
self._balls = list(iterable) # <1>
1512

1613
def load(self, iterable):
1714
self._balls.extend(iterable)
18-
self.randomizer.shuffle(self._balls) # <2>
1915

2016
def pick(self):
21-
return self._balls.pop() # <3>
17+
try:
18+
position = random.randrange(len(self._balls)) # <2>
19+
except ValueError:
20+
raise LookupError('pick from empty BingoCage')
21+
return self._balls.pop(position) # <3>
2222

2323
def loaded(self): # <4>
24-
return len(self._balls) > 0
24+
return bool(self._balls)
25+
26+
def inspect(self): # <5>
27+
return tuple(sorted(self._balls))
28+
29+
30+
# END LOTTERY_BLOWER

0 commit comments

Comments
 (0)