classFridge:"""
this is a fridge.
Methods:
add_one(food_name): adds a single food_name to the fridge
add_many(food_dict): adds a whole dictionary filled with food as keys and quantity as value
has(food_name, quantity=1): check if the string food_name is in the fridge
has_various( foods): determines if the dictionary food_name has enough of every element to satisfy a request
get_one(food_name): remove one
get_multi(food_dict):remove each type of food in dict
"""def__init__(self, items):"""Optionally pass in an initial dictionary of items"""ifnotisinstance(items,dict):raise TypeError("Fridge require a dictionary but was given {0}".format(type(items)))
self.items = items
def__add_multi(self, food_name, quantity):"""
function:adds more than one of a food item
:param food_name: str
:param quantity: int
:return: the number of items added
attention:this should only be used internally,after the type checking has been done
"""# initial a new type of food to 0if food_name notin self.items:
self.items[food_name]=0
self.items[food_name]= self.items[food_name]+ quantity
defadd_one(self, food_name):"""
function:adds a single food_name to the fridge
:param food_name: str
:return: True
attention:Raise a TypeError if food_name is not a string
"""ifnotisinstance(food_name,str):raise TypeError("add_one requires a string but given a {0}".format(type(food_name)))else:
self.__add_multi(food_name,1)returnTruedefadd_many(self, food_dict):"""
function:adds a whole dictionary filled with food as keys and quantity as value
:param food_dict: dict{'food_name': 'quantity'}
:return: none
"""ifnotisinstance(food_dict,dict):raise TypeError("add_many requires a dict but given a {0}".format(type(food_dict)))for item in food_dict.keys():
self.__add_multi(item, food_dict[item])returnTruedefhas(self, food_name, quantity=1):"""
function:check if the string food_name is in the fridge
:param food_name: string
:param quantity: default to 1
:return: enough --> true; empty --> false
"""ifnotisinstance(food_name,str):raise TypeError("has() needs a string but given a {0}".format(type(food_name)))return self.has_various({food_name: quantity})defhas_various(self, foods):"""
has_various(foods): determines if the dictionary food_name has enough
of every element to satisfy a request
:param foods: dict
:return: enough --> true; less --> false
"""try:for food_name in foods.keys():if self.items[food_name]< foods[food_name]:returnFalsereturnTrueexcept KeyError:print("there are no this kind of food ")returnFalsedef__get_multi(self, food_name, quantity):"""
:param food_name: string
:param quantity: int
:return: there isn't enough food --> False ; the number of items been removed
"""if food_name notin self.items.keys():print("state --> failed")print("this is no this kind of food that named '{0}'".format(food_name))returnFalseelse:if quantity > self.items[food_name]:print("state --> failed")print("{0} not enough {1}.".format(food_name, quantity))returnFalseelse:
self.items[food_name]= self.items[food_name]- quantity
print("state --> success")print("remove :", food_name, quantity)print("left:", food_name, self.items[food_name])returnTruedefget_one(self, food_name):"""
function:remove one
:param food_name: string
:return: function ( __get_multi )
"""ifnotisinstance(food_name,str):raise TypeError("food_name needs a string but a {0} is given".format(type(food_name)))return self.__get_multi(food_name,1)defget_multi(self, food_dict):"""
:param food_dict:
:return: foodGetted
"""
foodGetted ={}ifnotisinstance(food_dict,dict):raise TypeError("get_multi requires a dict but given a {0}".format(type(food_dict)))for food_name in food_dict.keys():if self.__get_multi(food_name, quantity=food_dict[food_name]):
foodGetted[food_name]= food_dict[food_name]return foodGetted
defdisplay_items(self):print(self.items)if __name__ =='__main__':print('*'*20,"test initial","*"*20)
fridge = Fridge({"egg":6,"milk":4,"cheese":3})
fridge.display_items()print('*'*20,"test add_many","*"*20)
fridge.add_many({"egg":2,"grape":3})
fridge.display_items()print('*'*20,"test has()","*"*20)print(fridge.has("egg",6))print("-"*20)print(fridge.has("apple"))print('*'*20,"test get_one()","*"*20)
fridge.get_one('apple')print("-"*20)
fridge.get_one('egg')
fridge.display_items()print('*'*20,"test get_multi()","*"*20)
get = fridge.get_multi({"egg":6,"apple":2,"milk":3})
fridge.display_items()print('you get the list of this:')print(get)
类编写类时,先确定这个类的功能向类中增加文档字符串,定义这个类的功能,并写出这个类的方法写出方法,在doc中标出方法的接受参数和返回值class Fridge(object): &quot;&quot;&quot;This class implements a fridge where ingredients can be added and remove individually,or in gr...