# This is a coded version of a blog post, written in python.
# In it, I have authored some basic processes to help myself
# bring in a snack for class. Please feel free to comment
# with any analysis you might have of what I've chosen to
# include/exclude, as well as any additional suggestions.
# Here goes nothing! - Java
# And yes, as long as WordPress hasn't messed up my spacing, this all runs!
# Import the datetime library
from datetime import datetime
# Set some time variables that we'll need
snack_day = 1
snack_month = 10 # Date I was supposed to bring snacks: October 1st
now = datetime.now()
this_day = now.day
this_month = now.month
class_hour = 18 # 6 pm
this_hour = now.hour
this_minute = now.minute
# And the functions to check those variables
def checkDate(): # Check if it's the day you're supposed to bring snacks
if snack_day == this_day and snack_month == this_month:
print("You need to get snacks today.")
return True # Returning booleans to let me know if I need to proceed
else:
print("You don't need to get snacks today.")
return False
def checkTime(): # Check how many hours you have left to get snacks
hours = class_hour - this_hour - 1
minutes = 60 - this_minute
if this_hour < class_hour:
print("You have " + str(hours) + " hours and " + str(minutes) + " minutes to get snacks.")
return True
else:
print("You didn't bring snacks. Shaaaaaame...")
return False
# Ok, now that we can check our date and time, let's make a Food object with useful attributes
class Food(object):
def __init__(self, name, temperature, hasMeat, hasDairy, hasGluten, isTasty, hasBeenDone):
self.name = name # Give the food a name
self.temperature = temperature # Hot, Cold, or RoomTemp
self.hasMeat = hasMeat # True or False
self.hasDairy = hasDairy # True or False
self.hasGluten = hasGluten # True or False
self.isTasty = isTasty # True or False
self.hasBeenDone = hasBeenDone # True or False
def check(self):
if self.hasBeenDone: # First things first, check if that snack's been used already
print(self.name + " has been done before!")
return False # I'm returning booleans to make my looping easy later on
elif not self.isTasty: # Next, check if it's tasty
print("Nobody would want to eat " + self.name + "!")
return False
elif self.hasMeat: # Make sure that it meets peoples' dietary restrictions
print("Vegetarians can't eat " + self.name + "!")
return False
elif self.hasDairy:
print("Vegans can't eat " + self.name + "!")
return False
elif self.hasGluten:
print("Individuals with gluten intolerance can't eat " + self.name + "!")
return False
elif self.temperature != "RoomTemp": # Finally, check if it needs to be kept hot/cold
print("You would have to keep " + self.name + " " + self.temperature + "!")
return False
else: # Eureka!
print("OH MY GOD " + self.name + " WILL PROBABLY WORK!!!")
return True
# Now let's come up with some different food items...
chocolate = Food("Chocolate", "RoomTemp", False, True, False, True, True)
broccoli = Food("Broccoli", "Hot", False, False, False, False, False)
pitaChips = Food("Pita Chips", "RoomTemp", False, False, True, True, False)
pepperoniPizza = Food("Pepperoni Pizza", "Hot", True, True, True, True, False)
cake = Food("Cake", "RoomTemp", False, True, True, True, False)
sorbet = Food("Sorbet", "Cold", False, False, False, True, False)
tortillaChips = Food("Tortilla Chips", "RoomTemp", False, False, False, True, False)
cheeseburger = Food("Cheeseburger", "Hot", True, True, True, True, False)
# ...and put them in an ideas list
ideas = [chocolate, broccoli, pitaChips, pepperoniPizza, cake, sorbet, tortillaChips, cheeseburger]
# This last part is where the magic happens. Since my checkDate()
# and checkTime() functions return booleans as well as printing
# out their result, I can use them to run an if statement. Then I
# can iterate through my ideas list and run my check function, and
# once that returns True, exit the loop. This means I don't actually
# check the cheeseburger because I stop at tortillaChips.
if checkDate() and checkTime():
for idea in ideas:
if idea.check():
break
It’s a SNACK MACHINE!
What a delightful bit of code! I am reminded, tangentially, of this article I came across some years ago regarding food metaphors in computational language, and imagining the endless punning potential from there.
(Ah, here it is: Bründl, Monika E. 1999. Cookies, Strudels, and Easter Eggs – (Food) Metaphors in the Language of Computing).
Thank you! And that article is magical, particularly since I had no idea that @ could be referred to as strudel, vortex, whirlpool, cyclone, snail, ape, cat, rose, and cabbage…