-
Notifications
You must be signed in to change notification settings - Fork 0
/
bottles.py
93 lines (67 loc) · 1.93 KB
/
bottles.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class BottleVerse:
@classmethod
def lyrics(cls, number):
return cls(BottleNumber(number))._lyrics()
def __init__(self, bottle_number):
self._bottle_number = bottle_number
def _lyrics(self):
return (
f'{self._bottle_number} of milk on the wall, '.capitalize() +
f'{self._bottle_number} of milk.\n'
f'{self._bottle_number.action()}, '
f'{self._bottle_number.successor()} of milk on the wall.\n'
)
class CountdownSong:
def __init__(self, verse_template, max=999999, min=0):
self._max = max
self._min = min
self._verse_template = verse_template
def song(self):
return self.verses(self._max, self._min)
def verses(self, upper, lower):
return '\n'.join(self.verse(i) for i in reversed(range(lower, upper + 1)))
def verse(self, number):
return self._verse_template.lyrics(number)
class BottleNumber:
def __new__(cls, number):
match number:
case 0:
cls = BottleNumber0
case 1:
cls = BottleNumber1
case 6:
cls = BottleNumber6
case _:
cls = BottleNumber
return super().__new__(cls)
def __init__(self, number):
self._number = number
def __str__(self):
return f'{self.quantity()} {self.container()}'
def quantity(self):
return str(self._number)
def container(self):
return 'bottles'
def action(self):
return f'Take {self.pronoun()} down and pass it around'
def pronoun(self):
return 'one'
def successor(self):
return BottleNumber(self._number - 1)
class BottleNumber0(BottleNumber):
def quantity(self):
return 'no more'
def action(self):
return 'Go to the store and buy some more'
def successor(self):
return BottleNumber(99)
class BottleNumber1(BottleNumber):
def container(self):
return 'bottle'
def pronoun(self):
return 'it'
class BottleNumber6(BottleNumber):
def quantity(self):
return '1'
def container(self):
return 'six-pack'