Python3

Python script: Recipe Organizer (with search and categorization)


# Recipe Organizer with search and categorization

class Recipe:
def __init__(self, name, ingredients, category):
self.name = name
self.ingredients = ingredients
self.category = category

class RecipeOrganizer:
def __init__(self):
self.recipes = []

def add_recipe(self, recipe):
self.recipes.append(recipe)

def search_by_name(self, name):
for recipe in self.recipes:
if recipe.name == name:
return recipe
return None

def search_by_ingredient(self, ingredient):
found_recipes = []
for recipe in self.recipes:
if ingredient in recipe.ingredients:
found_recipes.append(recipe)
return found_recipes

def get_recipes_by_category(self, category):
category_recipes = []
for recipe in self.recipes:
if recipe.category == category:
category_recipes.append(recipe)
return category_recipes

# Example usage
recipe1 = Recipe("Pasta Carbonara", ["pasta", "eggs", "bacon", "cheese"], "Italian")
recipe2 = Recipe("Chicken Stir Fry", ["chicken", "vegetables", "soy sauce"], "Asian")

organizer = RecipeOrganizer()
organizer.add_recipe(recipe1)
organizer.add_recipe(recipe2)

search_result = organizer.search_by_name("Pasta Carbonara")
print(search_result.name) # Output: Pasta Carbonara

ingredient_search_result = organizer.search_by_ingredient("chicken")
for recipe in ingredient_search_result:
print(recipe.name) # Output: Chicken Stir Fry

category_result = organizer.get_recipes_by_category("Italian")
for recipe in category_result:
print(recipe.name) # Output: Pasta Carbonara

In this Python script, we have defined two classes: `Recipe` and `RecipeOrganizer`. The `Recipe` class represents a recipe with attributes like name, ingredients, and category. The `RecipeOrganizer` class acts as a container for multiple recipes and provides methods to add recipes, search for recipes by name, ingredient, or category.

We demonstrate the usage of these classes by creating two recipe instances, adding them to an organizer, and performing search operations based on name, ingredient, and category.

When the script is executed, it will showcase the search functionality by finding recipes by name, ingredient, and category, based on the input provided.

This script can be expanded further by adding more advanced search options or saving recipes to a file for future reference.