Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question #1 Complete the get_food function for the food_utilities module. Use the following input format: Name: Origin 0 Canadian 1 Chinese 2 Indian : Vegetarian

Question #1 Complete the get_food function for the food_utilities module.

Use the following input format:

Name: Origin 0 Canadian 1 Chinese 2 Indian  : Vegetarian (Y/N): Calories: 

Test this function by printing out the resulting Food object.

food_utilities.py

from Food import Food

def get_food(): """ Question # 1 Creates a food object by requesting data from a user. Use: f = get_food() Returns: food - a completed food object (Food).

"""

Food.py

class Food: """ Defines an object for a single food: name, origin, vegetarian, calories. """ # Constants ORIGIN = ("Canadian", "Chinese", "Indian", "Ethiopian", "Mexican", "Greek", "Japanese", "Italian", "American", "Scottish", "New Zealand", "English")

@staticmethod def origins(): """ Creates a string list of food origins in the format: 0 Canadian 1 Chinese 2 Indian ... Use: s = Food.origins() Use: print(Food.origins()) Returns: string - A numbered list of valid food origins. """ string = ""

for i in range(len(Food.ORIGIN)): string += """{:2d} {} """.format(i, Food.ORIGIN[i]) return string

def __init__(self, name, origin, is_vegetarian, calories): """ Initialize a food object. Use: f = Food( name, origin, is_vegetarian, calories ) Parameters: name - food name (str) origin - food origin (int) is_vegetarian - whether food is vegetarian (boolean) calories - caloric content of food (int > 0) Returns: A new Food object (Food) """ assert origin in range(len(Food.ORIGIN)), "Invalid origin ID" assert is_vegetarian in (True, False, None), "Must be True or False" assert calories is None or calories >= 0, "Calories must be >= 0"

self.name = name self.origin = origin self.is_vegetarian = is_vegetarian self.calories = calories return

def __str__(self): """ Creates a formatted string of food data. Use: print(f) Use: s = str(f) Returns: string - the formatted contents of food (str) """ if self.calories is None: # is a key string = "{}, {}".format(self.name, Food.ORIGIN[self.origin]) else: # full data set string = """Name: {} Origin: {} Vegetarian: {} Calories: {:,d}""".format(self.name, Food.ORIGIN[self.origin], self.is_vegetarian, self.calories) return string

def __eq__(self, rs): """ Compares this food against another food for equality. Use: f == rs Parameters: rs - [right side] food to compare to (Food) Returns: result - True if name and origin match, False otherwise (boolean) """ result = (self.name.lower(), self.origin) == ( rs.name.lower(), rs.origin) return result

def __lt__(self, rs): """ Determines if this food comes before another. Use: f < rs Parameters: rs - [right side] food to compare to (Food) Returns: result - True if food precedes rs, False otherwise (boolean) """ result = (self.name.lower(), self.origin) < \ (rs.name.lower(), rs.origin) return result

def __le__(self, rs): """ Determines if this food precedes or is or equal to another. Use: f <= rs Parameters: rs - [right side] food to compare to (Food) Returns: result - True if this food precedes or is equal to rs, False otherwise (boolean) """ result = self < rs or self == rs return result

def write(self, file_variable): """ Writes a single line of food data to an open file. Use: f.write( file_variable ) Parameters: file_variable - an open file of food data (file) Returns: The contents of food are written as a string in the format name|origin|is_vegetarian to file_variable. """ print("{}|{}|{}|{}" .format(self.name, self.origin, self.is_vegetarian, self.calories), file=file_variable) return

def key(self): """ Creates a formatted string of food key data. Use: key = f.key() Returns: the formatted contents of food key (str) """ return "{}, {}".format(self.name, self.origin)

def __hash__(self): """ Generates a hash value from a food name. Use: h = hash(f) Returns: value - the total of the characters in the name string (int > 0) """ value = 0

for c in self.name: value = value + ord(c) return value

class Food: """ Defines an object for a single food: name, origin, vegetarian, calories. """ # Constants ORIGIN = ("Canadian", "Chinese", "Indian", "Ethiopian", "Mexican", "Greek", "Japanese", "Italian", "American", "Scottish", "New Zealand", "English")

@staticmethod def origins(): """ Creates a string list of food origins in the format: 0 Canadian 1 Chinese 2 Indian ... Use: s = Food.origins() Use: print(Food.origins()) - Returns: returns string - A numbered list of valid food origins. """ string = ""

for i in range(len(Food.ORIGIN)): string += """{:2d} {} """.format(i, Food.ORIGIN[i]) return string

def __init__(self, name, origin, is_vegetarian, calories): """ Initialize a food object. Use: f = Food( name, origin, is_vegetarian, calories ) Parameters: name - food name (str) origin - food origin (int) is_vegetarian - whether food is vegetarian (boolean) calories - caloric content of food (int > 0) Returns: food values are set. """ assert origin in range(len(Food.ORIGIN)), "Invalid origin ID" assert is_vegetarian in (True, False, None), "Must be True or False" assert calories is None or calories >= 0, "Calories must be >= 0"

self.name = name self.origin = origin self.is_vegetarian = is_vegetarian self.calories = calories return

def __str__(self): """ Creates a formatted string of food data. Use: print(f) Use: s = str(f) Returns: string - the formatted contents of food (str) """ if self.calories is None: # is a key string = "{}, {}".format(self.name, Food.ORIGIN[self.origin]) else: # full data set string = """Name: {} Origin: {} Vegetarian: {} Calories: {:,d}""".format(self.name, Food.ORIGIN[self.origin], self.is_vegetarian, self.calories) return string

def __eq__(self, rs): """ Compares this food against another food for equality. Use: f == rs Parameters: rs - [right side] food to compare to (Food) Returns: result - True if name and origin match, False otherwise (boolean) """ result = (self.name.lower(), self.origin) == ( rs.name.lower(), rs.origin) return result

def __lt__(self, rs): """ Determines if this food comes before another. Use: f < rs Parameters: rs - [right side] food to compare to (Food) Returns: result - True if food precedes rs, False otherwise (boolean) """ result = (self.name.lower(), self.origin) < \ (rs.name.lower(), rs.origin) return result

def __le__(self, rs): """ Determines if this food precedes or is or equal to another. Use: f <= rs Parameters: rs - [right side] food to compare to (Food) Returns: result - True if this food precedes or is equal to rs, False otherwise (boolean) """ result = self < rs or self == rs return result

def write(self, file_variable): """ Writes a single line of food data to an open file. Use: f.write( file_variable ) Parameters: file_variable - an open file of food data (file) Returns: The contents of food are written as a string in the format name|origin|is_vegetarian to file_variable. """ print("{}|{}|{}|{}" .format(self.name, self.origin, self.is_vegetarian, self.calories), file=file_variable) return

def key(self): """ Creates a formatted string of food key data. Use: key = f.key() Returns: the formatted contents of food key (str) """ return "{}, {}".format(self.name, self.origin)

def __hash__(self): """ Generates a hash value from a food name. Use: h = hash(f) Returns: value - the total of the characters in the name string (int > 0) """ value = 0

for c in self.name: value = value + ord(c) return value

 foods.txt Lasagna|7|False|135 Butter Chicken|2|False|490 Moo Goo Guy Pan|1|False|272 Vegetable Alicha|3|True|138 Spanakopita|5|True|260 Chirashi Don|6|False|600 Canuck Burger|0|False|7500 Turducken|8|False|12240 Shark Fin Soup|1|False|46 Chamuco|4|True|150 Natto|6|True|212 Canada Goose Chili|0|False|199 Diet Free-Range Gluten-free Water|8|True|0 Kung Pao Chicken|1|False|229 General Tao Chicken|1|False|850 Spicy Vegetable Moo Shu|1|True|140 BBQ Pork|1|False|920 Orange Chicken|1|False|800 Vegetables with Cashew Nuts|1|True|143 Lemon Chicken|1|False|226 Beef with Green Pepper|1|False|251 Sweet and Sour Pork|1|False|231 Szechuan Shrimp|1|False|516 Poutine|0|False|710 Teppanyaki|6|False|200 Greek Salad|5|True|185 Fettuccine|7|False|266 Teriyaki|6|False|233 Shortbread |9|True|502 Pavlova|10|True|272 Hokey Pokey Ice Cream|10|True|106 Fricot|0|False|360 Chip Butty|11|True|956 Grilled Salmon|0|False|511 Ravioli|7|False|246 Crepe|7|True|186 Fried Rice|1|False|425 Pepperoni Pizza|7|False|713 Chicken Kabob|2|False|167 Panzerotti|7|False|770 Spring Rolls|1|True|200 Chicken Chow Mein|1|False|178 Chicken Wings|8|False|298

t1.py

#test all code here

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Eric Redmond ,Jim Wilson

1st Edition

1934356921, 978-1934356920

More Books

Students also viewed these Databases questions

Question

Express your answer to three significant figures

Answered: 1 week ago

Question

Find the derivative. f(x) 8 3 4 mix X O 4 x32 4 x32 3 -4x - x2

Answered: 1 week ago