Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Basic value comparison
- from operator import *
- # Main compare class
- class Compare(object):
- def __init__(self):
- self.OpError = "OpError: Invalid operator or brace.n"
- self.IntError = "IntError: Invalid integers.n"
- self.StrError = "StrError: Invalid strings.n"
- self.LstError = "LstError: Invalid list.n"
- self.BoolError = "BoolError: Invalid boolean.n"
- self.st_brace = "["
- self.end_brace = "]"
- self.OPERATORS = {
- ">=": ge,
- "<=": le,
- "==": eq,
- "!=": ne,
- ">": gt,
- "<": lt}
- # Compare two integers
- def compareint(self, command):
- if command[1] == self.st_brace and command[3] in self.OPERATORS and command[5] == self.end_brace:
- try:
- print self.OPERATORS[command[3]](
- int(command[2]), int(command[4]))
- except ValueError:
- print self.IntError
- if command[3] not in self.OPERATORS:
- print self.OpError
- # Compare two strings
- def comparestr(self, command):
- if command[1] == self.st_brace and command[3] in self.OPERATORS and command[5] == self.end_brace:
- try:
- print self.OPERATORS[command[3]](
- eval(command[2]), eval(command[4]))
- except SyntaxError:
- print self.StrError
- if command[3] not in self.OPERATORS:
- print self.OpError
- # Compare two lists
- def comparelst(self, command):
- if command[1] == self.st_brace and command[3] in self.OPERATORS and command[5] == self.end_brace:
- try:
- print self.OPERATORS[command[3]](
- eval(command[2]), eval(command[4]))
- except SyntaxError:
- print self.LstError
- if command[3] not in self.OPERATORS:
- print self.OpError
- # Compare two booleans
- def comparebool(self, command):
- if command[1] == self.st_brace and command[3] in self.OPERATORS and command[5] == self.end_brace:
- try:
- print self.OPERATORS[command[3]](
- eval(command[2]), eval(command[4]))
- except NameError:
- print self.BoolError
- if command[3] not in self.OPERATORS:
- print self.OpError
- # Dict containing commands
- COMMANDS = {
- "cmpbool": Compare().comparebool,
- "cmplst": Compare().comparelst,
- "cmpint": Compare().compareint,
- "cmpstr": Compare().comparestr,
- }
- # Read the inputted commands
- def read_command(prompt):
- command = raw_input(prompt)
- split_cmd = command.split(" ")
- if split_cmd[0] in COMMANDS:
- COMMANDS[split_cmd[0]](split_cmd)
- if split_cmd[0] not in COMMANDS:
- print "CmdError: Invalid command.n"
- # Run the program
- if __name__ == "__main__":
- while True: read_command(">}}>")
- >}}>cmpbool [ True == True ]
- True
- >}}>cmplst [ [""] == [""] ]
- True
- >}}>cmpint [ 2 == 2 ]
- True
- >}}>cmpstr [ "Hello" == "Hello" ]
- True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement