summaryrefslogtreecommitdiff
path: root/calc.py
diff options
context:
space:
mode:
authorjomo <github@jomo.tv>2015-01-06 23:38:42 +0100
committerjomo <github@jomo.tv>2015-01-06 23:38:42 +0100
commit9a916ed2cd9440d46079f9b7db421a7b9fcbf6ee (patch)
tree3af33b3ddc2fa3159f9243b0ade09b3e3cc96425 /calc.py
parent3435e994326b14f700777741a76dd990a2bc703f (diff)
change indentation from 2 spaces to 4, fix #5
Diffstat (limited to 'calc.py')
-rw-r--r--calc.py92
1 files changed, 46 insertions, 46 deletions
diff --git a/calc.py b/calc.py
index eeb236b..2022261 100644
--- a/calc.py
+++ b/calc.py
@@ -1,65 +1,65 @@
from helpers import *
-calc_users = open_json_file("calc", [])
+calc_users = open_json_file("calc", [])
math_operators = ["+", "-", "*", "/", "&", "|"]
ignore_operators = ["**", "&&", "||"] # ** may be too intensive, the others cause syntax errors
calc_perm = "utils.calc"
def calc(text):
- """
- extracts a mathematical expression from `text`
- returns (expression, result) or None
- """
- expression = ""
- should_calc = False
- for char in text:
- if char.isdigit() or (should_calc and char in [".", " "]):
- expression += char
- elif char in math_operators:
- # calculation must include at least 1 operator
- should_calc = True
- expression += char
- elif should_calc and char.isalpha():
- # don't include any more text in the calculation
- break
- if should_calc and not any(op in expression for op in ignore_operators):
- try:
- result = str(eval(expression)) # pylint: disable = W0123
- except: # pylint: disable = W0702
- # we can run into all kinds of errors here
- # most probably SyntaxError
- return None
- return (expression, result)
- return None
+ """
+ extracts a mathematical expression from `text`
+ returns (expression, result) or None
+ """
+ expression = ""
+ should_calc = False
+ for char in text:
+ if char.isdigit() or (should_calc and char in [".", " "]):
+ expression += char
+ elif char in math_operators:
+ # calculation must include at least 1 operator
+ should_calc = True
+ expression += char
+ elif should_calc and char.isalpha():
+ # don't include any more text in the calculation
+ break
+ if should_calc and not any(op in expression for op in ignore_operators):
+ try:
+ result = str(eval(expression)) # pylint: disable = W0123
+ except: # pylint: disable = W0702
+ # we can run into all kinds of errors here
+ # most probably SyntaxError
+ return None
+ return (expression, result)
+ return None
@hook.event("player.AsyncPlayerChatEvent", "monitor")
def on_calc_chat(event):
- sender = event.getPlayer()
- message = event.getMessage()
- if not event.isCancelled() and uid(sender) in calc_users and sender.hasPermission(calc_perm):
- output = calc(message)
- if output:
- msg(sender, "&2=== Calc: &e" + output[0] + " &2= &c" + output[1])
+ sender = event.getPlayer()
+ message = event.getMessage()
+ if not event.isCancelled() and uid(sender) in calc_users and sender.hasPermission(calc_perm):
+ output = calc(message)
+ if output:
+ msg(sender, "&2=== Calc: &e" + output[0] + " &2= &c" + output[1])
@hook.command("calc", description="Toggles chat calculations")
def on_calc_command(sender, args):
- plugin_header(sender, "Chat Calculator")
- if not sender.hasPermission(calc_perm):
- noperm(sender)
- return True
- if not checkargs(sender, args, 0, 1):
- return True
- if not is_player(sender):
- msg(sender, "&cYou are not a player!" % sender)
- return True
+ plugin_header(sender, "Chat Calculator")
+ if not sender.hasPermission(calc_perm):
+ noperm(sender)
+ return True
+ if not checkargs(sender, args, 0, 1):
+ return True
+ if not is_player(sender):
+ msg(sender, "&cYou are not a player!" % sender)
+ return True
- toggle(sender, calc_users, name = "Calc")
- save_json_file("calc", calc_users)
+ toggle(sender, calc_users, name = "Calc")
+ save_json_file("calc", calc_users)
- status = "enabled" if uid(sender) in calc_users else "disabled"
- msg(sender, "&6We just &e%s&6 Chat Calculator for you!" % status)
+ status = "enabled" if uid(sender) in calc_users else "disabled"
+ msg(sender, "&6We just &e%s&6 Chat Calculator for you!" % status)
- return True \ No newline at end of file
+ return True \ No newline at end of file