Example #1
0
def edit_recipe_instructions():
    global selected_recipe
    while True:
        print("""
    Please enter your desired option
    0. Done
    1. Add to existing instructions
    2. Edit
    3. Re-arrange instructions (Moving)
    4. Delete
        """)
        user_input = get_number_input(0, 4)
        instructions = selected_recipe['instructions']
        if user_input == 0:
            break
        elif user_input == 1:
            add_instructions(selected_recipe)
            break
        elif user_input == 2:
            print('Enter the number of the instruction you want to edit:\n')
            edit_instruction_num = get_number_input(1, len(instructions))
            edit_instruction = get_string(
                'Re-enter the instruction here to edit:\n')
            del instructions[edit_instruction_num - 1]
            instructions.insert(edit_instruction_num - 1, edit_instruction)
            break
        elif user_input == 3:
            move_instruction(selected_recipe)
            break
        elif user_input == 4:
            print('Enter the number of the instruction you want to delete:\n')
            delete_num = get_number_input(1, len(instructions))
            del instructions[delete_num - 1]
            print('Instruction Deleted!')
Example #2
0
def edit_recipe_ingredients():
    global selected_recipe

    while True:
        print("""
    Please enter your desired option
    0. Done
    1. Add to existing ingredients
    2. Edit
    3. Delete
        """)
        user_input = get_number_input(0, 3)
        ingredients = selected_recipe['ingredients']
        if user_input == 0:
            break
        elif user_input == 1:
            add_ingredients(selected_recipe)
            break
        elif user_input == 2:
            edit_ingredients(ingredients)
            break
        elif user_input == 3:
            print('Enter the number of the ingredient you want to delete:\n')
            del_num = get_number_input(1, len(ingredients))
            del ingredients[del_num - 1]
            print('Ingredients Deleted!')
    def test_get_number_input(self):
        # single valid number
        with patch('builtins.input', side_effect='3'):
            result = get_number_input(2, 5)
            self.assertEqual(result, 3)

        # single valid number with space
        with patch('builtins.input', side_effect=' 4   '):
            result = get_number_input(2, 5)
            self.assertEqual(result, 4)

        # multiple inputs, return first valid one
        with patch('builtins.input', side_effect=['1', '6', '2', '5', '3']):
            result = get_number_input(2, 5)
            self.assertEqual(result, 2)
Example #4
0
def recipe_search_ingredients(recipes):
    while True:
        query = input('Enter your search term by ingredient: ').strip()
        if query == '':
            print('Search canceled')
            break
        matches = []
        for recipe in recipes:
            for ingredient in recipe['ingredients']:
                ingredient_name = ingredient[0]
                if query.lower() in ingredient_name:
                    matches.append(recipe)

        print(f'Found {len(matches)} match(es)!')

        if len(matches) == 0:
            if yes_no('Do you want to try a new search? '):
                continue
            else:
                return False
        food_ingredients = list(map(lambda x: x['name'], matches))

        print('0. Return to Main Menu')
        for i in range(0, len(food_ingredients)):
            print(f'{i + 1}. {food_ingredients[i]}\n')

        print(
            'Enter a number related to the food (match) you are interested: ')
        num_input = get_number_input(0, len(matches))
        if num_input == 0:
            print('Returning to main menu')
            return False
        else:
            selected_recipe = matches[num_input - 1]
            return selected_recipe
Example #5
0
def recipe_search(recipes):
    while True:
        query = input('Enter your search term: ').strip()
        if query == '':
            print('Search canceled')
            break

        matches = list(
            filter(lambda x: query.lower() in x['name'].lower(), recipes))

        print(f'Found {len(matches)} match(es)!')

        if len(matches) == 0:
            if yes_no('Do you want to try a new search? '):
                continue
            else:
                return False

        names = list(map(lambda x: x['name'], matches))

        print('0. Return to Main Menu')
        for i in range(0, len(names)):
            print(f'{i + 1}. {names[i]}\n')

        print(
            'Enter a number related to the food (match) you are interested: ')
        num_input = get_number_input(0, len(matches))
        if num_input == 0:
            print('Returning to main menu')
            return False
        else:
            selected_recipe = matches[num_input - 1]
            return selected_recipe
Example #6
0
def main():
    global recipes
    global selected_recipe
    recipes = load_recipes()
    print('WELCOME TO YOUR RECIPE BOOK!')

    while True:
        print(f'There Are Currently {len(recipes)} recipes')
        print("""
Please enter your desired option
0. Exit
1. Search for a Recipe
2. Add a New Recipe
3. Make changes to existing Recipe (Edit)
4. Delete Recipe
    """)
        user_input = get_number_input(0, 4)
        if user_input == 0:
            print('Thanks for your time. Bye!')
            break
        elif user_input == 1:
            print("""
Please enter your desired option:
Do you want to search by 
1. Food Name 
2. Ingredients\n""")
            search_term = get_number_input(1, 2)
            if search_term == 1:
                selected_recipe = recipe_search(recipes)
            elif search_term == 2:
                selected_recipe = recipe_search_ingredients(recipes)
            if selected_recipe:
                display_recipe(selected_recipe)
        elif user_input == 2:
            new_recipe = create_recipe()
            recipes.append(new_recipe)
            save_recipes(recipes)
        elif user_input == 3:
            edit_recipe(recipes)
        elif user_input == 4:
            delete_recipe(recipes)
def move_instruction(selected_recipe):
    instructions = selected_recipe['instructions']
    while True:
        print(
            'Enter the number of the instruction you want to move/re-arrange:\n'
        )
        move_instruction_num = get_number_input(1, len(instructions))
        print(
            'Enter the preferred position (number) you want to move the instruction to:\n'
        )
        position_instruction_num = get_number_input(1, len(instructions))
        instructions.insert(position_instruction_num - 1,
                            instructions.pop(move_instruction_num - 1))
        print('Instruction Moved!')
        move_instr_ans = yes_no("""Do you want to move another instruction?
                                                    \nPlease input Yes(y) or No(n): """
                                )
        if not move_instr_ans:
            break
        else:
            for i, instruction in enumerate(instructions):
                print(f'{i + 1}. {instruction}\n')
def add_instructions(selected_recipe):

    instructions = selected_recipe['instructions']
    while True:
        add_instruction = get_string(
            'Enter the instruction and the preferred position of this instruction below:\n'
        )
        print('Enter the number related to the instruction:\n')
        position_instruction = get_number_input(1, len(instructions) + 1)
        instructions.insert(position_instruction - 1, add_instruction)
        print('Instructions Added')
        instr_ans = yes_no("""Do you want to add another instruction?
                                \nPlease input Yes(y) or No(n): """)
        if not instr_ans:
            break
        else:
            for i, instruction in enumerate(instructions):
                print(f'{i + 1}. {instruction}\n')