2
2
from pizzapi import *
3
3
from credentials import *
4
4
5
- def decode_message (message ):
6
5
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 :
10
10
message = message .replace (phrase , "" )
11
-
12
11
items = message .split ("," )
13
12
return items
14
13
15
14
16
- def editDistanceDP (str1 , str2 , m , n ):
15
+ def edit_distance_dp (str1 , str2 , m , n ):
17
16
# 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 )]
20
18
# 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 ]
39
32
40
33
41
34
def get_names_preconfigured (user ):
42
-
43
35
names_to_ids = {}
44
-
45
36
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 :
51
39
current = preconfigured [item ].__dict__
52
40
names_to_ids [current ["name" ]] = current ["code" ]
53
41
except :
54
42
print ("ERROR" )
55
-
56
-
57
43
products = user .menu .__dict__ ["products" ]
58
-
59
- for item in range (len (products )):
60
- try :
44
+ for item in range (len (products )):
45
+ try :
61
46
current = products [item ].__dict__
62
47
names_to_ids [current ["name" ]] = current ["code" ]
63
- except :
48
+ except :
64
49
print ("ERROR1" )
65
-
66
50
return names_to_ids
67
51
68
52
69
53
def ids_to_variants (user ):
70
-
71
54
ids_to_variants = {}
72
-
73
55
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 :
77
58
ids_to_variants [variants [item ]["ProductCode" ]].append (item )
78
- else :
59
+ else :
79
60
ids_to_variants [variants [item ]["ProductCode" ]] = [item ]
80
-
81
61
return ids_to_variants
82
62
83
63
84
-
85
- def item_to_ids (items , user ):
64
+ def item_to_ids (items , user ):
86
65
"""
87
66
Small - 10" / 25 cm.
88
67
Medium - 12" / 30 cm.
89
68
Large - 14" / 35 cm.
90
69
X-Large - 16" / 40 cm
91
70
"""
92
71
sizes = ["10" , "12" , "14" , "16" , "25" , "30" , "35" , "40" ]
93
-
94
- if not items :
72
+ if not items :
95
73
return []
96
-
97
74
ids = []
98
-
99
75
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 ():
103
78
# CLEAN TO REMOVE SMALL MEDIUM LARGE, AND STRIP
104
79
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" :
109
83
replace = "Small"
110
84
elif size == "12" or size == "30" :
111
85
replace = "Medium"
@@ -114,70 +88,60 @@ def item_to_ids(items, user):
114
88
elif size == "16" or size == "40" :
115
89
replace = "X-Large"
116
90
item = item .replace (size + '"' , replace ).replace (size + "'" , replace )
117
-
118
-
119
91
# 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 :
122
95
ids .append (product_id )
123
96
break
124
-
125
-
126
97
final_ids = []
127
-
128
- for id in ids :
129
- if "F_" in id :
98
+ for id in ids :
99
+ if "F_" in id :
130
100
variants = ids_to_variants (user )
131
101
replace = variants [id ][0 ]
132
102
if replace == "STJUDE" :
133
- replace = "STJUDE10"
103
+ replace = "STJUDE10"
134
104
final_ids .append (replace )
135
- else :
105
+ else :
136
106
final_ids .append (id )
137
-
138
107
return final_ids
139
-
140
108
# order.add_item('P12IPAZA') # add a 12-inch pan pizza
141
109
# order.add_item('MARINARA') # with an extra marinara cup
142
110
# order.add_item('20BCOKE') # and a 20oz bottle of coke
143
-
144
111
return ['P12IPAZA' , 'MARINARA' , '20BCOKE' ]
145
112
113
+
146
114
def intro ():
147
115
return "Hey! What would you like from Domino's?"
148
116
117
+
149
118
def format_text_back (result ):
150
119
name = result ["Order" ]["FirstName" ]
151
120
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" ]
153
124
delivery_location = "your house"
154
-
155
- donation = None
156
-
125
+ donation = None
157
126
products = result ["Order" ]["Products" ]
158
127
products_list = []
159
- for product in products :
128
+ for product in products :
160
129
if "Donation" not in product ["Name" ]:
161
130
products_list .append (product ["Name" ])
162
- else :
131
+ else :
163
132
donation = product ["Name" ]
164
-
165
133
products_clean = ", " .join (products_list )
166
134
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."
172
139
print (price )
173
140
return message
174
141
175
142
176
-
177
-
178
- class credentials :
143
+ class Credentials :
179
144
def __init__ (self ):
180
-
181
145
self .FIRST_NAME = FIRST_NAME
182
146
self .LAST_NAME = LAST_NAME
183
147
self .EMAIL = EMAIL
@@ -191,79 +155,62 @@ def __init__(self):
191
155
self .EXPIRATION = EXPIRATION
192
156
self .CVC = CVC
193
157
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 )
195
159
self .address = Address (self .STREET , self .CITY , self .STATE , self .ZIP_CODE )
196
160
self .store = self .address .closest_store ()
197
161
self .card = PaymentObject (self .CARD_NO , self .EXPIRATION , self .CVC , self .ZIP_CODE )
198
162
self .order = Order (self .store , self .customer , self .address )
199
163
self .menu = self .store .get_menu ()
200
164
201
165
202
-
203
166
def get_menu ():
204
- user = credentials ()
205
-
206
- preconfigured = get_names_preconfigured (user )
167
+ user = Credentials ()
168
+ preconfigured = get_names_preconfigured (user )
207
169
variants = ids_to_variants (user )
208
170
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 :
211
173
preconfigured [key ] = variants [val ]
212
- else :
174
+ else :
213
175
preconfigured [key ] = variants [val ][0 ]
214
-
215
- menu_items = [ "Here is the menu! \n To 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 ():
217
179
menu_items .append (key + " : " + str (val ))
218
-
219
180
return "\n " .join (menu_items )
220
181
182
+
221
183
def app_options ():
222
184
help_menu_message = "THIS IS THE HELP MENU\n These are your options"
223
185
return help_menu_message
224
186
225
187
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"
231
191
# STEP 1: Create User
232
- user = credentials ()
233
-
192
+ user = Credentials ()
234
193
# 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.\n Try 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 :
243
200
user .order .add_item (item )
244
201
# user.order.add_item('STJUDE10')
245
-
246
-
247
- # STEP 3: ORDER THE PIZZA
248
-
202
+ # STEP 3: ORDER THE PIZZA
249
203
# REAL PAY
250
204
# order.place(card) #ONLY USE WHEN YOU WANT TO PAY
251
205
# TEST PAY
252
206
# 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
256
208
# STEP 4: TEXT BACK CONFIRMATION TO THE USER
257
209
confirmation_text = format_text_back (result )
258
-
259
210
return confirmation_text
260
211
261
212
262
-
263
-
264
-
265
- if __name__ == "__main__" :
213
+ if __name__ == "__main__" :
266
214
textMessage = "Could I get a 12' Cheese Pan Pizza, Marinara Sauce, Coke, and St. Jude Donation"
267
215
print (order_pizza (textMessage ))
268
216
print ()
269
-
0 commit comments