本文整理汇总了Python中inc.Commons.Commons.check_calculation方法的典型用法代码示例。如果您正苦于以下问题:Python Commons.check_calculation方法的具体用法?Python Commons.check_calculation怎么用?Python Commons.check_calculation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类inc.Commons.Commons
的用法示例。
在下文中一共展示了Commons.check_calculation方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_check_calculation
# 需要导入模块: from inc.Commons import Commons [as 别名]
# 或者: from inc.Commons.Commons import check_calculation [as 别名]
def test_check_calculation(self):
valid = ["23", "2.123", "cos(12.2)", "tan(sin(atan(cosh(1))))", "pie", "1+2*3/4^5%6", "gamma(17)"]
invalid = ["hello", "1&2", "$13.50", "1::", "cos(tan(12+t+15))"]
for valid_str in valid:
assert Commons.check_calculation(valid_str), "Valid string judged to be not calculation, "+valid_str
for invalid_str in invalid:
assert not Commons.check_calculation(invalid_str), "Invalid string judged to be calculation, "+invalid_str
示例2: run
# 需要导入模块: from inc.Commons import Commons [as 别名]
# 或者: from inc.Commons.Commons import check_calculation [as 别名]
def run(self, line, user_obj, destination_obj=None):
line_clean = line.strip().lower()
if line_clean.isdigit():
number = int(line_clean)
elif Commons.check_calculation(line_clean):
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_str = calc_obj.process_calculation(line_clean)
if "." in number_str:
return "Error, this calculation does not result in an integer. The answer is: " + number_str
number = int(number_str)
else:
return "Error, this is not a valid number or calculation."
prime_factors = self.find_prime_factors(number)
return "The prime factors of " + str(number) + " are: " + 'x'.join(str(x) for x in prime_factors) + "."
示例3: preflight_checks
# 需要导入模块: from inc.Commons import Commons [as 别名]
# 或者: from inc.Commons.Commons import check_calculation [as 别名]
def preflight_checks(self, calc):
# strip spaces
calc_clean = calc.replace(' ', '').lower()
# make sure only legit characters are allowed
if not Commons.check_calculation(calc_clean):
# TODO use custom exception
raise Exception('Error, Invalid characters in expression')
# make sure open brackets don't out-number close
if calc.count('(') > calc.count(')'):
raise Exception('Error, too many open brackets')
# Make sure close brackets don't out-number open.
# Previously I thought it would be okay to skip this, but "(21/3))+2))*5" evaluates as 17, rather than 45
if calc.count(')') > calc.count('('):
raise Exception('Error, too many close brackets')
if len(calc) == 0:
raise Exception("Error, empty calculation or brackets")
return True
示例4: passive_run
# 需要导入模块: from inc.Commons import Commons [as 别名]
# 或者: from inc.Commons.Commons import check_calculation [as 别名]
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