def create(ourSkewer, args): """ Create the skewer that will hold the food. Arguments: ourSkewer - the Skewer object args - a string containing the size to create. Example: create 10 # creates a skewer that can hold 10 elements """ # make sure the size is valid try: size = int(args[0]) if size < 1: raise skewer_exception.SkewerException( "Skewer size must be greater than 0") except ValueError as e: print("The size of the skewer must be a valid integer") return None # force delete any pre-existing skewer if ourSkewer: ourSkewer = skewer.close(ourSkewer) ourSkewer = skewer.mkSkewer(size) print("Skewer created.") return ourSkewer
def add(ourSkewer, args): """ Add a specified item to the skewer Arguments: ourSkewer - the Skewer object args - The name of the item to add Example: add Onion # add an Onion to the skewer """ if len(args) < 1: help(None, None) return name = args[0] # Check that the name is in the list of valid foods. if name not in food.FOODS: raise skewer_exception.SkewerException( "Kebab can hold the following foods: " + str(food.FOODS)) skewer.add(ourSkewer, name) if ourSkewer != None: print(name, "successfully added to the skewer.") else: print(name, "was not successfully added to the skewer.")
def top(skwr): """ Gets the name of the top item on the skewer. Arguments: skwr - the Skewer instance Exceptions: skewer_exception.SkewerException - no item was on the skewer, or it doesn't exist Returns: The name of the food item (string) on the front """ if skwr == None: raise skewer_exception.SkewerException( "Cannot get top item from skewer that does not exist") if kebab_spot.emptyKebab(skwr.top): raise skewer_exception.SkewerException( "Cannot get top item from an empty skewer!") return skwr.top.item.name
def destroy(skwr): """ Close and destroy the skewer, if it exists Exceptions: skewer_exception.SkewerException - cannot destroy the skewer because it doesn't exist """ if skwr == None: raise skewer_exception.SkewerException( "Cannot destroy the skewer if it does not exist") close(skwr)
def calories(skwr): """ This function takes the paramter of the skewer and uses it to test for an empty skewer and returns the value of calories. """ if skwr == None: raise skewer_exception.SkewerException( \ "Cannot get calories from skewer if it does not have anything") return kebab_spot.calories(skwr.top)
def remove(skwr): """ Remove the front item from the skewer. Arguments: skwr - the Skewer instance Exceptions: skewer_exception.SkewerException - no item was on the skewer, or it doesn't exist """ if skwr == None: raise skewer_exception.SkewerException( "Cannot eat item from skewer that does not exist") if kebab_spot.emptyKebab(skwr.top): raise skewer_exception.SkewerException( "Cannot get item from an empty skewer!") # Un-draw the food item. skwr.ui.remove() # "Pop" the item off the skewer. skwr.top = skwr.top.succ
def close(skwr): """ Close the UI if the skewer is deleted. Arguments: skwr - the Skewer instance being deleted. Exceptions: skewer_exception.SkewerException - cannot close the skewer because it doesn't exist """ if skwr == None: raise skewer_exception.SkewerException( "Cannot close the skewer if it does not exist") skwr.ui.close()
def size(skwr): """ Get the number of elements on the skewer. Arguments: skwr - the Skewer instance Returns: the number of elements (int) Exceptions: skewer_exception.SkewerException - skewer doesn't exist """ if skwr == None: raise skewer_exception.SkewerException( \ "Cannot get size from skewer that does not exist") return kebab_spot.size(skwr.top)
def add(skwr, name): """ Add a food item to the skewer Arguments: skwr - the Skewer instance name - the string name of the food item Exceptions: skewer_exception.SkewerException - item could not be added because skewer doesn't exist """ if skwr == None: raise skewer_exception.SkewerException( "Cannot add item to skewer that does not exist") if skwr.top == kebab_spot.NONE_NODE or kebab_spot.size( skwr.top) < skwr.capacity: obj = food.mkFood(name) # Create new KebabSpot and "push" it on the skwr. skwr.top = kebab_spot.mkKebabSpot(obj, skwr.top) # Have the food appear in the right spot. skwr.ui.add(skwr.top) else: raise skewer_exception.SkewerException( "Cannot add item to a full skewer!")
def asString(skwr): """ Return a string representation of the items on the skewer. Arguments: skwr - the Skewer instance Returns: A string containing all the items on the skewer, from front to back, comma separated, and surrounded with square brackets. """ if skwr == None: raise skewer_exception.SkewerException( \ "Cannot get items from skewer if it does not exist") return "[ " + kebab_spot.stringEm(skwr.top) + " ]"
def has(skwr, name): """ Is a particular food item on the skewer? Arguments: skwr - the Skewer instance name - the name (string) of the food item to search for Returns: True if the item is on the skewer, False if not. Exceptions: skewer_exception.SkewerException - skewer does not exist """ if skwr == None: raise skewer_exception.SkewerException( \ "Cannot tell if skewer has something on it if it does not exist") return kebab_spot.has(skwr.top, name)
def isVeggie(skwr): """ Are there only vegetables on the skewer? Arguments: skwr - the Skewer instance Returns: True if there are only veggies on the skewer, False if not. Exceptions: skewer_exception.SkewerException - skewer does not exist """ if skwr == None: raise skewer_exception.SkewerException( \ "Cannot tell if veggie if the skewer does not exist") return kebab_spot.isVeggie(skwr.top)
def mkSkewer(capacity): """ Construct a Skewer instance. Arguments: capacity - the maximum number of items it can hold Exceptions: skewer_exception.SkewerException - a capacity less than 1 was specified. """ if capacity < 1: raise skewer_exception.SkewerException( \ "Skewers must have positive capacity!" ) result = Skewer() result.top = kebab_spot.NONE_NODE result.capacity = capacity result.ui = kebab_graphics.SkewerUI(capacity) return result
def has(ourSkewer, args): """ Checks if the skewer holds a certain item. Arguments: ourSkewer - the Skewer object args - the name of the item to search for Example: has Onion # does the skewer have an Onion on it? """ if len(args) < 1: help(None, None) return name = args[0] if skewer.has(ourSkewer, name): print(name, "does exist on the Skewer.") else: raise skewer_exception.SkewerException( "Skewer size must be greater than 0")