forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.py
More file actions
143 lines (115 loc) · 4.57 KB
/
builder.py
File metadata and controls
143 lines (115 loc) · 4.57 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from enum import Enum
import time
PizzaProgress = Enum('PizzaProgress', 'queued preparation baking ready')
PizzaDough = Enum('PizzaDough', 'thin thick')
PizzaSauce = Enum('PizzaSauce', 'tomato creme_fraiche')
PizzaTopping = Enum('PizzaTopping',
'mozzarella double_mozzarella bacon ham mushrooms red_onion oregano')
STEP_DELAY = 3 # in seconds for the sake of the example
class Pizza:
def __init__(self, name):
self.name = name
self.dough = None
self.sauce = None
self.topping = []
def __str__(self):
return self.name
def prepare_dough(self, dough):
self.dough = dough
print(f'preparing the {self.dough.name} dough of your {self}...')
time.sleep(STEP_DELAY)
print(f'done with the {self.dough.name} dough')
class MargaritaBuilder:
def __init__(self):
self.pizza = Pizza('margarita')
self.progress = PizzaProgress.queued
self.baking_time = 5 # in seconds for the sake of the example
def prepare_dough(self):
self.progress = PizzaProgress.preparation
self.pizza.prepare_dough(PizzaDough.thin)
def add_sauce(self):
print('adding the tomato sauce to your margarita...')
self.pizza.sauce = PizzaSauce.tomato
time.sleep(STEP_DELAY)
print('done with the tomato sauce')
def add_topping(self):
topping_desc = 'double mozzarella, oregano'
topping_items = (PizzaTopping.double_mozzarella, PizzaTopping.oregano)
print(f'adding the topping ({topping_desc}) to your margarita')
self.pizza.topping.append([t for t in topping_items])
time.sleep(STEP_DELAY)
print(f'done with the topping ({topping_desc})')
def bake(self):
self.progress = PizzaProgress.baking
print(f'baking your margarita for {self.baking_time} seconds')
time.sleep(self.baking_time)
self.progress = PizzaProgress.ready
print('your margarita is ready')
class CreamyBaconBuilder:
def __init__(self):
self.pizza = Pizza('creamy bacon')
self.progress = PizzaProgress.queued
self.baking_time = 7 # in seconds for the sake of the example
def prepare_dough(self):
self.progress = PizzaProgress.preparation
self.pizza.prepare_dough(PizzaDough.thick)
def add_sauce(self):
print('adding the crème fraîche sauce to your creamy bacon')
self.pizza.sauce = PizzaSauce.creme_fraiche
time.sleep(STEP_DELAY)
print('done with the crème fraîche sauce')
def add_topping(self):
topping_desc = 'mozzarella, bacon, ham, mushrooms, red onion, oregano'
topping_items = (PizzaTopping.mozzarella,
PizzaTopping.bacon,
PizzaTopping.ham,
PizzaTopping.mushrooms,
PizzaTopping.red_onion,
PizzaTopping.oregano)
print(f'adding the topping ({topping_desc}) to your creamy bacon')
self.pizza.topping.append([t for t in topping_items])
time.sleep(STEP_DELAY)
print(f'done with the topping ({topping_desc})')
def bake(self):
self.progress = PizzaProgress.baking
print(f'baking your creamy bacon for {self.baking_time} seconds')
time.sleep(self.baking_time)
self.progress = PizzaProgress.ready
print('your creamy bacon is ready')
class Waiter:
def __init__(self):
self.builder = None
def construct_pizza(self, builder):
self.builder = builder
steps = (builder.prepare_dough,
builder.add_sauce,
builder.add_topping,
builder.bake)
[step() for step in steps]
@property
def pizza(self):
return self.builder.pizza
def validate_style(builders):
try:
input_msg = 'What pizza would you like, [m]argarita or [c]reamy bacon? '
pizza_style = input(input_msg)
builder = builders[pizza_style]()
valid_input = True
except KeyError:
error_msg = 'Sorry, only margarita (key m) and creamy bacon (key c) are available'
print(error_msg)
return (False, None)
return (True, builder)
def main():
builders = dict(m=MargaritaBuilder, c=CreamyBaconBuilder)
valid_input = False
while not valid_input:
valid_input, builder = validate_style(builders)
print()
waiter = Waiter()
waiter.construct_pizza(builder)
pizza = waiter.pizza
print()
print(f'Enjoy your {pizza}!')
if __name__ == '__main__':
main()