update from Atlas with major reorg

This commit is contained in:
Luciano Ramalho
2015-04-17 21:29:30 -03:00
parent 57902d31b5
commit a786180239
134 changed files with 369 additions and 520 deletions

View File

@@ -1,3 +1,5 @@
# BEGIN TOMBOLA_BINGO
import random
from tombola import Tombola
@@ -6,14 +8,21 @@ from tombola import Tombola
class BingoCage(Tombola): # <1>
def __init__(self, items):
self._balls = list(items) # <2>
self._randomizer = random.SystemRandom() # <2>
self._items = []
self.load(items) # <3>
def load(self, items):
self._balls.extend(items)
self._items.extend(items)
self._randomizer.shuffle(self._items) # <4>
def pick(self):
def pick(self): # <5>
try:
position = random.randrange(len(self._balls)) # <3>
except ValueError:
raise LookupError('pop from empty BingoCage')
return self._balls.pop(position) # <4>
return self._items.pop()
except IndexError:
raise LookupError('pick from empty BingoCage')
def __call__(self): # <7>
self.pick()
# END TOMBOLA_BINGO

17
11-iface-abc/drum.py Normal file
View File

@@ -0,0 +1,17 @@
from random import shuffle
from tombola import Tombola
class TumblingDrum(Tombola):
def __init__(self, iterable):
self._balls = []
self.load(iterable)
def load(self, iterable):
self._balls.extend(iterable)
shuffle(self._balls)
def pick(self):
return self._balls.pop()

View File

@@ -1,3 +1,5 @@
# BEGIN LOTTERY_BLOWER
import random
from tombola import Tombola
@@ -6,19 +8,23 @@ from tombola import Tombola
class LotteryBlower(Tombola):
def __init__(self, iterable):
self.randomizer = random.SystemRandom() # <1>
self.clear()
self.load(iterable)
def clear(self):
self._balls = []
self._balls = list(iterable) # <1>
def load(self, iterable):
self._balls.extend(iterable)
self.randomizer.shuffle(self._balls) # <2>
def pick(self):
return self._balls.pop() # <3>
try:
position = random.randrange(len(self._balls)) # <2>
except ValueError:
raise LookupError('pick from empty BingoCage')
return self._balls.pop(position) # <3>
def loaded(self): # <4>
return len(self._balls) > 0
return bool(self._balls)
def inspect(self): # <5>
return tuple(sorted(self._balls))
# END LOTTERY_BLOWER

View File

@@ -1,3 +1,5 @@
# BEGIN TOMBOLA_ABC
import abc
class Tombola(abc.ABC): # <1>
@@ -14,10 +16,20 @@ class Tombola(abc.ABC): # <1>
"""
def loaded(self): # <4>
try:
item = self.pick()
except LookupError:
return False
else:
self.load([item]) # put it back
return True
"""Return `True` if there's at least 1 item, `False` otherwise."""
return bool(self.inspect()) # <5>
def inspect(self):
"""Return a sorted tuple with the items currently inside."""
items = []
while True: # <6>
try:
items.append(self.pick())
except LookupError:
break
self.load(items) # <7>
return tuple(sorted(items))
# END TOMBOLA_ABC

View File

@@ -11,6 +11,8 @@ Create and load instance from iterable::
>>> globe = ConcreteTombola(balls)
>>> globe.loaded()
True
>>> globe.inspect()
(0, 1, 2)
Pick and collect balls::
@@ -55,7 +57,7 @@ Load and pick 100 balls to verify that they all come out::
>>> balls = list(range(100))
>>> globe = ConcreteTombola(balls)
>>> picks = []
>>> while globe.loaded():
>>> while globe.inspect():
... picks.append(globe.pick())
>>> len(picks) == len(balls)
True
@@ -72,7 +74,7 @@ Check that the order has changed and is not simply reversed::
Note: the previous 2 tests have a *very* small chance of failing
even if the implementation is OK. The probability of the 100
balls coming out, by chance, in the order they were loaded is
balls coming out, by chance, in the order they were inspect is
1/100!, or approximately 1.07e-158. It's much easier to win the
Lotto or to become a billionaire working as a programmer.

View File

@@ -12,8 +12,12 @@ class TomboList(list): # <2>
else:
raise LookupError('pop from empty TomboList')
def load(self, iterable): self.extend(iterable) # <5>
load = list.extend # <5>
def loaded(self): return bool(self) # <6>
def loaded(self):
return bool(self) # <6>
# Tombola.register(TomboList) # <- Python 3.2 or earlier
def inspect(self):
return tuple(sorted(self))
# Tombola.register(TomboList) # <7>