annotate markup/eval.py @ 32:2224a52256ca trunk

A couple more operators supported in expressions.
author cmlenz
date Wed, 28 Jun 2006 19:13:47 +0000
parents 2ab5fa60575d
children 28ddb79414b2
rev   line source
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
2 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2006 Christopher Lenz
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
5 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
8 # are also available at http://markup.cmlenz.net/wiki/License.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
9 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
12 # history and logs, available at http://markup.cmlenz.net/log/.
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
13
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
14 """Support for "safe" evaluation of Python expressions."""
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
15
32
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
16 from __future__ import division
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
17
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
18 import __builtin__
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
19 try:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
20 import _ast # Python 2.5
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
21 except ImportError:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
22 _ast = None
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
23 import compiler
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
24 import operator
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
25
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
26 from markup.core import Stream
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
27
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
28 __all__ = ['Expression']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
29
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
30
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
31 class Expression(object):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
32 """Evaluates Python expressions used in templates.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
33
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
34 >>> data = dict(test='Foo', items=[1, 2, 3], dict={'some': 'thing'})
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
35 >>> Expression('test').evaluate(data)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
36 'Foo'
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
37 >>> Expression('items[0]').evaluate(data)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
38 1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
39 >>> Expression('items[-1]').evaluate(data)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
40 3
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
41 >>> Expression('dict["some"]').evaluate(data)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
42 'thing'
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
43
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
44 Similar to e.g. Javascript, expressions in templates can use the dot
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
45 notation for attribute access to access items in mappings:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
46
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
47 >>> Expression('dict.some').evaluate(data)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
48 'thing'
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
49
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
50 This also works the other way around: item access can be used to access
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
51 any object attribute (meaning there's no use for `getattr()` in templates):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
52
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
53 >>> class MyClass(object):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
54 ... myattr = 'Bar'
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
55 >>> data = dict(mine=MyClass(), key='myattr')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
56 >>> Expression('mine.myattr').evaluate(data)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
57 'Bar'
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
58 >>> Expression('mine["myattr"]').evaluate(data)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
59 'Bar'
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
60 >>> Expression('mine[key]').evaluate(data)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
61 'Bar'
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
62
31
2ab5fa60575d * More test cases for expression evaluation.
cmlenz
parents: 30
diff changeset
63 All of the standard Python operators are available to template expressions.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
64 Built-in functions such as `len()` are also available in template
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
65 expressions:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
66
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
67 >>> data = dict(items=[1, 2, 3])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
68 >>> Expression('len(items)').evaluate(data)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
69 3
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
70 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
71 __slots__ = ['source', 'ast']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
72 __visitors = {}
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
73
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
74 def __init__(self, source):
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
75 """Create the expression.
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
76
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
77 @param source: the expression as string
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
78 """
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
79 self.source = source
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
80 self.ast = None
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
81
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
82 def __repr__(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
83 return '<Expression "%s">' % self.source
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
84
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
85 if _ast is None:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
86
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
87 def evaluate(self, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
88 """Evaluate the expression against the given data dictionary.
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
89
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
90 @param data: a mapping containing the data to evaluate against
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
91 @return: the result of the evaluation
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
92 """
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
93 if not self.ast:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
94 self.ast = compiler.parse(self.source, 'eval')
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
95 return self._visit(self.ast.node, data)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
96
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
97 # AST traversal
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
98
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
99 def _visit(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
100 v = self.__visitors.get(node.__class__)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
101 if not v:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
102 v = getattr(self, '_visit_%s' % node.__class__.__name__.lower())
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
103 self.__visitors[node.__class__] = v
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
104 return v(node, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
105
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
106 def _visit_expression(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
107 for child in node.getChildNodes():
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
108 return self._visit(child, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
109
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
110 # Functions & Accessors
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
111
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
112 def _visit_callfunc(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
113 func = self._visit(node.node, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
114 if func is None:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
115 return None
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
116 args = [self._visit(arg, data) for arg in node.args
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
117 if not isinstance(arg, compiler.ast.Keyword)]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
118 kwargs = dict([(arg.name, self._visit(arg.expr, data)) for arg
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
119 in node.args if isinstance(arg, compiler.ast.Keyword)])
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
120 return func(*args, **kwargs)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
121
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
122 def _visit_getattr(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
123 obj = self._visit(node.expr, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
124 if hasattr(obj, node.attrname):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
125 return getattr(obj, node.attrname)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
126 try:
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
127 return obj[node.attrname]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
128 except TypeError:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
129 return None
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
130
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
131 def _visit_slice(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
132 obj = self._visit(node.expr, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
133 lower = node.lower and self._visit(node.lower, data) or None
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
134 upper = node.upper and self._visit(node.upper, data) or None
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
135 return obj[lower:upper]
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
136
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
137 def _visit_subscript(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
138 obj = self._visit(node.expr, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
139 subs = map(lambda sub: self._visit(sub, data), node.subs)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
140 if len(subs) == 1:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
141 subs = subs[0]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
142 try:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
143 return obj[subs]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
144 except (KeyError, IndexError, TypeError):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
145 try:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
146 return getattr(obj, subs)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
147 except (AttributeError, TypeError):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
148 return None
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
149
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
150 # Operators
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
151
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
152 def _visit_and(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
153 return reduce(lambda x, y: x and y,
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
154 [self._visit(n, data) for n in node.nodes])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
155
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
156 def _visit_or(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
157 return reduce(lambda x, y: x or y,
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
158 [self._visit(n, data) for n in node.nodes])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
159
32
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
160 def _visit_bitand(self, node, data):
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
161 return reduce(operator.and_,
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
162 [self._visit(n, data) for n in node.nodes])
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
163
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
164 def _visit_bitor(self, node, data):
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
165 return reduce(operator.or_,
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
166 [self._visit(n, data) for n in node.nodes])
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
167
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
168 _OP_MAP = {'==': operator.eq, '!=': operator.ne,
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
169 '<': operator.lt, '<=': operator.le,
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
170 '>': operator.gt, '>=': operator.ge,
32
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
171 'is': operator.is_, 'is not': operator.is_not,
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
172 'in': lambda x, y: operator.contains(y, x),
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
173 'not in': lambda x, y: not operator.contains(y, x)}
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
174 def _visit_compare(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
175 result = self._visit(node.expr, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
176 ops = node.ops[:]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
177 ops.reverse()
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
178 for op, rval in ops:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
179 result = self._OP_MAP[op](result, self._visit(rval, data))
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
180 return result
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
181
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
182 def _visit_add(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
183 return self._visit(node.left, data) + self._visit(node.right, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
184
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
185 def _visit_div(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
186 return self._visit(node.left, data) / self._visit(node.right, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
187
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
188 def _visit_floordiv(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
189 return self._visit(node.left, data) // self._visit(node.right, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
190
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
191 def _visit_mod(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
192 return self._visit(node.left, data) % self._visit(node.right, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
193
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
194 def _visit_mul(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
195 return self._visit(node.left, data) * self._visit(node.right, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
196
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
197 def _visit_power(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
198 return self._visit(node.left, data) ** self._visit(node.right, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
199
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
200 def _visit_sub(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
201 return self._visit(node.left, data) - self._visit(node.right, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
202
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
203 def _visit_unaryadd(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
204 return +self._visit(node.expr, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
205
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
206 def _visit_unarysub(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
207 return -self._visit(node.expr, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
208
32
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
209 def _visit_not(self, node, data):
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
210 return not self._visit(node.expr, data)
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
211
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
212 def _visit_invert(self, node, data):
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
213 return ~self._visit(node.expr, data)
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
214
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
215 # Identifiers & Literals
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
216
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
217 def _visit_name(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
218 val = data.get(node.name)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
219 if val is None:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
220 val = getattr(__builtin__, node.name, None)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
221 return val
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
222
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
223 def _visit_const(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
224 return node.value
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
225
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
226 def _visit_dict(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
227 return dict([(self._visit(k, data), self._visit(v, data))
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
228 for k, v in node.items])
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
229
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
230 def _visit_tuple(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
231 return tuple([self._visit(n, data) for n in node.nodes])
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
232
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
233 def _visit_list(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
234 return [self._visit(n, data) for n in node.nodes]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
235
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
236 else:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
237
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
238 def evaluate(self, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
239 """Evaluate the expression against the given data dictionary.
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
240
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
241 @param data: a mapping containing the data to evaluate against
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
242 @return: the result of the evaluation
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
243 """
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
244 if not self.ast:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
245 self.ast = compile(self.source, '?', 'eval', 0x400)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
246 return self._visit(self.ast, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
247
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
248 # AST traversal
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
249
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
250 def _visit(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
251 v = self.__visitors.get(node.__class__)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
252 if not v:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
253 v = getattr(self, '_visit_%s' % node.__class__.__name__.lower())
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
254 self.__visitors[node.__class__] = v
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
255 return v(node, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
256
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
257 def _visit_expression(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
258 return self._visit(node.body, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
259
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
260 # Functions & Accessors
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
261
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
262 def _visit_attribute(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
263 obj = self._visit(node.value, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
264 if hasattr(obj, node.attr):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
265 return getattr(obj, node.attr)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
266 try:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
267 return obj[node.attr]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
268 except TypeError:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
269 return None
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
270
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
271 def _visit_call(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
272 func = self._visit(node.func, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
273 if func is None:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
274 return None
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
275 args = [self._visit(arg, data) for arg in node.args]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
276 kwargs = dict([(kwarg.arg, self._visit(kwarg.value, data))
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
277 for kwarg in node.keywords])
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
278 return func(*args, **kwargs)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
279
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
280 def _visit_subscript(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
281 obj = self._visit(node.value, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
282 if isinstance(node.slice, _ast.Slice):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
283 try:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
284 return obj[self._visit(lower, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
285 self._visit(upper, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
286 self._visit(step, data)]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
287 except (KeyError, IndexError, TypeError):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
288 pass
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
289 else:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
290 index = self._visit(node.slice.value, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
291 try:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
292 return obj[index]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
293 except (KeyError, IndexError, TypeError):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
294 try:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
295 return getattr(obj, index)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
296 except (AttributeError, TypeError):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
297 pass
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
298 return None
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
299
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
300 # Operators
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
301
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
302 _OP_MAP = {_ast.Add: operator.add, _ast.And: lambda l, r: l and r,
32
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
303 _ast.BitAnd: operator.and_, _ast.BitOr: operator.or_,
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
304 _ast.Div: operator.truediv, _ast.Eq: operator.eq,
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
305 _ast.FloorDiv: operator.floordiv, _ast.Gt: operator.gt,
32
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
306 _ast.GtE: operator.ge, _ast.Invert: operator.inv,
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
307 _ast.In: lambda l, r: operator.contains(r, l),
32
2224a52256ca A couple more operators supported in expressions.
cmlenz
parents: 31
diff changeset
308 _ast.Is: operator.is_, _ast.IsNot: operator.is_not,
31
2ab5fa60575d * More test cases for expression evaluation.
cmlenz
parents: 30
diff changeset
309 _ast.Lt: operator.lt, _ast.LtE: operator.le,
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
310 _ast.Mod: operator.mod, _ast.Mult: operator.mul,
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
311 _ast.Not: operator.not_, _ast.NotEq: operator.ne,
31
2ab5fa60575d * More test cases for expression evaluation.
cmlenz
parents: 30
diff changeset
312 _ast.NotIn: lambda l, r: not operator.contains(r, l),
30
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
313 _ast.Or: lambda l, r: l or r, _ast.Pow: operator.pow,
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
314 _ast.Sub: operator.sub, _ast.UAdd: operator.pos,
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
315 _ast.USub: operator.neg}
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
316
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
317 def _visit_unaryop(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
318 return self._OP_MAP[node.op.__class__](self._visit(node.operand, data))
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
319
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
320 def _visit_binop(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
321 return self._OP_MAP[node.op.__class__](self._visit(node.left, data),
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
322 self._visit(node.right, data))
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
323
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
324 def _visit_boolop(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
325 return reduce(self._OP_MAP[node.op.__class__],
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
326 [self._visit(n, data) for n in node.values])
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
327
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
328 def _visit_compare(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
329 result = self._visit(node.left, data)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
330 ops = node.ops[:]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
331 ops.reverse()
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
332 for op, rval in zip(ops, node.comparators):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
333 result = self._OP_MAP[op.__class__](result,
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
334 self._visit(rval, data))
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
335 return result
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
336
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
337 # Identifiers & Literals
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
338
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
339 def _visit_dict(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
340 return dict([(self._visit(k, data), self._visit(v, data))
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
341 for k, v in zip(node.keys, node.values)])
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
342
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
343 def _visit_list(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
344 return [self._visit(n, data) for n in node.elts]
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
345
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
346 def _visit_name(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
347 val = data.get(node.id)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
348 if val is None:
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
349 val = getattr(__builtin__, node.id, None)
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
350 return val
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
351
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
352 def _visit_num(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
353 return node.n
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
354
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
355 def _visit_str(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
356 return node.s
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
357
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
358 def _visit_tuple(self, node, data):
bcdbb7e5e4e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
359 return tuple([self._visit(n, data) for n in node.elts])
Copyright (C) 2012-2017 Edgewall Software