-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into release-0.22.0
- Loading branch information
Showing
7 changed files
with
394 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import ast | ||
|
||
|
||
class SafeEvalVisitor(ast.NodeVisitor): | ||
def __init__(self, dataset_map): | ||
self.dataset_map = dataset_map | ||
|
||
def evaluate(self, tree): | ||
return self.visit(tree) | ||
|
||
def visit_Expression(self, node): | ||
return self.visit(node.body) | ||
|
||
def visit_BinOp(self, node): | ||
left = self.visit(node.left) | ||
right = self.visit(node.right) | ||
|
||
if isinstance(node.op, ast.BitAnd): | ||
return left & right | ||
elif isinstance(node.op, ast.BitOr): | ||
return left | right | ||
else: | ||
raise ValueError(f"Unsupported binary operation: {type(node.op).__name__}") | ||
|
||
def visit_Name(self, node): | ||
if node.id in self.dataset_map: | ||
return self.dataset_map[node.id] | ||
raise NameError(f"Undefined variable: {node.id}") | ||
|
||
def visit_Constant(self, node): | ||
return node.value | ||
|
||
def generic_visit(self, node): | ||
raise ValueError(f"Unsupported syntax: {type(node).__name__}") |
Oops, something went wrong.