-
Notifications
You must be signed in to change notification settings - Fork 0
/
fridge.py
199 lines (162 loc) · 7.53 KB
/
fridge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import datetime #date time functions
from ocr import *
class Fridge:
def __init__(self):
self.foods = []
#Values initialised for demonstration
self.categories = {"FRUITS": ["APPLE", "ORANGE", "BANANA", "Others"], "VEGETABLES": ["TOMATO", "POTATO", "CABBAGE", "SPINACH", "Others"], "MEAT": ["BEEF", "PORK", "CHICKEN", "Others"],
"DAIRY PRODUCTS": ["MILK", "CHOCOLATE MILK", "CHEESE", "Others"], "OTHERS": ["DRINKS", "CURRY", "Others"]}
self.items = {"APPLE": "FRUITS", "ORANGE": "FRUITS", "BANANA": "FRUITS", "TOMATO": "VEGETABLES", "POTATO": "VEGETABLES", "CABBAGE": "VEGETABLES",
"SPINACH": "VEGETABLES", "BEEF": "MEAT", "PORK": "MEAT", "CHICKEN": "MEAT", "MILK": "DAIRY PRODUCTS", "CHOCOLATE MILK": "DAIRY PRODUCTS",
"CHEESE": "DAIRY PRODUCTS", "DRINKS": "OTHERS"}
## GETTERS
def get_food(self):
return self.foods
def get_food_names(self):
return list(map(lambda x: x.get_name(), self.get_food()))
def get_expired(self):
return list(filter(lambda x: x.get_status(), self.get_food()))
def get_not_expired(self):
return list(filter(lambda x: not x.get_status(), self.get_food()))
def get_category(self, category):
#List out all the food belonging to a category
#This is for /add - when adding a food belonging to a category,
#it will pull out the past options as an option list
return self.categories[category]
def get_object_by_id(self, id):
for food in self.get_food():
if food.get_id() == id:
return food
## SETTERS
def add_entry_to_cat(self, name, category):
#Adds new names to existing categories
self.categories[category].insert(-1,name)
self.items[name] = category
def add_food(self, other):
#For /add command. Takes in a food object and adds it to the fridge
#Check if there are existing food with the same name as the added food
# if other.get_name() in self.get_food_names():
# existing = list(filter(lambda x: x.get_name() == other.get_name(),
# self.get_food()))
# # If yes, loop through all the food with the same name
# for food in existing:
# print("ALERT!")
# """
# INSERT CODE HERE - call a function that triggers an alert
# prompt if the previous entry had been consumed
# if yes: call remove_food
# """
# pass
self.foods.append(other)
def remove_food(self, id):
##For the /remove command. Takes in the id, removes item from fridge
food_obj = self.get_object_by_id(id)
self.foods.remove(food_obj)
def sort_by_expiry(self):
self.foods.sort(key = lambda x: x.get_expiry_date())
def add_bulk(self, lst):
res = []
for word in lst:
if word in self.items:
res.append((word, self.items[word]))
return res
def clear(self):
#For the /clear command - Resets the whole fridge (Probably need some confirmation
#prompt
self.foods.clear()
def print_by_category(self, category):
##For the /remove command - Returns a list that can be printed as options
filtered = list(filter(lambda x: x.get_category() == category, self.get_food()))
ids = list(map(lambda x: x.get_id(), filtered))
return ids
def daily_update(self):
text = ""
expired_food = list(filter(lambda x: x.get_status(), self.get_food()))
if expired_food: #If there is expired food
text += "The following food are expired:\nName of food - Expired by"
for food in self.get_expired():
text += (food.get_name() + " - " + str(-1 * food.get_remaining_days()) + "\n")
text += "\n"
expiring = list(filter(lambda x: 0 <= x.get_remaining_days() <= 2,
self.get_food())) #List of food that are not expired
if expiring: #If there are expiring things
text += "Following things are expiring within 2 days:\n"
text += "Name of food - Days before expiry\n"
for food in expiring:
text += (food.get_name() + " - " + str(food.get_remaining_days()) + "\n")
return text
def print_full_fridge(self):
if not self.get_food():
return "Fridge is empty"
## For the /display command - Shows all the food in the fridge
self.sort_by_expiry()
expired_food = self.get_expired() #List of expired food
text = ""
if expired_food: #If there is expired food
text += "The following food are expired:\nName of food - Expired by"
for food in self.get_expired():
text += (food.get_name() + " - " + str(-1 * food.get_remaining_days()) + "\n")
text += "\n"
not_expired = self.get_not_expired() #List of food that are not expired
if not_expired: #If there are non-expired food
text += "Remaining food:\n"
text += "Name of food - Days before expiry\n"
for food in self.get_not_expired():
text += (food.get_name() + " - " + str(food.get_remaining_days()) + "\n")
return text
class Food:
def __init__(self, name, expiry, category):
#Contains name, category, date_entered, date_expiring (based on expiry),
#days_left, expired_status (boolean T/F)
self.name = name
self.category = category
today = datetime.datetime.now() #Unformatted date
year = str(today.year)
month = str(today.month)
day = str(today.day)
hour = str(today.hour)
minute = today.minute
if minute < 10:
minute = "0" + str(minute)
else:
minute = str(minute)
self.id = f"{name} bought on {day}/{month}/{year[2:]} {hour}:{minute}"
self.start_date = today
expiry = today + datetime.timedelta(days = expiry)
year_expiry = expiry.year
month_expiry = expiry.month
day_expiry = expiry.day
self.expiry_date = datetime.datetime(year_expiry, month_expiry, day_expiry, 23, 59)
self.days_left = self.expiry_date - self.start_date #How many days left before expiry
self.alert_days = 2 #No of days ahead to warn about expiring food
self.expired = False #Is food expired?
def get_name(self):
return self.name
def get_expiry_date(self):
return self.expiry_date
def get_category(self):
return self.category
def get_id(self):
return self.id
def get_remaining_days(self):
#Need some way to active this command
today = datetime.datetime.now()
self.days_left = (self.get_expiry_date() - today).days
if self.days_left < 0:
self.expired = True
return self.days_left
def get_status(self):
return self.expired
##
##
##tomato = Food("TOMATO", 0, 'FRUITS')
##potato = Food("POTATO", 3, 'VEGETABLES')
##fridge = Fridge()
##fridge.add_food(tomato)
##fridge.add_food(potato)
##potato2 = Food("POTATO", 4, 'VEGETABLES')
##lettuce = Food("LETTUCE", 2, 'VEGETABLES')
##spinach = Food("SPINACH", 7, "VEGETABLES")
##fridge.add_food(potato2)
##fridge.add_food(lettuce)
##fridge.add_food(spinach)