def need_ingredients(self): amountInv = 0 amountNeeded = 0 listOfKeys = [] listOfTypes = [] listOfAmounts = [] listOfNeeded = [] # get list of keys for the types in inventory for (typ, amount) in self.needIngredients: for mfg, liquor in db.check_inventory_for_type(typ): listOfKeys.append((mfg, liquor)) listOfTypes.append((typ, listOfKeys)) listOfKeys = [] for (t, lk) in listOfTypes: amountInv = 0 for (m, l) in lk: if (m, l) in db._inventory_db or listOfTypes[1] == []: if db._inventory_db[(m, l)] > amountInv: amountInv = db._inventory_db[(m, l)] listOfAmounts.append((t, amountInv)) for (t, a) in self.needIngredients: for (typ, amount) in listOfAmounts: if t == typ: amountNeeded = float(convert.convert_to_ml(a)) - float(amount) if amountNeeded > 0: listOfNeeded.append((typ, amountNeeded)) return listOfNeeded
def recv(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) try: amount = results['amount'][0] except KeyError: amount = "0" try: amount = convert.convert_to_ml(amount) addin = "<p>Converted to ml: %s.</p>" % (amount) except AssertionError: addin = """\ <form action='recv'> Please enter amount to convert to ml: <input type='text' name='amount' size'20'> <input type='submit' value="Convert"> </form> <a href="#popupBasic" data-role="button" data-rel="popup">How to use?</a> <div data-role="popup" id="popupBasic"> <p>Possible inputs: 25ml 30 gallon 4 liter 9oz<p> </div> """ vars = dict(convert=addin) template = env.get_template("convert.html") data = template.render(vars) content_type = 'text/html' start_response('200 OK', list(html_headers)) return [str(data)]
def rpc_addrecipe(self,name,ingridients): try: name = name.strip() ing = ingridients.strip() templist = ing.split(',') sendoff = [] counter = 0 while counter< len(templist): temp1 = templist[counter].strip() temp2 = templist[counter+1].strip() finalamount = str(convert.convert_to_ml(temp2)) finalamount = finalamount + "ml" print temp1,finalamount sendoff.append((temp1,finalamount)) counter = counter + 2 r = recipes.Recipe(name, sendoff) try: db.add_recipe(r) addin= "Succesfully Added." except db.DuplicateRecipeName: addin= "There is already a recipe by this name." except (AssertionError, KeyError, IndexError) : addin = "Incorrect format or incomplete. Please try again." return addin
def test_convert_to_ml(): data = ["1 ml", "1 milliliter", "2 milliliters", "2 l", "1 l", "10 l", "10 oz", "1 ounce", "15 ounces", "2 g", "1 gallon", "5 gallons"] converted_data = [] for entry in data: converted = convert.convert_to_ml(entry) converted_data.append(converted) assert converted_data == [1.0, 1.0, 2.0, 2000.0, 1000.0, 10000.0, 295.735, 29.5735, 443.60249999999996, 7570.82, 3785.41, 18927.05] print converted_data
def add_to_inventory(mfg, liquor, amount): "Add the given liquor/amount to inventory." if not _check_bottle_type_exists(mfg, liquor): err = "Missing liquor: manufacturer '%s', name '%s'" % (mfg, liquor) raise LiquorMissing(err) amount = convert.convert_to_ml(amount) if check_inventory(mfg,liquor): db_inventory_update(mfg,liquor,amount) else: db_inventory_insert(mfg,liquor,amount)
def add_to_inventory(mfg, liquor, amount): "Add the given liquor/amount to inventory." if not _check_bottle_type_exists(mfg, liquor): err = "Missing liquor: manufacturer '%s', name '%s'" % (mfg, liquor) raise LiquorMissing(err) # just add it to the inventory database as a tuple, for now. if check_inventory(mfg,liquor): #try to gsee if it is in the inventory current = _inventory_db[(mfg, liquor)] #get current amount if it is else: current = 0 #if it isnt, adding to inventory, current amount is 0 pass add = convert.convert_to_ml(amount) #new amount to add #convert new amount finalamount = add+current #new total _inventory_db[(mfg, liquor)]=finalamount #put the final value back in
def add_to_inventory(mfg, liquor, amount): "Add the given liquor/amount to inventory." if not _check_bottle_type_exists(mfg, liquor): err = "Missing liquor: manufacturer '%s', name '%s'" % (mfg, liquor) raise LiquorMissing(err) if not check_inventory(mfg, liquor): # just add it to the inventory database as a tuple, for now. _inventory_db[(mfg, liquor)] = amount else: amount_to_add = convert.convert_to_ml(amount) old_amount = get_liquor_amount(mfg, liquor) new_amount = amount_to_add + old_amount amount_str = str(new_amount) + " ml" _inventory_db[(mfg, liquor)] = amount_str
def check_recipe_needs(ing): amtNeeded = convert.convert_to_ml(ing[1]) bottlesMatch = check_inventory_for_type(ing[0]) amtList = [] for bottle in bottlesMatch: amtList.append(get_liquor_amount(bottle[0],bottle[1])) amtList.sort() if len(amtList) > 0: currAmount = amtList[-1] if amtNeeded > currAmount: return (ing[0], (amtNeeded - currAmount)) else: return (ing[0], 0) else: return (ing[0], amtNeeded)
def get_liquor_amount(mfg, liquor): "Retrieve the total amount of any given liquor currently in inventory." amounts = [] found = False for ((m, l), amount) in _inventory_db.iteritems(): if mfg == m and liquor == l: found = True ml_amount = convert.convert_to_ml(amount) amounts.append(ml_amount) total = sum(amounts) return total if not found: err = "Missing liquor: manufacturer '%s', name '%s'" % (mfg, liquor) raise LiquorMissing(err)
def recv(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) if ( 'amount' in results.keys() ): amount = results['amount'][0] else: amount = '0 ml' amount = str(convert.convert_to_ml(amount)) content_type = 'text/html' data = "Converted Amount: %s ml<p><a href='./'>Index</a>" % amount start_response('200 OK', list(html_headers)) return [data]
def available_recipes(): recipeSet = set() add = True for recipe in _recipes_db: add = True for (typ, amount) in recipe.needIngredients: liquors = check_inventory_for_type(typ) # If we don't have that type in our inventory we can't make the recipe if not liquors: add = False break if get_liquor_type_amount(typ) < convert.convert_to_ml(amount): add = False break if add: recipeSet.add(recipe) return recipeSet
def need_ingredients(self): volume = 0 missing = [] for type, quantity in self.singridients: needed = convert.convert_to_ml(quantity) available = db.check_inventory_for_type(type) for (m,l) in available: vol = db.get_liquor_amount(m,l) if vol > volume: volume = vol if volume < needed: missing.append((type,needed - volume)) volume = 0 else: continue return missing
def recv_conversion(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) amount = results['amount'][0] unit = results['unit'][0] amount_converted = convert.convert_to_ml(amount + " " + unit) db._tmp_results = [] db._tmp_results.append(amount) db._tmp_results.append(unit) db._tmp_results.append(amount_converted) content_type = 'text/html' data = generate_html.generate_conversion_result_html() start_response('200 OK', list(html_headers)) return [data]
def add_to_inventory(mfg, liquor, amount): "Add the given liquor/amount to inventory." sql_pull_db() if not _check_bottle_type_exists(mfg, liquor): err = "Missing liquor: manufacturer '%s', name '%s'" % (mfg, liquor) raise LiquorMissing(err) amountTotal = convert.convert_to_ml(amount) # sql_add_inventory(mfg, liquor, amount+" ml") if (mfg, liquor) in _inventory_db: _inventory_db[(mfg, liquor)] += amountTotal sql_update_inventory(mfg, liquor, _inventory_db[(mfg, liquor)]) else: # _inventory_db[(mfg, liquor)] = amountTotal sql_insert_inventory(mfg, liquor, amountTotal) sql_pull_db()
def need_ingredients(self): types_needed = [] ingredients_needed = [] for (typ, amt) in self.ingredients: amt = convert.convert_to_ml(amt) brands_owned = db.check_inventory_for_type(typ) if len(brands_owned) == 0: types_needed.append((typ, amt)) else: amounts_from_brands = [] for (m, l) in brands_owned: amount_owned_of_brand = db.get_liquor_amount(m, l) amounts_from_brands.append(amount_owned_of_brand) if (amount_owned_of_brand <= amt): amount_needed = amt - amount_owned_of_brand ingredients_needed.append((typ, amount_needed)) for (type_needed, amount_needed) in types_needed: ingredients_needed.append((type_needed, amount_needed)) return ingredients_needed
def addrecipe(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) try: name = results['name'][0] ing = results['ing'][0] name = name.strip() ing = ing.strip() templist = ing.split(',') sendoff = [] counter = 0 while counter< len(templist): temp1 = templist[counter].strip() temp2 = templist[counter+1].strip() finalamount = str(convert.convert_to_ml(temp2)) finalamount = finalamount + "ml" print temp1,finalamount sendoff.append((temp1,finalamount)) counter = counter + 2 r = recipes.Recipe(name, sendoff) try: db.add_recipe(r) addin= "<p>Succesfully Added.</p>" except db.DuplicateRecipeName: addin= "<p>There is already a recipe by this name.</p>" except (AssertionError, KeyError, IndexError) : addin = """\ <p> Incorrect format or incomplete. Please try again.</p> """ vars = dict(form=addin) template = env.get_template("add.html") data = template.render(vars) content_type = 'text/html' start_response('200 OK', list(html_headers)) return [str(data)]
def rpc_convert_units_to_ml(self, amount): return convert.convert_to_ml(amount)
def rpc_convert_units_to_ml(self,amount): return str(convert.convert_to_ml(amount))+" ml"