Repository navigation
Expand file tree
/
Copy pathequation_parser.py
More file actions
149 lines (105 loc) · 4.29 KB
/
Copy pathequation_parser.py
File metadata and controls
149 lines (105 loc) · 4.29 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import ast
import sympy as sp
# Variables supported by Gradient Explorer
x, y = sp.symbols("x y")
# Mathematical functions users are allowed to enter
ALLOWED_FUNCTIONS = {
"sin": sp.sin,
"cos": sp.cos,
"tan": sp.tan,
"exp": sp.exp,
"log": sp.log,
"sqrt": sp.sqrt,
"abs": sp.Abs,
}
# Mathematical constants
ALLOWED_CONSTANTS = {
"pi": sp.pi,
"E": sp.E,
}
ALLOWED_VARIABLES = {
"x": x,
"y": y,
}
class EquationValidationError(ValueError):
"""Raised when an equation contains unsupported input."""
def _convert_node(node, depth=0):
"""Convert an allowlisted Python syntax tree into a SymPy expression."""
if depth > 20:
raise EquationValidationError("Equation is too deeply nested.")
if isinstance(node, ast.Constant):
if isinstance(node.value, bool):
raise EquationValidationError("Boolean values are not supported.")
if isinstance(node.value, (int, float)):
return sp.sympify(node.value)
raise EquationValidationError("Only numeric constants are allowed.")
if isinstance(node, ast.Name):
if node.id in ALLOWED_VARIABLES:
return ALLOWED_VARIABLES[node.id]
if node.id in ALLOWED_CONSTANTS:
return ALLOWED_CONSTANTS[node.id]
raise EquationValidationError(
f"'{node.id}' is not an allowed variable or constant."
)
if isinstance(node, ast.BinOp):
left = _convert_node(node.left, depth + 1)
right = _convert_node(node.right, depth + 1)
operations = {
ast.Add: lambda a, b: a + b,
ast.Sub: lambda a, b: a - b,
ast.Mult: lambda a, b: a * b,
ast.Div: lambda a, b: a / b,
ast.Pow: lambda a, b: a ** b,
}
operation = operations.get(type(node.op))
if operation is None:
raise EquationValidationError("This mathematical operator is not supported.")
if isinstance(node.op, ast.Pow):
if right.is_number and abs(float(right)) > 100:
raise EquationValidationError("Exponent magnitude cannot exceed 100.")
return operation(left, right)
if isinstance(node, ast.UnaryOp):
value = _convert_node(node.operand, depth + 1)
if isinstance(node.op, ast.UAdd):
return value
if isinstance(node.op, ast.USub):
return -value
raise EquationValidationError("This unary operator is not supported.")
if isinstance(node, ast.Call):
if not isinstance(node.func, ast.Name):
raise EquationValidationError("Only direct mathematical function calls are allowed.")
function = ALLOWED_FUNCTIONS.get(node.func.id)
if function is None:
raise EquationValidationError(
f"Function '{node.func.id}' is not supported."
)
if node.keywords or len(node.args) != 1:
raise EquationValidationError(
"Mathematical functions must have exactly one argument."
)
argument = _convert_node(node.args[0], depth + 1)
return function(argument)
raise EquationValidationError("The equation contains unsupported syntax.")
def parse_equation(equation: str):
"""Validate user input and return a SymPy expression in x and y."""
if not isinstance(equation, str) or not equation.strip():
raise EquationValidationError("Please enter a mathematical function.")
equation = equation.strip()
if len(equation) > 200:
raise EquationValidationError("Equation is too long. Maximum: 200 characters.")
# Accept ^ as a user-friendly alternative for **
equation = equation.replace("^", "**")
try:
syntax_tree = ast.parse(equation, mode="eval")
expression = _convert_node(syntax_tree.body)
if len(list(ast.walk(syntax_tree))) > 100:
raise EquationValidationError("Equation is too complex.")
if not expression.free_symbols.issubset({x, y}):
raise EquationValidationError("Only x and y are supported.")
return expression
except EquationValidationError:
raise
except (SyntaxError, TypeError, ValueError, ZeroDivisionError) as error:
raise EquationValidationError(
"Invalid equation. Check the mathematical syntax."
) from error