Skip to content

Commit 31275d7

Browse files
committed
Reformatted project to conform with PEP8 Standards.
Also changed formatted strings to use f-strings instead.
1 parent 5938e4b commit 31275d7

File tree

2 files changed

+80
-131
lines changed

2 files changed

+80
-131
lines changed

app.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
app = Flask(__name__)
77

8+
89
@app.route("/sms", methods=['GET', 'POST'])
910
def incoming_sms():
1011
"""Send a dynamic reply to an incoming text message"""
@@ -20,5 +21,6 @@ def incoming_sms():
2021

2122
return str(resp) if resp else "NA"
2223

24+
2325
if __name__ == "__main__":
24-
app.run(host= '127.0.0.1', port=5000, debug=True) # host= '0.0.0.0',
26+
app.run(host='127.0.0.1', port=5000, debug=True) # host= '0.0.0.0'

orderPizza.py

+77-130
Original file line numberDiff line numberDiff line change
@@ -2,110 +2,84 @@
22
from pizzapi import *
33
from credentials import *
44

5-
def decode_message(message):
65

7-
message = message.title().strip()
8-
common_phrases = ["Can I Have A", "Could I Have A" , "Could I Get A", "And", "Some"]
9-
for phrase in common_phrases:
6+
def decode_message(message):
7+
message = message.title().strip()
8+
common_phrases = ["Can I Have A", "Could I Have A", "Could I Get A", "And", "Some"]
9+
for phrase in common_phrases:
1010
message = message.replace(phrase, "")
11-
1211
items = message.split(",")
1312
return items
1413

1514

16-
def editDistanceDP(str1, str2, m, n):
15+
def edit_distance_dp(str1, str2, m, n):
1716
# Create a table to store results of subproblems
18-
dp = [[0 for x in range(n + 1)] for x in range(m + 1)]
19-
17+
dp = [[0 for x in range(n + 1)] for x in range(m + 1)]
2018
# Fill d[][] in bottom up manner
21-
for i in range(m + 1):
22-
for j in range(n + 1):
23-
if i == 0:
24-
dp[i][j] = j # Min. operations = j
25-
elif j == 0:
26-
dp[i][j] = i # Min. operations = i
27-
28-
29-
elif str1[i-1] == str2[j-1]:
30-
dp[i][j] = dp[i-1][j-1]
31-
32-
33-
else:
34-
dp[i][j] = 1 + min(dp[i][j-1], # Insert
35-
dp[i-1][j], # Remove
36-
dp[i-1][j-1]) # Replace
37-
38-
return dp[m][n]
19+
for i in range(m + 1):
20+
for j in range(n + 1):
21+
if i == 0:
22+
dp[i][j] = j # Min. operations = j
23+
elif j == 0:
24+
dp[i][j] = i # Min. operations = i
25+
elif str1[i - 1] == str2[j - 1]:
26+
dp[i][j] = dp[i - 1][j - 1]
27+
else:
28+
dp[i][j] = 1 + min(dp[i][j - 1], # Insert
29+
dp[i - 1][j], # Remove
30+
dp[i - 1][j - 1]) # Replace
31+
return dp[m][n]
3932

4033

4134
def get_names_preconfigured(user):
42-
4335
names_to_ids = {}
44-
4536
preconfigured = user.menu.__dict__["preconfigured"]
46-
47-
48-
49-
for item in range(len(preconfigured)):
50-
try:
37+
for item in range(len(preconfigured)):
38+
try:
5139
current = preconfigured[item].__dict__
5240
names_to_ids[current["name"]] = current["code"]
5341
except:
5442
print("ERROR")
55-
56-
5743
products = user.menu.__dict__["products"]
58-
59-
for item in range(len(products)):
60-
try:
44+
for item in range(len(products)):
45+
try:
6146
current = products[item].__dict__
6247
names_to_ids[current["name"]] = current["code"]
63-
except:
48+
except:
6449
print("ERROR1")
65-
6650
return names_to_ids
6751

6852

6953
def ids_to_variants(user):
70-
7154
ids_to_variants = {}
72-
7355
variants = user.menu.__dict__["variants"]
74-
75-
for item in variants:
76-
if variants[item]["ProductCode"] in ids_to_variants:
56+
for item in variants:
57+
if variants[item]["ProductCode"] in ids_to_variants:
7758
ids_to_variants[variants[item]["ProductCode"]].append(item)
78-
else:
59+
else:
7960
ids_to_variants[variants[item]["ProductCode"]] = [item]
80-
8161
return ids_to_variants
8262

8363

84-
85-
def item_to_ids(items, user):
64+
def item_to_ids(items, user):
8665
"""
8766
Small - 10" / 25 cm.
8867
Medium - 12" / 30 cm.
8968
Large - 14" / 35 cm.
9069
X-Large - 16" / 40 cm
9170
"""
9271
sizes = ["10", "12", "14", "16", "25", "30", "35", "40"]
93-
94-
if not items:
72+
if not items:
9573
return []
96-
9774
ids = []
98-
9975
names_to_id_product = get_names_preconfigured(user)
100-
101-
for item in items:
102-
for name, product_id in names_to_id_product.items():
76+
for item in items:
77+
for name, product_id in names_to_id_product.items():
10378
# CLEAN TO REMOVE SMALL MEDIUM LARGE, AND STRIP
10479
item = item.strip()
105-
106-
for size in sizes:
107-
if size in item:
108-
if size == "10" or size == "25":
80+
for size in sizes:
81+
if size in item:
82+
if size == "10" or size == "25":
10983
replace = "Small"
11084
elif size == "12" or size == "30":
11185
replace = "Medium"
@@ -114,70 +88,60 @@ def item_to_ids(items, user):
11488
elif size == "16" or size == "40":
11589
replace = "X-Large"
11690
item = item.replace(size + '"', replace).replace(size + "'", replace)
117-
118-
11991
# print(item, " | ", name, editDistanceDP(item, name, len(item), len(name)) / (len(name)))
120-
121-
if editDistanceDP(item, name, len(item), len(name)) / (len(name)) < .3 or editDistanceDP(item.replace("Pizza", ""), name.replace("Dipping ", ""), len(item.replace("Pizza", "")), len(name.replace("Dipping ", ""))) / (len(name)) < .1:
92+
if edit_distance_dp(item, name, len(item), len(name)) / (len(name)) < .3 or edit_distance_dp(
93+
item.replace("Pizza", ""), name.replace("Dipping ", ""), len(item.replace("Pizza", "")),
94+
len(name.replace("Dipping ", ""))) / (len(name)) < .1:
12295
ids.append(product_id)
12396
break
124-
125-
12697
final_ids = []
127-
128-
for id in ids:
129-
if "F_" in id:
98+
for id in ids:
99+
if "F_" in id:
130100
variants = ids_to_variants(user)
131101
replace = variants[id][0]
132102
if replace == "STJUDE":
133-
replace = "STJUDE10"
103+
replace = "STJUDE10"
134104
final_ids.append(replace)
135-
else:
105+
else:
136106
final_ids.append(id)
137-
138107
return final_ids
139-
140108
# order.add_item('P12IPAZA') # add a 12-inch pan pizza
141109
# order.add_item('MARINARA') # with an extra marinara cup
142110
# order.add_item('20BCOKE') # and a 20oz bottle of coke
143-
144111
return ['P12IPAZA', 'MARINARA', '20BCOKE']
145112

113+
146114
def intro():
147115
return "Hey! What would you like from Domino's?"
148116

117+
149118
def format_text_back(result):
150119
name = result["Order"]["FirstName"]
151120
price = result["Order"]["AmountsBreakdown"]["Customer"]
152-
delivery_location = result["Order"]["Address"]["Street"] + " " + result["Order"]["Address"]["City"] + ", " + result["Order"]["Address"]["Region"]
121+
delivery_location = result["Order"]["Address"]["Street"] + " " \
122+
+ result["Order"]["Address"]["City"] + ", " \
123+
+ result["Order"]["Address"]["Region"]
153124
delivery_location = "your house"
154-
155-
donation = None
156-
125+
donation = None
157126
products = result["Order"]["Products"]
158127
products_list = []
159-
for product in products:
128+
for product in products:
160129
if "Donation" not in product["Name"]:
161130
products_list.append(product["Name"])
162-
else:
131+
else:
163132
donation = product["Name"]
164-
165133
products_clean = ", ".join(products_list)
166134
products_clean = ', and'.join(products_clean.rsplit(',', 1))
167-
168-
message = "Sup, {0}. You will recieve {1} in 30 minutes at {2}. Make sure to tip the driver!".format(name, products_clean, delivery_location)
169-
if donation:
170-
message += " Thanks for the {0}, you rock.".format(donation)
171-
135+
message = f"Sup, {name}. You will receive {products_clean} in 30 minutes at {delivery_location}. " \
136+
"Make sure to tip the driver!"
137+
if donation:
138+
message += f" Thanks for the {donation}, you rock."
172139
print(price)
173140
return message
174141

175142

176-
177-
178-
class credentials:
143+
class Credentials:
179144
def __init__(self):
180-
181145
self.FIRST_NAME = FIRST_NAME
182146
self.LAST_NAME = LAST_NAME
183147
self.EMAIL = EMAIL
@@ -191,79 +155,62 @@ def __init__(self):
191155
self.EXPIRATION = EXPIRATION
192156
self.CVC = CVC
193157

194-
self.customer = Customer(self.FIRST_NAME, self.LAST_NAME, self.EMAIL, self.PHONE_NO )
158+
self.customer = Customer(self.FIRST_NAME, self.LAST_NAME, self.EMAIL, self.PHONE_NO)
195159
self.address = Address(self.STREET, self.CITY, self.STATE, self.ZIP_CODE)
196160
self.store = self.address.closest_store()
197161
self.card = PaymentObject(self.CARD_NO, self.EXPIRATION, self.CVC, self.ZIP_CODE)
198162
self.order = Order(self.store, self.customer, self.address)
199163
self.menu = self.store.get_menu()
200164

201165

202-
203166
def get_menu():
204-
user = credentials()
205-
206-
preconfigured = get_names_preconfigured(user)
167+
user = Credentials()
168+
preconfigured = get_names_preconfigured(user)
207169
variants = ids_to_variants(user)
208170
for key, val in preconfigured.items():
209-
if "F_" in val:
210-
if len(variants[val]) > 1:
171+
if "F_" in val:
172+
if len(variants[val]) > 1:
211173
preconfigured[key] = variants[val]
212-
else:
174+
else:
213175
preconfigured[key] = variants[val][0]
214-
215-
menu_items = ["Here is the menu!\nTo order with ProductCode write 'finalOrder:' followed by the codes on the left of the ':'\n"]
216-
for key, val in preconfigured.items():
176+
menu_items = ["Here is the menu!\n" \
177+
"To order with ProductCode write 'finalOrder:' followed by the codes on the left of the ':'\n"]
178+
for key, val in preconfigured.items():
217179
menu_items.append(key + " : " + str(val))
218-
219180
return "\n".join(menu_items)
220181

182+
221183
def app_options():
222184
help_menu_message = "THIS IS THE HELP MENU\nThese are your options"
223185
return help_menu_message
224186

225187

226-
227-
def order_pizza(textMessage):
228-
229-
if not textMessage: return "NO MESSAGE"
230-
188+
def order_pizza(textMessage):
189+
if not textMessage:
190+
return "NO MESSAGE"
231191
# STEP 1: Create User
232-
user = credentials()
233-
192+
user = Credentials()
234193
# STEP 2: DECODE MESSSAGE TO GET ORDER ITEMS AND ADD TO ORDER
235-
items = decode_message(textMessage) # pass back a list
236-
item_ids = item_to_ids(items, user) # pass back a list
237-
238-
if not item_ids and "finalOrder:" not in textMessage:
239-
return "Sorry we don't understand what you are asking for.\nTry again and separate each item with a comma (,)."
240-
241-
242-
for item in item_ids:
194+
items = decode_message(textMessage) # pass back a list
195+
item_ids = item_to_ids(items, user) # pass back a list
196+
if not item_ids and "finalOrder:" not in textMessage:
197+
return "Sorry we don't understand what you are asking for.\n" \
198+
"Try again and separate each item with a comma (,)."
199+
for item in item_ids:
243200
user.order.add_item(item)
244201
# user.order.add_item('STJUDE10')
245-
246-
247-
# STEP 3: ORDER THE PIZZA
248-
202+
# STEP 3: ORDER THE PIZZA
249203
# REAL PAY
250204
# order.place(card) #ONLY USE WHEN YOU WANT TO PAY
251205
# TEST PAY
252206
# result = user.order.place(user.card) #WHEN YOU WANT TO PAY
253-
result = user.order.pay_with(user.card) # USE FOR TEST
254-
255-
207+
result = user.order.pay_with(user.card) # USE FOR TEST
256208
# STEP 4: TEXT BACK CONFIRMATION TO THE USER
257209
confirmation_text = format_text_back(result)
258-
259210
return confirmation_text
260211

261212

262-
263-
264-
265-
if __name__ == "__main__":
213+
if __name__ == "__main__":
266214
textMessage = "Could I get a 12' Cheese Pan Pizza, Marinara Sauce, Coke, and St. Jude Donation"
267215
print(order_pizza(textMessage))
268216
print()
269-

0 commit comments

Comments
 (0)