forked from hassanshamim/Recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipes.py
152 lines (127 loc) · 4.52 KB
/
recipes.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import json
import os
class Recipe:
def __init__(self,
name,
servings=0,
total_time='',
ingredients=None,
directions=None):
self.name = name
self.servings = servings
self.total_time = total_time
self.ingredients = ingredients # list of ingredients, i.e.: ['soy sauce', 2, 'tablespoon']
self.directions = directions
def __iter__(self):
'''
Special method to allow us to convert our Recipe instances to a dictionary
by calling dict(recipe_object)
'''
keys = ['name', 'servings', 'total_time', 'ingredients', 'directions']
for key in keys:
yield key, getattr(self, key)
@classmethod
def load_recipes(cls):
"""
Returns a list of recipe objects saved in the recipe.json file.abs
If the file does not exist, returns an empty list.
"""
fname = 'recipes.json'
if not os.path.exists(fname):
return []
file = open(fname, 'r')
recipes_data = json.load(file)
file.close()
recipes = [cls(**data) for data in recipes_data]
return recipes
@classmethod
def save_recipes(cls, recipes):
"""
Takes a list of recipe object, converts them to json,
and saves them to a file called 'recipes.json'.abs
If recipes.json already exists, it replaces the file with the contents of
the recipes parameter.
"""
recipes = [dict(recipe) for recipe in recipes]
file = open('recipes.json', 'w')
json.dump(recipes, file, indent=4)
file.close()
@classmethod
def from_input(cls):
"""
Creates a new Recipe object from gathered input from the user
"""
print('Creating a new Recipe')
recipe_name = input("What is your recipe's name? ")
servings = int(input(' many does it serve? '))
total_time = input("How long does it take? i.e: 40 minutes ")
print('Please follow the prompt to list your ingredients')
print('When you are done, leave the name prompt blank and hit enter\n')
ingredients = []
while True:
name = input('Name of ingredient: ')
if not name:
break
amount = float(input('How much? Just a number please: '))
unit = input('And what is the unit of measurement? I.e. cups ')
ingredients.append([name, amount, unit])
print('\n\nNow list your directions')
print('Enter a blank line when you are done')
directions = []
while True:
line = input()
if not line:
break
directions.append(line)
return cls(
recipe_name,
servings=servings,
total_time=total_time,
ingredients=ingredients,
directions=directions)
def display(self):
print(self.name)
print('Servings:', self.servings)
print('Cooking time:', self.total_time)
print('Ingredients:'.center(40))
for ingredient in self.ingredients:
name, amount, unit = ingredient
name = name.ljust(25)
amounts = str(amount).ljust(10) + unit.ljust(15)
print(name + amounts)
print()
print('DIRECTIONS'.center(70))
for direction in self.directions:
print(direction)
print()
def list_recipes(recipes):
print('your recipes are:')
for i, recipe in enumerate(recipes):
print(f"{i} - {recipe.name}")
def display_recipe(recipes):
command = input()
try:
i = int(command)
recipes[i].display()
except ValueError:
pass
if __name__ == '__main__':
all_recipes = Recipe.load_recipes()
command = None
print('Hello and welcome to Recipe App')
while command != 'q':
# # display actions
print('Your options:')
print('q - exit this program')
print('1 - list recipes')
print('2 - add a recipe')
# prompt user for action
command = input('Choose an action\n')
if command == '1':
list_recipes(all_recipes)
print("Input a Recipes number to display it, or leave blank to return to the menu\n")
display_recipe(all_recipes)
elif command == '2': # add new recipe
new_recipe = Recipe.from_input()
all_recipes.append(new_recipe)
Recipe.save_recipes(all_recipes)