-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomath.py
More file actions
62 lines (53 loc) · 1.91 KB
/
domath.py
File metadata and controls
62 lines (53 loc) · 1.91 KB
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
62
#!/usr/bin/python3
import ast
import sys
import operator
from math_evaluator.explicit import calc as explicit_calc, op_map
from math_evaluator.implicit import calc as implicit_calc, valid_ops, allowed_types
# This depends entirely on the Math Evaluator package.
# see: https://medium.com/@r.harvey/how-i-made-a-math-evaluator-on-24-lines-65afe8e560fd
# and the code at: https://github.com/theRealProHacker/MathEvaluator
#
# Support both ** and ^ as power of...
valid_ops.add(ast.Pow)
op_map[ast.BitXor] = operator.__pow__
errmsg = "Malformed equation? The Python package Math Evaluator accepts:\n \
° operands\n \
· ints and\n \
· floats\n \
° operators\n \
· unary +-\n \
· binary +-*^**/ and\n \
· parentheses ()"
def domath(equation):
expression = equation
try:
result = implicit_calc(equation)
except Exception as e:
try:
result = explicit_calc(equation)
return(result)
except Exception as e:
print(f"{expression} errored out in explicit_calc with: {e}")
print(f"{errmsg}")
sys.exit(1)
print(f"{expression} errored out in implicit_calc with: {e}")
print(f"{errmsg}")
sys.exit(1)
return(result)
def main(num_args: int, usage: str):
if len(sys.argv) != num_args:
this_script = sys.argv[0]
usage_message = usage.replace("script_name", str(this_script))
print(usage_message)
sys.exit(1)
equation = sys.argv[1]
if equation != None:
# remove commas & $ signs from any of the numbers, if present
clean_equation_a = equation.replace(",", "")
clean_equation = clean_equation_a.replace("$", "")
print(f"{clean_equation} = {domath(clean_equation)}")
sys.exit()
if __name__ == "__main__":
# print(f"{str(sys.argv[1:])}")
main(2, 'USAGE: script_name <input_equation_in_quotes>')