Advertisement
Vermiculus

Untitled

Apr 9th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. # Use docstrings.
  2. # http://docs.python.org/tutorial/controlflow.html#intermezzo-coding-style
  3. # http://www.python.org/dev/peps/pep-0257/
  4.  
  5. ##random needed for mix method.  Needs to be external to the class
  6.  
  7. class Bowl:
  8.     ##with a little help from: http://stackoverflow.com/questions/227459/ascii-value-of-a-character-in-python
  9.     ##as well as: http://www.rafekettler.com/magicmethods.html
  10.    
  11.    
  12.    
  13.     def __init__(self, values = []):
  14.         """Constructor"""
  15.         #sorry to change your code, Sean but this allows Bowl to be treated as a list
  16.         #-Actually, this is what I had in mind,
  17.         #   although I don't see where we'd need to have the copy constructor ('values = []')
  18.         #   but it can't hurt
  19.         self.values = values
  20.  
  21.     def __len__(self):
  22.         """returns length of Bowl"""
  23.         return len (self.values)
  24.  
  25.     def delitem(self, key):
  26.         """deletes the item at key in self"""
  27.         del self.values[key]
  28.  
  29.     def top(self):
  30.         """returns the last element in self.values. (top of the stack)"""
  31.         return self.values[-1]
  32.  
  33.     def put(self, ingredient):
  34.         """appends value to self"""
  35.         self.values.append(ingredient)
  36.  
  37.     def fold(self, ingredient):
  38.         """sets value of the top of the stack to ingredient"""
  39.         ingredient = self.values.pop()
  40.         return ingredient
  41.  
  42.     def add(self, ingredient):
  43.         """    ##This adds the value of ingredient to the value of the ingredient
  44.    ##on top of the bowl and sets that sum as the new top of the stack,
  45.    ##erasing what was there.
  46.         """
  47.         holder = self.top()
  48.         holder = holder + ingredient
  49.         self.values[-1] = holder
  50.  
  51.     def remove(self, ingredient):
  52.         """##This subtracts the value of ingredient from the value of the ingredient
  53.    ##on top of the bowl and sets that difference as the new top of the stack,
  54.    ##erasing what was there.
  55.         """
  56.         holder = self.top()
  57.         holder = holder - ingredient
  58.         self.values[-1] = holder
  59.  
  60.     def combine(self, ingredient):
  61.         """This multiplies the value of ingredient from the value of the ingredient
  62. on top of the bowl and sets that product as the new top of the stack, erasing what was there.
  63.         """
  64.         holder = self.top()
  65.         holder = holder * ingredient
  66.         self.values[-1] = holder
  67.  
  68.  
  69.     def divide(self, ingredient):
  70.         """This divides the value of ingredient from the value of the ingredient
  71.        on top of the bowl and sets that quotient as the new top of the stack, erasing what was there.
  72.         """
  73.         holder = self.top()
  74.         holder = holder / ingredient
  75.         self.values[-1] = holder
  76.  
  77.     def mix(self):
  78.         """This randomises the order of the ingredients"""
  79.         import random
  80.         random.shuffle(self.values)
  81.  
  82.     def liquify(self):
  83.         """This turns all the ingredients in the bowl into a liquid, i.e. a Unicode characters for output purposes."""
  84. #         better = []
  85. #         for x in range (len(self.values)):
  86. #             better.append(chr(self.values[x]))
  87. #         self.values = better
  88.         self.values = map(chr, self.values)
  89.        
  90.     def stir (self, minutes):
  91.         dad = []
  92.         butts = self.values.pop()
  93.         self.values.insert(len(self.values)-minutes, butts)
  94.        
  95.        
  96. ##          self.values.insert(minutes - 1, self.values.pop())
  97.  
  98.     def __str__(self):
  99.         """to string method, so one can print Bowl when needed """
  100.         return str(self.values)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement