2021-02-15 00:58:46 +01:00
|
|
|
# tag::TOMBOLA_ABC[]
|
|
|
|
|
|
|
|
import abc
|
|
|
|
|
|
|
|
class Tombola(abc.ABC): # <1>
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def load(self, iterable): # <2>
|
|
|
|
"""Add items from an iterable."""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def pick(self): # <3>
|
|
|
|
"""Remove item at random, returning it.
|
|
|
|
|
|
|
|
This method should raise `LookupError` when the instance is empty.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def loaded(self): # <4>
|
|
|
|
"""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>
|
2021-06-09 05:13:02 +02:00
|
|
|
return tuple(items)
|
2021-02-15 00:58:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
# end::TOMBOLA_ABC[]
|