Try It Out Finishing a Module

Enter the following code and name the file meal.py:

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.''' self.name = 'generic meal' self.food = food self.drink = drink def printIt(self, prefix=''):

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

# Setter for the food.

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

# Setter for the drink.

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

# Setter for the name.

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

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

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

self.setName('breakfast')

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.'''

self.setName('dinner')

print prefix,'A gourmet',self.name,'with',self.food,'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().'

try:

l.setFood('hotdog')

except AngryChefException:

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

. Pick an omelet.'

# Run test if this module is run as a

program.

if _name_ == '_main_':

test()

Was this article helpful?

0 0

Post a comment