-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
36 lines (27 loc) · 1.1 KB
/
utils.py
File metadata and controls
36 lines (27 loc) · 1.1 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
import ast
def extract_datafields(alpha_expression):
# 1. Sanitize: Make the string compatible with Python syntax parser
# Replace &&/|| with and/or, and remove trailing semicolons if necessary
alpha_expression = alpha_expression.replace("&&", " and ").replace("||", " or ")
# 2. Parse the code into a tree
tree = ast.parse(alpha_expression)
# 3. Collect identifiers
variables = set()
assignments = set()
operators = set()
for node in ast.walk(tree):
# Detect assignments
if isinstance(node, ast.Assign):
for target in node.targets:
if isinstance(target, ast.Name):
assignments.add(target.id)
# Detect operators
elif isinstance(node, ast.Call):
if isinstance(node.func, ast.Name):
operators.add(node.func.id)
# Detect all variables
elif isinstance(node, ast.Name):
variables.add(node.id)
# 4. Logic: Data Fields = Variables - (Operators + Assignments)
datafields = variables - assignments - operators
return sorted(datafields)