-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathsimple_eval__math.py
61 lines (44 loc) · 1.65 KB
/
simple_eval__math.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
import math
import inspect
# pip install simpleeval
from simpleeval import SimpleEval
# SOURCE: https://github.com/gil9red/SimplePyScripts/blob/6c512c0e0df249822ff0f46bc32faaf4f74d6e05/get_functions_from_object__module_class_object.py
def get_name_by_func(obj: object) -> dict:
is_ok = lambda name, func: inspect.isroutine(func) and not name.startswith("_")
return {name: func for name, func in inspect.getmembers(obj) if is_ok(name, func)}
class SimpleMathEval(SimpleEval):
def __init__(self):
names = {
"e": math.e,
"inf": math.inf,
"nan": math.nan,
"pi": math.pi,
"tau": math.tau,
"True": True,
"False": False,
}
functions = get_name_by_func(math)
super().__init__(names=names, functions=functions)
if __name__ == "__main__":
math_eval = SimpleMathEval()
print(math_eval.eval("pi ** 2")) # 9.869604401089358
print(math_eval.eval("pow(4, 2)")) # 16
print(math_eval.eval("4 ** 2")) # 16
print(math_eval.eval("log(10)")) # 2.302585092994046
print()
print(math_eval.eval("sin(4) ** 2 + cos(4) ** 2")) # 1.0
print(math_eval.eval("sin(4) ** 2 + cos(4) ** 2 == 1")) # True
print()
print(math_eval.eval("factorial(5)")) # 120
print(math_eval.eval("gcd(9, 3)")) # 3
print()
print(math_eval.eval("inf + 2")) # inf
print(math_eval.eval("inf + inf")) # inf
print()
print(math_eval.eval("nan + 2")) # nan
print(math_eval.eval("nan + nan")) # nan
print(math_eval.eval("nan + inf")) # nan
print()