Advertisement
Vermiculus

Untitled

Apr 8th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.65 KB | None | 0 0
  1.  
  2. class Command:
  3.     ##token class to be executed in the kitchen
  4.     imperative = ""
  5.     imperativequalifier = ""
  6.     ing = ""
  7.     n = -1
  8.     p = -1
  9.  
  10.     def nth(self, a):
  11.         self.n = a
  12.     def pth(self, a):
  13.         self.p = a
  14.     def ingredient(self, a):
  15.         self.ing = a
  16.  
  17.     def __str__(self):
  18.         return self.imperative + " " + self.imperativequalifier + " " + self.ing + " " + str(self.n) + " " + str(self.p)
  19.  
  20. class Cookbook:
  21.     ##Class for parsing statements into command objects
  22.     a = "Take ingredient from refrigerator. \nPut ingredient into nth mixing bowl. \nPut ingredient into mixing bowl. \nAdd ingredient. \nAdd ingredient to mixing bowl. \nAdd ingredient to nth mixing bowl. \nRemove ingredient. \nRemove ingredient from mixing bowl. \nRemove ingredient from nth mixing bowl. \nCombine ingredient. \nCombine ingredient into mixing bowl. \nCombine ingredient into nth mixing bowl. \nDivide ingredient. \nDivide ingredient into mixing bowl. \nDivide ingredient into nth mixing bowl. \nAdd dry ingredients. \nAdd dry ingredients into mixing bowl. \nAdd dry ingredients into nth mixing bowl. \nLiquify ingredient. \nLiquify contents of the mixing bowl. \nLiquify contents of the nth mixing bowl. \nStir mixing bowl for num minutes. \nStir nth mixing bowl for num minutes. \nStir ingredient into the mixing bowl. \nStir ingredient into the nth mixing bowl. \nMix well. \nMix the mixing bowl well. \nMix the nth mixing bowl well. \nClean mixing bowl. \nClean nth mixing bowl. \nPour contents of the mixing bowl into baking dish \nPour contents of the nth mixing bowl the pth baking dish \nVerb the ingredient. \nVerb until verbed. \nVerb the ingredient until verbed. \nSet aside. \nServe with auxiliary-recipe. \nRefrigerate. \nRefrigerate for num hours. \nServes num."
  23.     l = list()
  24.     reserved = ['ingredient', "nth", "pth", "ingredient."]
  25.  
  26.     ##constructor initializes the list l
  27.     def __init__(self):
  28.         la = self.a.split("\n")
  29.        
  30.         for each in la:
  31.             self.l.append(each.split())
  32.  
  33.     ##verifies that a given ingredient has been declared
  34.     def ingredient(self, arg):
  35.         if(arg=="zuchinni"):
  36.             return arg
  37.  
  38.     ##verifies that a given number is a proper ordinal
  39.     ##returns an integer if it is
  40.     def nth(self, arg):
  41.         tailing = arg[len(arg)-2:len(arg)]
  42.         digits = arg[0:len(arg)-2]
  43.  
  44.         if(14 > int(digits[len(digits)-2:len(digits)]) > 10):
  45.             if(tailing=="th"):
  46.                 return digits
  47.         elif(int(digits[-1])==1):
  48.             if(tailing=="st"):
  49.                 return digits
  50.         elif(int(digits[-1])==2):
  51.             if(tailing=="nd"):
  52.                 return digits
  53.         elif(int(digits[-1])==3):
  54.             if(tailing=="rd"):
  55.                 return digits
  56.         elif(tailing=="th"):
  57.                 return digits
  58.         raise exception
  59.  
  60.     def pth(self, arg):
  61.         return nth(arg)
  62.  
  63.     def num(self, arg):
  64.         return int(arg)
  65.  
  66.     ##helper method for test that compares an individual template against the argument
  67.     def testDeep(self, arg, template):
  68.         for y in xrange(0, len(template)):
  69.             if(template[y] in self.reserved):
  70.                 func = getattr(self, template[y])
  71.                 try:    
  72.                     arg[y] = func(arg[y])
  73.                 except:
  74.                     return False
  75.             else:
  76.                 if(template[y] != arg[y]):
  77.                     return False
  78.                
  79.         return True
  80.  
  81.     ##helper method for test which converts the result into a command object
  82.     def makeCommand(self, passOn):
  83.         c = Command()
  84.         c.imperative = str(passOn[0][0])
  85.         c.imperativequalifier = str(passOn[0][1])
  86.        
  87.         for x in xrange(0, len(passOn[1])):
  88.             try:
  89.                 getattr(c, passOn[1][x])(passOn[0][x])
  90.             except:
  91.                 pass
  92.         return c
  93.  
  94.     ##turns a valid chef string into a command object
  95.     ##checks syntax against a list of valid template statements
  96.     def test(self, arg):
  97.        
  98.         testme = arg.split()
  99.         passOn = ()
  100.         for x in xrange(0, len(self.l)):
  101.             template = (self.l[x])
  102.             if(len(testme) == len(template)):
  103.                 if(self.testDeep(testme, template)==True):
  104.                     ##remove the period from the template (will interfere with getattr(command, s) in case "Add ingredient.")
  105.                     template[-1] = template[-1][0:len(template[-1])-1]
  106.                     passOn = (testme, template)
  107.                     break
  108.                 else:
  109.                     pass
  110.         m = self.makeCommand(passOn)
  111.         return m
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement