def test_check_numbers(self): valid = ["23", "2.123", " 97", "9 7"] invalid = ["127.0.0.1", "cos(12.2)", "tan(sin(atan(cosh(1))))", "pie", "1+2*3/4^5%6", "gamma(17)", "hello", "1&2", "$13.50", "1::", "cos(tan(12+t+15))"] for valid_str in valid: assert Commons.check_numbers(valid_str), "Valid string judged to be not calculation, "+valid_str for invalid_str in invalid: assert not Commons.check_numbers(invalid_str), "Invalid string judged to be calculation, "+invalid_str
def passive_run(self, event, full_line, hallo_obj, server_obj=None, user_obj=None, channel_obj=None): """Replies to an event not directly addressed to the bot.""" # Check if fullLine is a calculation, and is not just numbers, and contains numbers. if not Commons.check_calculation(full_line): return None if Commons.check_numbers(full_line.replace(".", "")): return None if not any([char in full_line for char in [str(x) for x in range(10)] + ["e", "pi"]]): return None # Clean up the line and feed to the calculator. calc = full_line.replace(' ', '').lower() try: self.preflight_checks(calc) answer = self.process_calculation(calc) return answer except Exception as e: print("Passive calc failed: "+str(e)) return None
def run(self, line, user_obj, destination_obj=None): if line.count(' ') == 0: number = line lang = "american" elif line.split()[1].lower() in ["british", "english"]: number = line.split()[0] lang = "english" elif line.split()[1].lower() in ["european", "french"]: number = line.split()[0] lang = "european" else: number = line.split()[0] lang = "american" if Commons.check_numbers(number): number = number elif Commons.check_calculation(number): function_dispatcher = user_obj.server.hallo.function_dispatcher calc_func = function_dispatcher.get_function_by_name("calc") calc_obj = function_dispatcher.get_function_object(calc_func) # type: Calculate number = calc_obj.process_calculation(number) else: return "Error, you must enter a valid number or calculation." return self.number_word(number, lang) + "."