HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Meal plan for the week

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
themealplanweekfor

Problem

So I made this program, where you can add a recipe which is saved as a json and you can get a plan for your meals and a grocerylist back.

```
"""Program that help you to decide what to eat for the week
You should start adding some Recipies and ingredience you need
for the Recipie. If you have at least 7 of them they will be
used to tell you what you could cook and give you a grocery list

Keywords are add_recipe and get_meals
"""
import sys
import getopt
import json
import glob
import random

class Usage(Exception):
def __init__(self, msg):
self.msg = msg

def main(argv=None):
"""Takes the Keywords add_recipe and get_meals and
calles the underlying functions.
"""
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "hamg", ["help", "add"
"get"])

except getopt.error as msg:
raise Usage(msg)

for o, args in opts:
print(args)
if o in ("-h", "--help"):
print('''Valid options ase -a to add a recipe and -m
to get meals and -g to get meals and grocerielist
''')
return 1
elif o in ("-a"):
save_recipe(add_recipe())
elif o in ("-m"):
recipes = get_recipes()
weekly_meals = return_shuffled_max_seven(recipes)
print_meals(weekly_meals)
elif o in ("-g"):
recipes = get_recipes()
weekly_meals = return_shuffled_max_seven(recipes)
print_meals(weekly_meals)
print_grocerylist(weekly_meals)
else:
raise Usage("No valid option")

except Usage as err:
print(sys.stderr, err.msg)
print(sys.stderr, "for help use --help")
return 2

def add_recipe():
""" Asks the user interactivly to input a rec

Solution

Your imports should, per the style guide, be in alphabetical order:

import getopt
import glob
import json
import random
import sys


Unless you have a particular reason/requirement for using getopt, you should strongly consider using argparse instead.

There doesn't seem to be much point to defining your own error, catching getopt.error and raising Usage - why not just omit the handling and allow the error from getopt to filter back up?

Rather than have a separate .recipe file for each recipe, why not keep them all in a master dictionary all_recipes = {name: recipe, ...} and json.dump that whole dictionary into a single file? You already return a dictionary from add_recipe, so you could:

all_recipes.update(add_recipe())


Rather than fix the name e.g.

def return_shuffled_max_seven(a_list)


make the number a parameter:

def return_shuffled(a_list, max_=7):


You could then call return_shuffled(some_list, None) to get all of the contents back. Also, note that it shuffles the argument list in-place then returns a new slice from that list - this is unconventional behaviour and should be clearly documented (indeed, more docstrings throughout would be helpful).

You have a lot of repetition on days of the week in print_meals, consider a simplification starting with:

for index, day in enumerate(['Monday', 'Tuesday', ...]):


Rather than:

grocery_list=[]
for sublists in recipe_list:
    grocery_list += sublists

print("Grocery List:\n")
grocery_list = list(set(grocery_list))
for item in grocery_list:
    print("    * "+item+"\n")


Make grocery_list a set from the start:

grocery_list = set()
for ingredients in recipe_list:
    grocery_list.update(ingredients)
print("Grocery List:\n")
for ingredient in grocery_list:
    print("    * {}\n".format(ingredient))


Note also use of str.format instead of concatenation.

Code Snippets

import getopt
import glob
import json
import random
import sys
all_recipes.update(add_recipe())
def return_shuffled_max_seven(a_list)
def return_shuffled(a_list, max_=7):
for index, day in enumerate(['Monday', 'Tuesday', ...]):

Context

StackExchange Code Review Q#69490, answer score: 3

Revisions (0)

No revisions yet.