I am working on a small program that uses classes to generate a weekly menu of daily food logs. I am still new to python and would like some help with what I view to be the next step. I want to be able to load the program and have it check if the weekly menu class for the week already exists or create a new one if there is not a created class. Right now the program generates an empty week's worth of list when the foodManagement class is created. How do I import and export a created class so I can load it from file? Also how to i get the program to check if this file exist or create a new weekly menu? Any help in my next process or with the code would really help me. Thanks.
# Food Inventory
# A program that creates a weekly menu to help manage food inventory and meals
# 2010.02.04
import datetime
from datetime import date, timedelta
convertUnits = {"Teaspoon" : .1667, "Tablespoon" : .5, "Ounces" : 1, "Cup" : 8, "Pound" : 16, "Gallon" : 128}
mealTypes = ["Breakfast", "Lunch", "Dinner", "Snack", "Workout"]
class foodManagement(object):
def __init__(self):
self.todaysDate = datetime.date.today()
self.weekDate = self.getWeekStartDate()
self.weekMenu = weeklyMenu(self.weekDate)
def getWeekStartDate(self):
today = self.todaysDate
year = int(today.strftime("%Y"))
week = int(today.strftime("%W"))
d = date(year, 1, 1)
delta_days = d.isoweekday() - 1
delta_weeks = week
if year == d.isocalendar()[0]:
delta_weeks -= 1
delta = timedelta(days=-delta_days, weeks=delta_weeks)
return d + delta
# Class Structures
class weeklyMenu(object):
def __init__(self, startDate):
self.startDate = startDate
self.menuDayLog = []
self.getWeeklyMenu(self.startDate)
def getWeeklyMenu(self, startDate):
weekStart = datetime.date(startDate.year, startDate.month, startDate.day)
for number in range(7):
difference = datetime.timedelta(days = number)
date = weekStart + difference
self.menuDayLog.append(dailyMenu(date))
class dailyMenu(object):
def __init__(self, date):
self.date = date
self.menuLog = []
self.getDailyMenu()
def getDailyMenu(self):
for meal in mealTypes:
self.menuLog.append(Meal(meal))
class Meal(object):
def __init__(self, mealType):
self.mealType = mealType
self.mealLog = []
self.totalCals = self.getTotalCals()
self.totalCost = self.getTotalCost()
def getTotalCals(self):
total = 0
for ingredient in self.mealLog:
total += ingredient.servCals
return total
def getTotalCost(self):
total = 0
for ingredient in self.mealLog:
total += ingredient.servCost
return total