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.

Question #2 Complete the read_food function for the food_utilities module.

Sample data:

Spanakopita|5|True|260 

Test this function from the next task.

Question #3 Complete the read_foods function for the food_utilities module.

Test this function with the file foods.txt.

Question #4 Complete the write_foods function for the food_utilities module.

The resulting file should have the same format as the file foods.txt. In Eclipse, right click on the lab 1 project name and choose, Refresh in order to see the new file in the project manage.

Test this function by writing the contents of the list from the previous task to a file named new_foods.txt.

Question #5 Complete the get_vegetarian function for the food_utilities module.

Test this function by selecting and printing all the vegetarian foods in the foods.txt file.

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). """

# Your code here

return food

def read_food(line): """ Question # 2 Creates and returns a food object from a line of string data. Use: f = read_food(line) Parameters: line - a vertical bar-delimited line of food data in the format name|origin|is_vegetarian|calories (str) Returns: food - contains the data from line (Food) """

# Your code here

return food

def read_foods(file_variable): """ Question # 3 Reads a file of food strings into a list of Food objects. Use: foods = read_food(file_variable) Parameters: file_variable - a file of food data (file) Returns: foods - a list of food objects (list of food) """

# Your code here

return foods

def write_foods(file_variable, foods): """ Question # 4 Writes a list of food objects to a file. file_variable contains the objects in foods as strings in the format name|origin|is_vegetarian|calories Use: write_foods(file_variable, foods) Parameters: file_variable - an open file of food data (file) foods - a list of Food objects (list of Food) Returns: None """

# Your code here

return

def get_vegetarian(foods): """ Question # 5 Creates a list of vegetarian foods. Use: v = get_vegetarian(foods) Parameters: foods - a list of Food objects (list of Food) Returns: veggies - Food objects from foods that are vegetarian (list of Food) """

# Your code here

return veggies

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

Intelligent Information And Database Systems Third International Conference Achids 2011 Daegu Korea April 2011 Proceedings Part 2 Lnai 6592

Authors: Ngoc Thanh Nguyen ,Chong-Gun Kim ,Adam Janiak

2011th Edition

3642200419, 978-3642200410

More Books

Students also viewed these Databases questions