Vermiculus

Untitled

Apr 1st, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. import random
  7.  
  8. class Bowl:
  9.     ##with a little help from: http://stackoverflow.com/questions/227459/ascii-value-of-a-character-in-python
  10.     ##as well as: http://www.rafekettler.com/magicmethods.html
  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.         self.values[-1] = ingredient
  40.  
  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.  
  52.     def remove(self, ingredient):
  53.         """##This subtracts the value of ingredient from the value of the ingredient
  54.    ##on top of the bowl and sets that difference as the new top of the stack,
  55.    ##erasing what was there.
  56.         """
  57.         holder = self.top()
  58.         holder = holder - ingredient
  59.         self.values[-1] = holder
  60.  
  61.     def combine(self, ingredient):
  62.         """This multiplies the value of ingredient from the value of the ingredient
  63. on top of the bowl and sets that product as the new top of the stack, erasing what was there.
  64.         """
  65.         holder = self.top()
  66.         holder = holder * ingredient
  67.         self.values[-1] = holder
  68.  
  69.  
  70.     def divide(self, ingredient):
  71.         """This divides the value of ingredient from the value of the ingredient
  72.        on top of the bowl and sets that quotient as the new top of the stack, erasing what was there.
  73.         """
  74.         holder = self.top()
  75.         holder = holder / ingredient
  76.         self.values[-1] = holder
  77.  
  78.     def mix(self):
  79.         """This randomises the order of the ingredients"""
  80.         pretend = []
  81.         while(len(self.values)>len(pretend)):
  82.             holder = random.choice(self.values)
  83.             if (pretend.count(holder)>0):
  84.                 pass
  85.             else:
  86.                 pretend.append(holder)
  87.         self.values = pretend
  88.  
  89.     def liquify(self):
  90.         """This turns all the ingredients in the bowl into a liquid, i.e. a Unicode characters for output purposes."""
  91.         better = []
  92.         for x in range (len(self.values)):
  93.             better.append(chr(self.values[x]))
  94.         self.values = better
  95.  
  96.     def __str__(self):
  97.         """to string method, so one can print Bowl when needed """
  98.         return str(self.values)
Add Comment
Please, Sign In to add comment