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

45 lines
986 B
Python
Raw Normal View History

2023-01-19 16:45:15 +00:00
import traceback
2023-01-19 15:10:06 +00:00
from constant import consts
2023-01-19 16:45:15 +00:00
from simpleeval import SimpleEval, DEFAULT_FUNCTIONS, DEFAULT_NAMES, DEFAULT_OPERATORS
2023-01-19 15:10:06 +00:00
import ast
import sys
import operator as op
import math
import statistics
funcs = {}
def add_func(module):
for name in dir(math):
item = getattr(math, 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,
2023-01-19 16:45:15 +00:00
}, )
2023-01-19 15:10:06 +00:00
2023-01-19 16:45:15 +00:00
try: # rina's rina solution :rina:
print('Result ' + str(s_eval.eval(' '.join(sys.argv[1:]))))
except Exception as e:
print('Failed ' + str(e))