Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. # Basic value comparison
  2. from operator import *
  3.  
  4.  
  5. # Main compare class
  6. class Compare(object):
  7. def __init__(self):
  8. self.OpError = "OpError: Invalid operator or brace.n"
  9. self.IntError = "IntError: Invalid integers.n"
  10. self.StrError = "StrError: Invalid strings.n"
  11. self.LstError = "LstError: Invalid list.n"
  12. self.BoolError = "BoolError: Invalid boolean.n"
  13. self.st_brace = "["
  14. self.end_brace = "]"
  15. self.OPERATORS = {
  16. ">=": ge,
  17. "<=": le,
  18. "==": eq,
  19. "!=": ne,
  20. ">": gt,
  21. "<": lt}
  22.  
  23. # Compare two integers
  24. def compareint(self, command):
  25. if command[1] == self.st_brace and command[3] in self.OPERATORS and command[5] == self.end_brace:
  26. try:
  27. print self.OPERATORS[command[3]](
  28. int(command[2]), int(command[4]))
  29. except ValueError:
  30. print self.IntError
  31. if command[3] not in self.OPERATORS:
  32. print self.OpError
  33.  
  34. # Compare two strings
  35. def comparestr(self, command):
  36. if command[1] == self.st_brace and command[3] in self.OPERATORS and command[5] == self.end_brace:
  37. try:
  38. print self.OPERATORS[command[3]](
  39. eval(command[2]), eval(command[4]))
  40. except SyntaxError:
  41. print self.StrError
  42. if command[3] not in self.OPERATORS:
  43. print self.OpError
  44.  
  45. # Compare two lists
  46. def comparelst(self, command):
  47. if command[1] == self.st_brace and command[3] in self.OPERATORS and command[5] == self.end_brace:
  48. try:
  49. print self.OPERATORS[command[3]](
  50. eval(command[2]), eval(command[4]))
  51. except SyntaxError:
  52. print self.LstError
  53. if command[3] not in self.OPERATORS:
  54. print self.OpError
  55.  
  56. # Compare two booleans
  57. def comparebool(self, command):
  58. if command[1] == self.st_brace and command[3] in self.OPERATORS and command[5] == self.end_brace:
  59. try:
  60. print self.OPERATORS[command[3]](
  61. eval(command[2]), eval(command[4]))
  62. except NameError:
  63. print self.BoolError
  64. if command[3] not in self.OPERATORS:
  65. print self.OpError
  66.  
  67.  
  68. # Dict containing commands
  69. COMMANDS = {
  70. "cmpbool": Compare().comparebool,
  71. "cmplst": Compare().comparelst,
  72. "cmpint": Compare().compareint,
  73. "cmpstr": Compare().comparestr,
  74. }
  75.  
  76.  
  77. # Read the inputted commands
  78. def read_command(prompt):
  79. command = raw_input(prompt)
  80. split_cmd = command.split(" ")
  81. if split_cmd[0] in COMMANDS:
  82. COMMANDS[split_cmd[0]](split_cmd)
  83. if split_cmd[0] not in COMMANDS:
  84. print "CmdError: Invalid command.n"
  85.  
  86.  
  87. # Run the program
  88. if __name__ == "__main__":
  89. while True: read_command(">}}>")
  90.  
  91. >}}>cmpbool [ True == True ]
  92. True
  93. >}}>cmplst [ [""] == [""] ]
  94. True
  95. >}}>cmpint [ 2 == 2 ]
  96. True
  97. >}}>cmpstr [ "Hello" == "Hello" ]
  98. True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement