forked from fluentpython/example-code-2e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbingo.py
More file actions
28 lines (19 loc) · 602 Bytes
/
bingo.py
File metadata and controls
28 lines (19 loc) · 602 Bytes
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
# tag::TOMBOLA_BINGO[]
import random
from tombola import Tombola
class BingoCage(Tombola): # <1>
def __init__(self, items):
self._randomizer = random.SystemRandom() # <2>
self._items = []
self.load(items) # <3>
def load(self, items):
self._items.extend(items)
self._randomizer.shuffle(self._items) # <4>
def pick(self): # <5>
try:
return self._items.pop()
except IndexError:
raise LookupError('pick from empty BingoCage')
def __call__(self): # <6>
self.pick()
# end::TOMBOLA_BINGO[]