This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingredient.py
78 lines (73 loc) · 3.29 KB
/
ingredient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class Ingredient:
field_names = ['?', 'Name', 'Menge', 'Kategorie', 'Gericht']
# Dont hardcode column number of "Name" because this number is used to filter for the ingredients with awk (s. ./main.py)
_name_col_num = field_names.index('Name') + 1
_padding = 15
_space_column_width = 3
def __init__(self, name, quantity, optional=False,
category='N/A-CATEGORY',
category_weight=0,
url='N/A-URL',
meal='N/A-MEAL'): # I hate it
self.name = name
self.quantity = str(quantity) # 2 (pieces), 250g, 1 Block => string necessary
self.optional = optional
self.category = category
# Category weight is assigned accoirding to order in supermarket (=> walk from
# category to category to be efficient).
# This key is also used for sorting the ingredients when generating the shopping list.
# 0 is used, when category is missing in the according csv file (2024-01-05: res/category_weight.csv)
self.category_weight = category_weight
self.url = url
self.meal = meal
def __str__(self):
return Ingredient.to_table_string([self.optional,
self.name,
self.quantity,
self.category,
self.meal])
# Also used for crafting a header for the table, hence class method and first argument as list
# (The field names are provided as list, attributes follow)
# Padding, to have a small neat table
@classmethod
def to_table_string(cls, attributes: list = field_names):
"""
Format elements (field names, ingredient) as string for shopping list.
"""
optional, name, quantity, category, meal = attributes
# Cap at _padding, no matter what (keep in mind when compsing a recipe)
pad = Ingredient._padding
scw = Ingredient._space_column_width
s = '?' if optional == '?' else '1' if optional else '•'
s = (' ' * scw).join((s, *(f"{attr[:pad]:<{pad}}"
for attr in [name,
quantity,
category])))
s = ' '.join((s, meal))
return s
# class Recipe:
# def __init__(self, title, servings, ingredients, instructions):
# self.title = title
# self.servings = servings
# self.ingredients = [Ingredient(**ingredient)
# for ingredient in ingredients]
# self.instructions = instructions
#
# def __str__(self):
# recipe_str = f"Recipe: {self.title}\nServings: {self.servings}\n\nIngredients:"
# for ingredient in self.ingredients:
# recipe_str += f"\n- {ingredient}"
#
# recipe_str += "\n\nInstructions:"
# for step in self.instructions:
# step_number = step.get("step", "N/A")
# description = step.get("description", "N/A")
# recipe_str += f"\n{step_number}. {description}"
#
# return recipe_str
#
#
# def read_recipe(file_path):
# with open(file_path, 'r') as file:
# recipe_data = yaml.safe_load(file)
# return Recipe(**recipe_data['recipe'])