def need_ingredients(self): print "SELF.IN: ",self.Ingredients missing = [] stock = dict() #contains key: (mfg,liquor,typ) value: amount #First: fill a dict with our stock that matches the liquor type for i in self.Ingredients: for item in db.check_inventory_for_type(i[0]): #returns mfg/liquor tuples with liquor == {ingredient name} #fetches from the inventory the amount db._c.execute("SELECT amount FROM inventory WHERE \ mfg = ? AND liquor = ?",(item[1],item[2])) amount = int(db._c.fetchone()[0]) if item in stock.keys(): #add to temp dictionary stock[(item[2],item[3])] += float(amount) else: stock[(item[2],item[3])] = float(amount) if len(stock) == 0: #no ingredients of that type(s) in db for i in self.Ingredients: amount = db.convert_to_ml(i[1]) missing.append((i[0],amount)) return missing #next: check to see if we have bottles with enough liquor for each ingredient in the recipe (to refrain from mixing bottles) for i in self.Ingredients: needed = db.convert_to_ml(i[1]) #print "\nWe need %s ml of %s" % (needed, i[0]) most = 0 #greatest amount per bottle, overwritable flag = False #False implies not enough liquor in any given bottle to satisfy the ingredient amount for item, amt in stock.items(): #print "looking for greatest amount: %s -- %s?" % (item, amt) print item[1], i[0] if str(item[1]) == str(i[0]): if amt >= needed: flag = True #print " greater than needed!" if amt > most: most = stock[item] #print " new greatest amount!" if flag == False: miss = i[0],(needed-most) missing.append(miss) if len(missing) != 0: return missing else: return False
def need_ingredients(self): needed = [] #loop through ingredients for (typ, needed_amount) in self.ingredients: needed_amount = db.convert_to_ml(needed_amount) #check supply supply = db.check_inventory_for_type(typ) print supply if supply: total_amount = 0 for m,l,_ in supply: total_amount = db.get_liquor_amount(m,l) if needed_amount - total_amount > 0: #we don't have enough :( needed_amount = needed_amount - total_amount else: continue needed.append((typ, needed_amount)) if needed == []: return False else: return needed
def recv(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) amountToConvert = results['liquorAmount'][0] amountConverted = db.convert_to_ml(amountToConvert) amountConverted = str(amountConverted) + ' ml' content_type = 'text/html' data = """ <!DOCTYPE HTML> <html> <head> <title>Conversion Results</title> <style type='text/css'> h1 {text-decoration:underline; text-align:center; color:red;} body { font-size:14px; } </style> </head> <body> <h1>Conversion Results</h1><br/> <p> """ data += "Original amount: %s; Amount converted to mL: %s. <br/><br/><a href='./'>return to index</a>" % (amountToConvert, amountConverted) data += "</p></body></html>" start_response('200 OK', list(html_headers)) return [data]
def need_ingredients(self): notFound = [] for typ, amount in self.ingredients: ingNeeded = db.convert_to_ml(amount) print type(ingNeeded) #print ingNeeded ingInInventory = db.check_inventory_for_type(typ) print type(ingInInventory) print typ, amount, ingInInventory, ingNeeded if float(ingInInventory) <= float(ingNeeded): ingMissing = float(ingNeeded) - float(ingInInventory) missingTup = (typ, ingMissing) notFound.append(missingTup) else: continue return notFound
def recv(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) amount = results['amount'][0] units = results['units'][0] content_type = 'text/html' ml = db.convert_to_ml(amount+units) data = """\ <html> <head> <title>Converted to ml</title> <style type='text/css'> h1 {color:red} body { font-size: 14px; } </style> </head> <body> <h1>Converted</h1> """ data += "Amount: %s %s to ml: %s" % (amount, units, ml) data += """ <p> <a href='./'>return to index</a> </body> </html> """ start_response('200 OK', list(html_headers)) return [data]
def __init__(self, name, ingredients): self._name = name self._ingredients = [] for (typ, amount) in ingredients: amount = db.convert_to_ml(amount) self._ingredients.append((typ, amount))
def need_ingredients(self): missing = [] for ingr in self.ingredients: typ, amount = ingr available = db.get_largest_liquor_type_amount(typ) needed = db.convert_to_ml(amount) if available < needed: missing.append((typ, needed - available)) return missing
def do_convert(self, environ, start_response): start_response("200 OK", list(html_headers)) formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) amount = results['amount'][0] amount = db.convert_to_ml(amount) x = ["Amount is: %s ml" % (amount,)] return x
def recvAmount(self, environ, start_response): formdata = environ["QUERY_STRING"] results = urlparse.parse_qs(formdata) amount = results["amount"][0] amount = str(db.convert_to_ml(amount)) content_type = "text/html" data = "<font size = +4>The amount converted to ml = %s ml</font><p><a href='./'>GO TO HOME PAGE</a>" % (amount) start_response("200 OK", list(html_headers)) return [data]
def convert_result(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) ml_amount = db.convert_to_ml(results['amount'][0]) content_type = 'text/html' data = "Amount entered: %s:" % (str(ml_amount)+" ml") #data = data + printMenu() data = self.buildHtmlPage("The Witch Has Spoken","",data) start_response('200 OK', list(html_headers)) return [data]
def convert_result(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) amount = results['amount'][0] unit = results['unit'][0] ml = db.convert_to_ml(amount + " " + unit) content_type = 'text/html' data = "<a>After unit conversion: %s ml</a> <a href='./'><p> return to index</a>" % (ml) if amount != 0 and ml ==0: data = "<a>Wrong format<a> <a href='./'><p> return to index</a>" start_response('200 OK', list(html_headers)) return [data]
def need_ingredients(self): missing = [] for (typ,need) in self.ingredients: database_liquors = db.check_for_type(typ) amt = db.convert_to_ml(need) liq_max = 0.0 for (m,l) in database_liquors: liq_amt = db.get_liquor_amount(m,l) if(liq_amt > liq_max): liq_max = liq_amt if(liq_max < amt): necesito = amt - liq_max missing.append((typ,necesito)) return missing
def recvAmount(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) amount = results['amount'][0] amount = str(db.convert_to_ml(amount)) content_type = 'text/html' data = "Converted Amount %s ml<p><a href='./'>return to index</a>" % (amount) start_response('200 OK', list(html_headers)) return [data]
def need_ingredients(self): myList = list() for currentIngredient in self._myIngredients: listOfMandLTuples = db.check_inventory_for_type(currentIngredient[0]) amountInStock = 0 for myTuple in listOfMandLTuples: val = db.get_liquor_amount(myTuple[0],myTuple[1]) if val>amountInStock: amountInStock = val amountInDebt = amountInStock - db.convert_to_ml(currentIngredient[1]) if ( amountInDebt < 0 ): myList.append((currentIngredient[0],amountInDebt*-1.)) return myList
def need_ingredients(self): missingList = list() for ingredient in self._ingredients: #go through current ingredients listOfTuples = db.check_inventory_for_type(ingredient[0]) stockAmount = 0 for tuple in listOfTuples: amt = db.get_liquor_amount(tuple[0],tuple[1]) if amt>stockAmount: stockAmount = amt debtAmount = stockAmount - db.convert_to_ml(ingredient[1]) if ( debtAmount < 0 ): missingList.append((ingredient[0],debtAmount*-1.)) return missingList
def need_ingredients(self): missing_list = [] # PUT ALL THE LIQUOR TYPES NEEDED IN A LIST(see if they exist) =========== bottle_type_list = [] for i in self.ingredients: for item in db._bottle_types_db: if i[0] == item[2]: bottle_type_list.append(item) #print bottle_type_list, "bottle_type_list" #========================================================================= # CHECK INVENTORY FOR THOSE LIQUORS(put them in a list of tuples)========= amounts_list = [] for i in bottle_type_list: if db.check_inventory(i[0], i[1]): amount = (i[0], i[2], db.get_liquor_amount(i[0], i[1])) amounts_list.append(amount) #print amounts_list, "amounts_list" # CREATE THE MISSING LIST================================================= for i in self.ingredients: amount = 0.0 for item in amounts_list: #replace smaller amount with larger if i[0]==item[1]: if amount < float(item[2]): amount = float(item[2]) ing_amount = db.convert_to_ml(i[1])#convert the ingredient to ml if float(amount) < float(ing_amount):#compare the amount with ing needed = float(ing_amount)-float(amount) needed_tup = (i[0], needed) missing_list.append(needed_tup)#add to missing list return missing_list
def need_ingredients(self): need = [] amount = 0 for i in self.ingredients: typehave = db.check_inventory_for_type(i[0]) most = 0 for (m, l) in typehave: if db.get_liquor_amount(m, l) > most: amount = db.get_liquor_amount(m, l) most = amount amount = db.convert_to_ml(i[1])-amount if amount > 0: need.append((i[0], amount)) return need
def recv_convert(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) amount = results['amount'][0] content = "%s Converted to mL is: %s. <br/><a href='/'>Return to Index</a>\ <a href='/convert'>Convert Another</a>"\ % (amount, db.convert_to_ml(str(amount))) vars = dict(content = content, title='Conversion Results') data = JinjaLoader('blank.html',vars) start_response('200 OK', list(html_headers)) return data
def need_ingredients(self): # the list we're hoping to return missing = [] found = False # go through the ingredients for ing in self.ingredients: found = False # make a tuple to be added eventually need = (ing[0], db.convert_to_ml(ing[1])) original_needed_amount = need[1] # ignore this for a while, it will come in handy soon # now compare the ingredient type to the types we hve for type in db.get_bottle_types(): # if we know such type exists and that type is in our inventory (by a mfg and a liquor) if (type[2] == need[0]) and db.check_inventory(type[0], type[1]): # print "checking "+type[2]+" with mfg= "+type[0]+ " with liquor "+type[1] # see how much liquor is available by that particular mfg and liquor available_amount = db.get_liquor_amount(type[0], type[1]) # if we have more than or equal amount of that liquor from that particular mfg and liquor if available_amount >= original_needed_amount: # print "found it :)" # then we're done here, let's move on to the next ingredient (break out of the current/types loop) found = True break else: # if the amount is not enough # how much is missing? (difference between what we need and what we have) difference_amount = original_needed_amount - available_amount # we will try to find the mfg and liquor with the minimum missing amount. Otherwise, just leave it alone. # I know I could've used min() but this will make thigns look simpler if difference_amount < need[1]: # print "we will replace the current "+str(need[1])+" with the new differnece: "+str(difference_amount) need = (need[0], difference_amount) # else: # print "we will not replace "+str(need[1])+" with the new difference "+str(difference_amount) if not found: missing.append(need) return missing
def recv(self, environ, start_response): title = "forum"; formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) num = results['amount'][0] unit = results['unit'][0] if num.replace('.','',1).isdigit(): amount = num + ' '+ unit r = db.convert_to_ml(amount) content_type = 'text/html' data = begining%(title,title) + "Amount: %.3f ml. <a href='./'>return to index</a>" %(r)+ end else: data = "Please insert a valid value, which is a number. <a href='./'>return to index</a>" start_response('200 OK', list(html_headers)) return [data]
def need_ingredients(self): """ This methode takes in the recipe and returns how many components are needed to complete the recipe. It returns the components in the form of a list of 2-tuples""" needed = [] for t, amount in self.comp: in_inventory = db.check_inventory_for_type(t) required = db.convert_to_ml(amount) if in_inventory < required: print required print in_inventory needed.append((t,required - in_inventory)) else: continue return needed
def need_ingredients(self): needed = [] for (generic_type, amount) in self.ingredients: matching = db.check_inventory_for_type(generic_type) max_m = '' max_l = '' max_amount = 0.0 for (m, l, t) in matching: if t > max_amount: max_amount = t amount_needed = db.convert_to_ml(amount) if max_amount < amount_needed: needed.append((generic_type, amount_needed - max_amount)) return needed
def show_ml_convert(self, environ, start_response): vars = {} if environ['REQUEST_METHOD'].endswith('POST'): body = None if environ.get('CONTENT_LENGTH'): length = int(environ['CONTENT_LENGTH']) body = environ['wsgi.input'].read(length) d = urlparse.parse_qs(body) try: amount = "%s %s" % (d['amount'][0], d['unit'][0]) converted = db.convert_to_ml(amount) vars['amount'] = amount vars['converted'] = converted except (KeyError, ValueError): vars['error'] = "Error processing form." data = self._render('convertresult.html', 'Convert to Milliliters Result', vars) start_response('200 OK', list(html_headers)) return [data]
def need_ingredients(self): Available = [] Needed = {} List = list() for i in self._ingredients: items = db.check_inventory_for_type(i[0]) #this is the amt we need in ml amt = db.convert_to_ml(i[1]) Needed[i[0]] = amt for x in items: amt_avl = db.get_liquor_amount(x[0], x[1]) #if the current amt_avl is the largest so far then remove it from the amount needed if (amt - amt_avl) < Needed[i[0]]: Needed[i[0]] = amt - amt_avl #The amount needed for the recipe will be less than 0 after this point if there is enough liquor available. So remove it from the Dictionary of needed ingredients if Needed[i[0]] <= 0.0: del Needed[i[0]] #put all the elements from the dictionary into a List for i in Needed: List.append( (i, Needed[i]) ) return List
def convert_all_the_things_recv(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) amount_to_convert = results['InputAmt'][0] converted_amt = db.convert_to_ml(amount_to_convert) content_type = 'text/html' data = """ <html> <head> <title>Converted!</title> <style type ="text/css"> h1{color:red;} </style> </head> <body>""" data += """<h1> Converted! </h1> <p>Amount to Convert: %s; Converted volume: %s mL. </p>""" % (amount_to_convert, converted_amt) data += generate_html.generate_menu() data += """ </body> </html> """ start_response('200 OK', list(html_headers)) return [data]
def convert_result(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) ml_amount = db.convert_to_ml(results['amount'][0]) content_type = 'text/html' data = "Amount entered: %s:" % (str(ml_amount)+" ml") data = data + """</br> </br> <h2> Menu </h2> <a href='/'>Home</a></br> <a href='recipes'>Recipes</a></br> <a href='inventory'>Inventory</a></br> <a href='liquor_types'>Liquor Types</a></br> <a href='convert_to_ml'>Convert Amount</a></br> <a href='recipe_input'>Add Recipe</a></br> <a href='type_form'>Add Bottle Type</a></br> <a href='inv_form'>Add to Inventory</a></br> """ data = self.buildHtmlPage("Converted!! What more do you want from me?!","",data) start_response('200 OK', list(html_headers)) return [data]
def rpc_convert_units_to_ml(self,amount): return db.convert_to_ml(amount)
def rpc_ConvertToMilliters(self, amount): return db.convert_to_ml(amount);
def rpc_convert_units_to_ml(self, amount): print "converting..." return "%s ml" % (db.convert_to_ml(amount))
def rpc_convert_units_to_ml(self, amount): return str(db.convert_to_ml(amount))+" ml"