Improved delete, scaling added
This commit is contained in:
203
recipe.py
203
recipe.py
@@ -3,7 +3,11 @@ Written by Lucas Jensen
|
||||
Portfolio Project for CS361
|
||||
The main logic behind a recipe
|
||||
"""
|
||||
|
||||
import json
|
||||
from pprint import pprint
|
||||
|
||||
JSON_FILE = "recipes/recipes.json"
|
||||
|
||||
|
||||
class RecipeBook:
|
||||
@@ -11,38 +15,29 @@ class RecipeBook:
|
||||
Represents a recipe book containing several bread recipes
|
||||
"""
|
||||
def __init__(self):
|
||||
self._recipes = self.__build_recipes()
|
||||
self._recipes = {} # a dictionary of recipe objects, ID as key
|
||||
self._id = 0
|
||||
self.__build_recipes()
|
||||
|
||||
@staticmethod
|
||||
def __build_recipes():
|
||||
def __build_recipes(self):
|
||||
"""
|
||||
Builds the recipe book from an existing json file
|
||||
:return:
|
||||
"""
|
||||
with open("recipes/recipes.json", "rt") as f:
|
||||
recipes = json.load(f)
|
||||
with open(JSON_FILE, 'rt') as f:
|
||||
recipes_dict = json.load(f)
|
||||
|
||||
return recipes
|
||||
for _id in recipes_dict:
|
||||
recipe_dict = recipes_dict[_id]
|
||||
recipe = Recipe(recipe_dict['name'], recipe_dict['quantity'])
|
||||
recipe.set_id(_id)
|
||||
ingredients = recipe_dict['ingredients']
|
||||
for ingredient in ingredients:
|
||||
recipe.add_ingredient(ingredient, ingredients[ingredient])
|
||||
recipe.add_ingredient(ingredient, ingredients[ingredient])
|
||||
self._recipes[_id] = recipe
|
||||
|
||||
def add_recipes(self, recipe: object) -> None:
|
||||
"""
|
||||
Adds a recipe to the book
|
||||
:param recipe: bread recipe object
|
||||
:return: nothing
|
||||
"""
|
||||
keys = open_json()
|
||||
count = 0
|
||||
|
||||
while str(count) in keys:
|
||||
count += 1
|
||||
|
||||
self._recipes[count] = recipe
|
||||
self.save(count, recipe)
|
||||
|
||||
def get_recipes(self) -> list:
|
||||
"""
|
||||
get the dict of recipes
|
||||
:return: the dict of recipes
|
||||
"""
|
||||
def get_recipes(self):
|
||||
return self._recipes
|
||||
|
||||
def find_by_id(self, _id: int) -> object:
|
||||
@@ -51,58 +46,83 @@ class RecipeBook:
|
||||
:param _id: int, the id of the recipe
|
||||
:return: recipe object
|
||||
"""
|
||||
return self._recipes[_id]
|
||||
return self._recipes[str(_id)]
|
||||
|
||||
def save(self, _id: int, recipe: object) -> None:
|
||||
def find_and_delete(self, _id):
|
||||
"""
|
||||
Saves a recipe to the JSON file
|
||||
:param _id: int
|
||||
:return: nothing
|
||||
"""
|
||||
self._recipes[_id] = recipe.get_recipe()
|
||||
|
||||
with open("recipes/recipes.json", "wt") as f:
|
||||
json.dump(self._recipes, f, indent=4)
|
||||
|
||||
def delete(self, _id: int) -> None:
|
||||
"""
|
||||
Deletes a recipes as found by its ID
|
||||
:param _id: int: id of the recipe
|
||||
Finds a recipe using its ID and deletes it
|
||||
:param _id: int of the recipe's ID
|
||||
:return: nothing
|
||||
"""
|
||||
del self._recipes[str(_id)]
|
||||
|
||||
with open("recipes/recipes.json", "wt") as f:
|
||||
json.dump(self._recipes, f, indent=4)
|
||||
# remove from JSON file
|
||||
with open(JSON_FILE, 'rt') as f:
|
||||
json_file = json.load(f)
|
||||
|
||||
pprint(json_file)
|
||||
|
||||
del json_file[str(_id)]
|
||||
|
||||
with open(JSON_FILE, 'wt') as f:
|
||||
json.dump(json_file, f, indent=4)
|
||||
|
||||
def add_recipe(self, recipe):
|
||||
"""
|
||||
appends a recipe object to the recipe book
|
||||
:param recipe: recipe object
|
||||
:return: the id of the newly stored recipe
|
||||
"""
|
||||
with open(JSON_FILE, 'rt') as f:
|
||||
keys = json.load(f)
|
||||
|
||||
while str(self._id) in keys:
|
||||
self._id += 1
|
||||
|
||||
recipe.set_id(self._id)
|
||||
saved_id = self._id
|
||||
self._recipes[str(self._id)] = recipe
|
||||
self._id += 1
|
||||
# pprint(self._recipes)
|
||||
self.save(recipe)
|
||||
|
||||
return saved_id
|
||||
|
||||
@staticmethod
|
||||
def save(recipe):
|
||||
"""
|
||||
appends a recipe dictionary to the JSON file
|
||||
:param recipe:
|
||||
:return:
|
||||
"""
|
||||
with open(JSON_FILE, 'rt') as f:
|
||||
recipe_dict = json.load(f)
|
||||
|
||||
recipe_dict[recipe.get_id()] = recipe.get_recipe()
|
||||
|
||||
with open(JSON_FILE, 'wt') as f:
|
||||
json.dump(recipe_dict, f, indent=4)
|
||||
|
||||
|
||||
class Recipe:
|
||||
"""
|
||||
Represents a bread recipe
|
||||
"""
|
||||
def __init__(self, name, num_loaves, ap_flour, water, salt):
|
||||
def __init__(self, name: str, num_loaves: int):
|
||||
self._name = name
|
||||
self._num_loaves = num_loaves
|
||||
self._ingredients = {
|
||||
'ap_flour': ap_flour,
|
||||
'water': water,
|
||||
'salt': salt
|
||||
}
|
||||
|
||||
def get_name(self) -> str:
|
||||
"""
|
||||
Gets the name of a recipe
|
||||
:return:
|
||||
"""
|
||||
return self._name
|
||||
self._ingredients = {}
|
||||
self._id = None
|
||||
|
||||
def get_recipe(self) -> dict:
|
||||
"""
|
||||
:return: returns the whole recipe as a dict, including quantity
|
||||
builds and returns the whole recipe as a single dictionary
|
||||
:return: dictionary
|
||||
"""
|
||||
recipe_dict = {
|
||||
'quantity': self._num_loaves,
|
||||
'name': self._name,
|
||||
# 'id': self._id,
|
||||
'ingredients': {}
|
||||
}
|
||||
|
||||
@@ -111,18 +131,21 @@ class Recipe:
|
||||
|
||||
return recipe_dict
|
||||
|
||||
def get_name(self) -> str:
|
||||
return self._name
|
||||
|
||||
def get_num_loaves(self) -> int:
|
||||
"""
|
||||
:return: int: number of loaves the recipe calls for
|
||||
"""
|
||||
return self._num_loaves
|
||||
|
||||
def get_ingredients(self) -> dict:
|
||||
"""
|
||||
:return: dictionary of all ingredients
|
||||
"""
|
||||
return self._ingredients
|
||||
|
||||
def get_id(self) -> int:
|
||||
return self._id
|
||||
|
||||
def set_id(self, _id):
|
||||
self._id = _id
|
||||
|
||||
def add_ingredient(self, name: str, mass: int) -> None:
|
||||
"""
|
||||
Adds an ingredient to the recipe. Mass in grams.
|
||||
@@ -132,54 +155,50 @@ class Recipe:
|
||||
"""
|
||||
self._ingredients[name] = mass
|
||||
|
||||
def scale(self, new_num: int) -> object:
|
||||
def scale(self, new_num) -> object:
|
||||
"""
|
||||
Scales the recipe by a given factor
|
||||
:param new_num: integer, the number of desired loaves
|
||||
:return: Recipe object, now scaled
|
||||
"""
|
||||
scale = new_num / self._num_loaves
|
||||
scale = int(new_num) / int(self._num_loaves)
|
||||
|
||||
ap_flour = self._ingredients['ap_flour'] * scale
|
||||
water = self._ingredients['water'] * scale
|
||||
salt = self._ingredients['salt'] * scale
|
||||
name = f"{self._name} * {scale}"
|
||||
|
||||
scaled_recipe = Recipe(name, new_num, ap_flour, water, salt)
|
||||
scaled_recipe = Recipe(f"{self._name} * {scale}", new_num)
|
||||
|
||||
for ingredient in self._ingredients:
|
||||
if ingredient not in ['ap_flour', 'water', 'salt']:
|
||||
mass = self._ingredients[ingredient] * scale
|
||||
scaled_recipe.add_ingredient(ingredient, mass)
|
||||
mass = self._ingredients[ingredient] * scale
|
||||
scaled_recipe.add_ingredient(ingredient, round(mass))
|
||||
|
||||
return scaled_recipe
|
||||
|
||||
|
||||
def default_recipes():
|
||||
recipe_1 = Recipe('Country Brown', 1, 600, 300, 13)
|
||||
recipe_2 = Recipe('Conventional White', 2, 1000, 700, 25)
|
||||
"""Adds some sample data"""
|
||||
recipe_1 = Recipe('Country Brown', 1)
|
||||
recipe_2 = Recipe('Conventional White', 2)
|
||||
|
||||
sample_recipes = [recipe_1, recipe_2]
|
||||
flour = 500
|
||||
water = 300
|
||||
salt = 12
|
||||
|
||||
for recipe in sample_recipes:
|
||||
recipe.add_ingredient("AP Flour", flour)
|
||||
flour += 500
|
||||
recipe.add_ingredient("Water", water)
|
||||
water += 300
|
||||
recipe.add_ingredient("salt", salt)
|
||||
salt += 12
|
||||
|
||||
recipe_3 = recipe_2.scale(4)
|
||||
|
||||
return [recipe_1, recipe_2, recipe_3]
|
||||
sample_recipes.append(recipe_3)
|
||||
|
||||
|
||||
def open_json():
|
||||
"""TODO"""
|
||||
with open('recipes/recipes.json') as f:
|
||||
data = json.load(f)
|
||||
|
||||
return data
|
||||
return sample_recipes
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
recipes = default_recipes()
|
||||
book = RecipeBook()
|
||||
for recipe in recipes:
|
||||
book.add_recipes(recipe)
|
||||
|
||||
# for recipe in book.get_recipes():
|
||||
# print(book.get_recipes()[recipe])
|
||||
#
|
||||
# rec = Recipe('sandwich loaf', 2, 1000, 700, 25)
|
||||
# rec.add_ingredient('ww flour', 500)
|
||||
# rec.save('recipes.json')
|
||||
book.add_recipe(recipe)
|
||||
|
||||
Reference in New Issue
Block a user