Try It Out Finishing a Module

Enter the following code and name the file

Module for making meals in Python.

Import this module and then call makeBreakfast(), makeDinner() or makeLunch().

all_ = ['Meal','AngryChefException1, 'makeBreakfast',

'makeLunch', 'makeDinner', 'Breakfast', 'Lunch', 'Dinner']

# Helper functions.

def makeBreakfast():

''' Creates a Breakfast object.''' return Breakfast()

def makeLunch():

def makeDinner():

''' Creates a Breakfast object.''' return Dinner()

# Exception classes.

class SensitiveArtistException(Exception):

'''Exception raised by an overly-sensitive artist.

Base class for artistic types.''' pass class AngryChefException(SensitiveArtistException):

'''Exception that indicates the chef is unhappy.''' pass class Meal:

'''Holds the food and drink used in a meal. In true object-oriented tradition, this class includes setter methods for the food and drink.

Call printIt to pretty-print the values.

'''Initialize to default values.''' = 'generic meal' = food self.drink = drink def printIt(self, prefix=''):

print prefix,'A fine',,'with',,'and',self.drink

# Setter for the food.

def setFood(self, food='omelet'): = food

# Setter for the drink.

def setDrink(self, drink='coffee'): self.drink = drink

# Setter for the name.

def setName(self, name=''): = name class Breakfast(Meal):

'''Holds the food and drink for breakfast.'''

'''Initialize with an omelet and coffee.'''


class Lunch(Meal):

'''Initialize with a sandwich and a gin and tonic.'''

self.setName('midday meal')

# Override setFood().

def setFood(self, food='sandwich'):

raise AngryChefException Meal.setFood(self, food)

class Dinner(Meal):

'''Initialize with steak and merlot.'''


print prefix,'A gourmet',,'with',,'and',self.drink def test():

print 'Module meal test.'

# Generic no arguments. print 'Testing Meal class.' m = Meal()

m = Meal('green eggs and ham', 'tea') m.printIt("\t")

# Test breakfast print 'Testing Breakfast class.' b = Breakfast() b.printIt("\t")

b.setName('breaking of the fast') b.print!t("\t")

# Test dinner print 'Testing Dinner class.' d = Dinner() d.printIt("\t")

# Test lunch print 'Testing Lunch class.' l = Lunch() l.printIt("\t")

print 'Calling Lunch.setFood().'



except AngryChefException:

print "\t",'The chef is angry.

. Pick an omelet.'

# Run test if this module is run as a


if _name_ == '_main_':


Was this article helpful?

0 0

Post a comment