def _test_calculate_expression(self): text1 = "exp (1 add 2 multiply 3)" text2 = ( "((15 DIVIDE (7 SUBTRACT (1 ADD 1))) MULTIPLY 3) SUBTRACT (2 ADD (1 ADD 1))" ) calc = Calculator() input_queue1 = calc.create_input_queue(text1) assert str(input_queue1) == "EXP, (, 1.0, ADD, 2.0, MULTIPLY, 3.0, ), " output_queue1 = calc.create_output_queue() input_queue2 = calc.create_input_queue(text2) assert ( str(input_queue2) == "(, (, 15.0, DIVIDE, (, 7.0, SUBTRACT, (, 1.0, ADD, 1.0, ), ), ), MULTIPLY, 3.0, ), SUBTRACT, (, 2.0, ADD, (, 1.0, ADD, 1.0, ), ), " ) output_queue2 = calc.create_output_queue() assert ( str(output_queue2) == "15.0, 7.0, 1.0, 1.0, add, subtract, true_divide, 3.0, multiply, 2.0, 1.0, 1.0, add, add, subtract, " ) assert str(calc.calculate_expression(text1)).replace(", ", "") == str( numpy.exp(7) ) assert str(calc.calculate_expression(text2)).replace(", ", "") == "5.0"
def main(self): command_helper = InputHelper(["calc", "test", "exit"]) try: next_command = command_helper.get_legal_input( "Please enter a command: \n") if next_command == "test": test_handler = TestHandler() test_handler.run() pass if next_command == "calc": calc = Calculator() calc.calculate_expression( input("Enter an expression to be calculated: ")) if next_command == "exit": sys.exit(0) except KeyboardInterrupt: return self.main()