-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from 4bitWise/dev
feat: generate recipes based on monthly budget
- Loading branch information
Showing
9 changed files
with
2,650 additions
and
1,766 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
""" [TODO] implement a script to calculate recipes costs and add theme to each recipes | ||
Returns the list of all recipes with all costs added | ||
""" | ||
import json | ||
import sys | ||
from typing import Any, Dict | ||
|
||
|
||
measureUnits = dict() | ||
ingredients = dict() | ||
recipes = dict() | ||
|
||
def readJsonFile(filepath: str) -> Dict: | ||
# Open and read the file | ||
with open(filepath, 'r', encoding='utf-8') as file: | ||
data = json.load(file) | ||
return data; | ||
|
||
def writeJsonFile(filepath: str, data: dict) -> None: | ||
# write data to file | ||
with open(filepath, 'w', encoding='utf-8') as file: | ||
json.dump(data, file, ensure_ascii=False, indent=4) | ||
|
||
def find_element_by_field_value(data: list[dict], field: str, value: Any) -> dict | None: | ||
for item in data: | ||
if item.get(field) == value: | ||
return item | ||
return None | ||
|
||
def convertToBaseUnit(qty: int, unit_id: str) -> int: | ||
unit = find_element_by_field_value(measureUnits, "id", unit_id) | ||
if (not unit['base_unit_id']): | ||
return qty | ||
else: | ||
return convertToBaseUnit(qty * unit['conversion_factor'], unit['base_unit_id']) | ||
|
||
def calculateRecipeCost(recipe: dict) -> int: | ||
totalCost = 0 | ||
for ingredientInfo in recipe['ingredients']: | ||
qty = ingredientInfo["quantity"] | ||
ingredientId = ingredientInfo['ingredient_id'] | ||
unitId = ingredientInfo["unit_id"] | ||
try: | ||
totalCost += find_element_by_field_value(ingredients, "id", ingredientId)['price'] * convertToBaseUnit(qty, unitId) | ||
except TypeError as e: | ||
# Handling the TypeError if the object is NoneType | ||
if str(e) == "'NoneType' object is not subscriptable": | ||
print("Error:") | ||
print(recipe["id"] + f" at Ingredient: {ingredientId}") | ||
raise | ||
else: | ||
print(e) | ||
sys.exit(1) | ||
return totalCost | ||
|
||
|
||
def addCostToRecipes() -> Dict: | ||
newRecipes = [] | ||
for recipe in recipes: | ||
try: | ||
cost = calculateRecipeCost(recipe) | ||
newRecipes.append({**recipe, "cost": cost}) | ||
except: | ||
None | ||
return (newRecipes) | ||
|
||
|
||
|
||
def main(av): | ||
global measureUnits, ingredients, recipes | ||
|
||
if (len(av) != 2): | ||
print(f"Usage {av[0]} path_to_new_file") | ||
sys.exit(0) | ||
measureUnits = readJsonFile("./data/measure-units.json") | ||
ingredients = readJsonFile("./data/ingredients.json") | ||
recipes = readJsonFile("./data/recipes.json") | ||
newRecipes = addCostToRecipes() | ||
writeJsonFile(sys.argv[1], newRecipes) | ||
return 0; | ||
|
||
|
||
if __name__ == "__main__": | ||
av = sys.argv | ||
sys.exit(main(av)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters