8
8
from pandas .computation .ops import _arith_ops_syms , _unary_ops_syms
9
9
from pandas .computation .ops import Term , Constant
10
10
11
-
12
11
class Scope (object ):
13
12
__slots__ = 'globals' , 'locals'
14
13
@@ -21,7 +20,6 @@ def __init__(self, gbls=None, lcls=None, frame_level=1):
21
20
finally :
22
21
del frame
23
22
24
-
25
23
class ExprParserError (Exception ):
26
24
pass
27
25
@@ -49,53 +47,66 @@ def __init__(self, env):
49
47
lambda node , unary_op = unary_op : partial (UnaryOp , unary_op ))
50
48
self .env = env
51
49
52
- def visit (self , node ):
50
+ def generic_visit (self , node , ** kwargs ):
51
+ """Called if no explicit visitor function exists for a node."""
52
+ for field , value in iter_fields (node ):
53
+ if isinstance (value , list ):
54
+ for item in value :
55
+ if isinstance (item , AST ):
56
+ self .visit (item , ** kwargs )
57
+ elif isinstance (value , AST ):
58
+ self .visit (value , ** kwargs )
59
+
60
+ def visit (self , node , ** kwargs ):
53
61
if not (isinstance (node , ast .AST ) or isinstance (node , basestring )):
54
62
raise TypeError ('"node" must be an AST node or a string, you'
55
63
' passed a(n) {0}' .format (node .__class__ ))
56
64
if isinstance (node , basestring ):
57
65
node = ast .fix_missing_locations (ast .parse (node ))
58
- return super (ExprVisitor , self ).visit (node )
59
66
60
- def visit_Module (self , node ):
67
+ method = 'visit_' + node .__class__ .__name__
68
+ visitor = getattr (self , method , self .generic_visit )
69
+ return visitor (node , ** kwargs )
70
+
71
+ def visit_Module (self , node , ** kwargs ):
61
72
if len (node .body ) != 1 :
62
73
raise ExprParserError ('only a single expression is allowed' )
63
74
64
75
expr = node .body [0 ]
65
76
if not isinstance (expr , ast .Expr ):
66
77
raise SyntaxError ('only expressions are allowed' )
67
78
68
- return self .visit (expr )
79
+ return self .visit (expr , ** kwargs )
69
80
70
- def visit_Expr (self , node ):
71
- return self .visit (node .value )
81
+ def visit_Expr (self , node , ** kwargs ):
82
+ return self .visit (node .value , ** kwargs )
72
83
73
- def visit_BinOp (self , node ):
84
+ def visit_BinOp (self , node , ** kwargs ):
74
85
op = self .visit (node .op )
75
- left = self .visit (node .left )
76
- right = self .visit (node .right )
86
+ left = self .visit (node .left , side = 'left' )
87
+ right = self .visit (node .right , side = 'right' )
77
88
return op (left , right )
78
89
79
- def visit_UnaryOp (self , node ):
90
+ def visit_UnaryOp (self , node , ** kwargs ):
80
91
if isinstance (node .op , ast .Not ):
81
92
raise NotImplementedError ("not operator not yet supported" )
82
93
op = self .visit (node .op )
83
94
return op (self .visit (node .operand ))
84
95
85
- def visit_Name (self , node ):
96
+ def visit_Name (self , node , ** kwargs ):
86
97
return Term (node .id , self .env )
87
98
88
- def visit_Num (self , node ):
99
+ def visit_Num (self , node , ** kwargs ):
89
100
return Constant (node .n , self .env )
90
101
91
- def visit_Compare (self , node ):
102
+ def visit_Compare (self , node , ** kwargs ):
92
103
ops = node .ops
93
104
comps = node .comparators
94
105
if len (ops ) != 1 :
95
106
raise ExprParserError ('chained comparisons not supported' )
96
- return self .visit (ops [0 ])(self .visit (node .left ), self .visit (comps [0 ]))
107
+ return self .visit (ops [0 ])(self .visit (node .left , side = 'left' ), self .visit (comps [0 ], side = 'right' ))
97
108
98
- def visit_Call (self , node ):
109
+ def visit_Call (self , node , ** kwargs ):
99
110
if not isinstance (node .func , ast .Name ):
100
111
raise TypeError ("Only named functions are supported" )
101
112
@@ -106,10 +117,10 @@ def visit_Call(self, node):
106
117
107
118
raise NotImplementedError ("function calls not yet supported" )
108
119
109
- def visit_Attribute (self , node ):
120
+ def visit_Attribute (self , node , ** kwargs ):
110
121
raise NotImplementedError ("attribute access is not yet supported" )
111
122
112
- def visit_BoolOp (self , node ):
123
+ def visit_BoolOp (self , node , ** kwargs ):
113
124
raise NotImplementedError ("boolean operators are not yet supported" )
114
125
115
126
0 commit comments