Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/calc/calc.py
2023-01-20 15:03:41 +08:00

44 lines
995 B
Python

import traceback
from constant import consts
from simpleeval import SimpleEval, DEFAULT_FUNCTIONS, DEFAULT_NAMES, DEFAULT_OPERATORS
import ast
import sys
import operator as op
import math
import statistics
funcs = {}
def add_func(module):
for name in dir(module):
item = getattr(module, name)
if not name.startswith('_') and callable(item):
funcs[name] = item
add_func(math)
add_func(statistics)
s_eval = SimpleEval(
operators={
**DEFAULT_OPERATORS,
ast.BitOr: op.or_,
ast.BitAnd: op.and_,
ast.BitXor: op.xor,
ast.Invert: op.invert,
},
functions={**funcs, **DEFAULT_FUNCTIONS},
names={
**DEFAULT_NAMES, **consts,
'pi': math.pi,
'e': math.e,
'tau': math.tau,
'inf': math.inf, 'nan': math.nan,
}, )
try: # rina's rina lazy solution :rina:
print('Result ' + str(s_eval.eval(' '.join(sys.argv[1:]))))
except Exception as e:
print('Failed ' + str(e))