minetestbot-modules/calc.py

82 lines
2.4 KiB
Python
Raw Normal View History

2013-01-07 10:00:44 -08:00
#!/usr/bin/env python
# coding=utf-8
"""
calc.py - Phenny Calculator Module
2014-01-06 07:01:54 -08:00
Copyright 2014, sfan5
2013-01-07 10:00:44 -08:00
"""
2014-01-06 07:01:54 -08:00
import ast
import operator as op
import math
# http://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string/9558001#9558001
2014-01-06 11:40:40 -08:00
operators = {
ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul, ast.Pow: op.pow,
ast.Div: op.truediv, ast.BitXor: op.xor, ast.Mod: op.mod
}
2014-01-06 07:01:54 -08:00
2014-01-06 11:40:40 -08:00
funcs = {
"bin": bin, "abs": abs, "oct": oct, "int": int, "sum": sum,
"tuple": tuple, "divmod": divmod, "hash": hash, "hex": hex,
"len": len, "list": list, "long": long, "max": max,
"range": range, "round": round, "min": min
}
2014-01-06 07:01:54 -08:00
for funcn in dir(math):
if funcn.startswith("__"):
continue
funcs[funcn] = getattr(math, funcn)
def getfunc(fn):
if fn in funcs:
return funcs[fn]
else:
2014-01-06 11:40:40 -08:00
raise ValueError("Function not known: '" + str(fn) + "'")
2014-01-06 07:01:54 -08:00
2014-01-06 11:40:40 -08:00
def get_kwargs(kwrds):
kwargs = {}
for kw in kwrds:
kwargs[kw.arg] = eval_astnode(kw.value)
return kwargs
2014-01-06 07:01:54 -08:00
2014-01-06 11:40:40 -08:00
def eval_strexpr(expr):
return eval_astnode(ast.parse(expr).body[0].value) # Module(body=[Expr(value=...)])
def eval_astnode(node):
2014-01-06 07:01:54 -08:00
if isinstance(node, ast.Num): # <number>
return node.n
elif isinstance(node, ast.operator): # <operator>
return operators[type(node)]
elif isinstance(node, ast.BinOp): # <left> <operator> <right>
2014-01-06 11:40:40 -08:00
return eval_astnode(node.op)(eval_astnode(node.left), eval_astnode(node.right))
2014-01-06 07:01:54 -08:00
elif isinstance(node, ast.Call): # <func> ( <args> )
2014-01-06 11:40:40 -08:00
return getfunc(node.func.id)(*(eval_astnode(e) for e in node.args), **get_kwargs(node.keywords))
elif isinstance(node, ast.Tuple): # ( <elem> , <elem2> , [...] )
return tuple(eval_astnode(e) for e in node.elts)
elif isinstance(node, ast.List): # [ <elem> , <elem2> , [...] ]
return list(eval_astnode(e) for e in node.elts)
elif isinstance(node, ast.Str): # ('|") <text> ('|")
return node.s
2014-01-06 07:01:54 -08:00
else:
raise TypeError("AST node type not allowed: '" + type(node).__name__ + "'")
def c(phenny, input):
for x in phenny.bot.commands["high"].values():
if x[0].__name__ == "aa_hook":
if x[0](phenny, input):
return # Abort function
if not input.group(2):
return phenny.reply("Nothing to calculate.")
q = input.group(2).encode('utf-8')
2014-01-06 11:40:40 -08:00
print("[LOG]: %s calculated '%s'" % (input.nick, q))
2014-01-06 07:01:54 -08:00
try:
2014-01-06 11:40:40 -08:00
phenny.say(repr(eval_strexpr(q)))
2014-01-06 07:10:13 -08:00
except Exception as e:
phenny.say("Exception: " + str(e))
2014-01-06 07:01:54 -08:00
2013-01-07 10:00:44 -08:00
c.commands = ['c']
c.example = '.c 5 + 3'
if __name__ == '__main__':
2014-01-06 07:10:13 -08:00
print __doc__.strip()