-2
PS D:\A. VS CODE Folder> & D:/python.exe "d:/A. VS CODE Folder/Python/# Import necessary libraries.py"
['vegetables', 'soy sauce', 'beef', 'coconut milk', 'curry paste', 'chicken', 'alfredo sauce', 'pasta']
Traceback (most recent call last):
  File "d:\A. VS CODE Folder\Python\# Import necessary libraries.py", line 68, in <module>
    plan.save("Meal_Plan.json")
  File "d:\A. VS CODE Folder\Python\# Import necessary libraries.py", line 45, in save    
    json.dump(data, f)
  File "D:\Lib\json\__init__.py", line 179, in dump
    for chunk in iterable:
  File "D:\Lib\json\encoder.py", line 432, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "D:\Lib\json\encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "D:\Lib\json\encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "D:\Lib\json\encoder.py", line 326, in _iterencode_list
    yield from chunks
  File "D:\Lib\json\encoder.py", line 439, in _iterencode
    o = _default(o)
        ^^^^^^^^^^^
  File "D:\Lib\json\encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Meal is not JSON serializable




# Import necessary libraries
import datetime
import json

# Define a "Meal" class to represent a single meal
class Meal:
  def __init__(self, name, ingredients):
    self.name = name
    self.ingredients = ingredients

# Define a "MealPlan" class to represent a weekly meal plan
class MealPlan:
  def __init__(self):
    self.days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    self.meals = {"Monday": [], "Tuesday": [], "Wednesday": [], "Thursday": [], "Friday": [], "Saturday": [], "Sunday": []}

  def add_meal(self, day, meal):
    self.meals[day].append(meal)

  def remove_meal(self, day, meal):
    self.meals[day].remove(meal)

  def get_grocery_list(self):
    # Create a set to store unique ingredients
    grocery_list = set()

    # Loop through each day of the week
    for day in self.days:
      # Loop through each meal on that day
      for meal in self.meals[day]:
        # Loop through each ingredient in the meal
        for ingredient in meal.ingredients:
          # Add the ingredient to the set
          grocery_list.add(ingredient)

    # Convert the set to a list and return it
    return list(grocery_list)

  def save(self, filename):
    # Convert the meal plan to a dictionary
    data = {"days": self.days, "meals": self.meals}

    # Write the dictionary to a JSON file
    with open(filename, "w") as f:
      json.dump(data, f)

  def load(self, filename):
    # Read the data from the JSON file
    with open(filename, "r") as f:
      data = json.load(f)

    # Set the days and meals from the data
    self.days = data["days"]
    self.meals = data["meals"]

# Test the classes
plan = MealPlan()

# Add some meals to the plan
plan.add_meal("Monday", Meal("Chicken Alfredo", ["chicken", "pasta", "alfredo sauce"]))
plan.add_meal("Tuesday", Meal("Beef Stir Fry", ["beef", "vegetables", "soy sauce"]))
plan.add_meal("Wednesday", Meal("Vegetable Curry", ["coconut milk", "curry paste", "vegetables"]))

# Print the grocery list
print(plan.get_grocery_list())

# Save the meal plan
plan.save("Meal_Plan.json")

# Clear the plan
plan.meals = {"Monday": [], "Tuesday": [], "Wednesday": [], "Thursday": [], "Friday": [], "Saturday": [], "Sunday": []}

# Load the meal plan
plan.load("meal_plan.json")

# Print the loaded meal plan
print(plan.meals)
dm2
  • 4,053
  • 3
  • 17
  • 28

1 Answers1

-1

You can try importing the package

from django.core.serializers import serialize

Serialization can be used when your view interface returns.

return {"data": json.loads(serialize('json', you data))}
吕寅寅
  • 1
  • 1