Reverted changes

This commit is contained in:
PanFritz
2015-01-13 13:12:00 +01:00
parent 89c5c3e646
commit 197ef515b9

19
calc.py
View File

@@ -1,7 +1,7 @@
from helpers import * from helpers import *
calc_users = open_json_file("calc", []) calc_users = open_json_file("calc", [])
math_operators = ["+", "-", "*", "/", "&", "|", ">", "<", "~", "%"] math_operators = ["+", "-", "*", "/", "&", "|"]
ignore_operators = ["**", "&&", "||"] # ** may be too intensive, the others cause syntax errors ignore_operators = ["**", "&&", "||"] # ** may be too intensive, the others cause syntax errors
calc_perm = "utils.calc" calc_perm = "utils.calc"
@@ -12,7 +12,6 @@ def calc(text):
returns (expression, result) or None returns (expression, result) or None
""" """
expression = "" expression = ""
d_expression = ""
should_calc = False should_calc = False
for char in text: for char in text:
if char.isdigit() or (should_calc and char in [".", " "]): if char.isdigit() or (should_calc and char in [".", " "]):
@@ -21,19 +20,17 @@ def calc(text):
# calculation must include at least 1 operator # calculation must include at least 1 operator
should_calc = True should_calc = True
expression += char expression += char
elif char.isalpha(): elif should_calc and char.isalpha():
# don't include any more text in the calculation # don't include any more text in the calculation
if should_calc: break
d_expression = expression if should_calc and not any(op in expression for op in ignore_operators):
expression = ""
if should_calc and not any(op in d_expression for op in ignore_operators):
try: try:
result = str(eval(d_expression)) # pylint: disable = W0123 result = str(eval(expression)) # pylint: disable = W0123
except: # pylint: disable = W0702 except: # pylint: disable = W0702
# we can run into all kinds of errors here # we can run into all kinds of errors here
# most probably SyntaxError # most probably SyntaxError
return None return None
return (d_expression, result) return (expression, result)
return None return None
@@ -43,8 +40,8 @@ def on_calc_chat(event):
message = event.getMessage() message = event.getMessage()
if not event.isCancelled() and uid(sender) in calc_users and sender.hasPermission(calc_perm): if not event.isCancelled() and uid(sender) in calc_users and sender.hasPermission(calc_perm):
output = calc(message) output = calc(message)
if type(output)in [int, float, long, complex]: if output:
msg(sender, "&2=== Calc: &e" + output[0] + " &2= &c" + str(output[1]).replace("420", "blazeit")) msg(sender, "&2=== Calc: &e" + output[0] + " &2= &c" + output[1])
@hook.command("calc", description="Toggles chat calculations") @hook.command("calc", description="Toggles chat calculations")