comparison markup/eval.py @ 118:c392d38694d9 trunk

Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
author cmlenz
date Tue, 01 Aug 2006 22:34:39 +0000
parents c77c113846d6
children c9f0a26e28a2
comparison
equal deleted inserted replaced
117:f96b04b0db96 118:c392d38694d9
80 """Evaluate the expression against the given data dictionary. 80 """Evaluate the expression against the given data dictionary.
81 81
82 @param data: a mapping containing the data to evaluate against 82 @param data: a mapping containing the data to evaluate against
83 @return: the result of the evaluation 83 @return: the result of the evaluation
84 """ 84 """
85 retval = eval(self.code) 85 retval = eval(self.code, {'data': data,
86 '_lookup_name': _lookup_name,
87 '_lookup_attr': _lookup_attr,
88 '_lookup_item': _lookup_item})
86 if callable(retval): 89 if callable(retval):
87 retval = retval() 90 retval = retval()
88 return retval 91 return retval
89 92
90 93
110 val = locals.get(name) 113 val = locals.get(name)
111 if val is None: 114 if val is None:
112 val = getattr(__builtin__, name, None) 115 val = getattr(__builtin__, name, None)
113 return val 116 return val
114 117
115 def _lookup_attribute(data, obj, key): 118 def _lookup_attr(data, obj, key):
116 if hasattr(obj, key): 119 if hasattr(obj, key):
117 return getattr(obj, key) 120 return getattr(obj, key)
118 try: 121 try:
119 return obj[key] 122 return obj[key]
120 except (KeyError, TypeError): 123 except (KeyError, TypeError):
163 if node.dstar_args: 166 if node.dstar_args:
164 node.dstart_args = map(lambda x: self.visit(x, *args, **kwargs), 167 node.dstart_args = map(lambda x: self.visit(x, *args, **kwargs),
165 node.dstar_args) 168 node.dstar_args)
166 return node 169 return node
167 170
171 def visitLambda(self, node, *args, **kwargs):
172 node.code = self.visit(node.code, *args, **kwargs)
173 node.filename = '<string>' # workaround for bug in pycodegen
174 return node
175
168 def visitGetattr(self, node, *args, **kwargs): 176 def visitGetattr(self, node, *args, **kwargs):
169 node.expr = self.visit(node.expr, *args, **kwargs) 177 node.expr = self.visit(node.expr, *args, **kwargs)
170 return node 178 return node
171 179
172 def visitSubscript(self, node, *args, **kwargs): 180 def visitSubscript(self, node, *args, **kwargs):
246 """Concrete AST transformer that implements the AST transformations needed 254 """Concrete AST transformer that implements the AST transformations needed
247 for template expressions. 255 for template expressions.
248 """ 256 """
249 257
250 def visitGetattr(self, node, *args, **kwargs): 258 def visitGetattr(self, node, *args, **kwargs):
251 return ast.CallFunc(ast.Name('_lookup_attribute'), 259 return ast.CallFunc(ast.Name('_lookup_attr'),
252 [ast.Name('data'), self.visit(node.expr, *args, **kwargs), 260 [ast.Name('data'), self.visit(node.expr, *args, **kwargs),
253 ast.Const(node.attrname)] 261 ast.Const(node.attrname)]
254 ) 262 )
263
264 def visitLambda(self, node, *args, **kwargs):
265 old_lookup_locals = kwargs.get('lookup_locals', False)
266 kwargs['lookup_locals'] = True
267 node.code = self.visit(node.code, *args, **kwargs)
268 node.filename = '<string>' # workaround for bug in pycodegen
269 kwargs['lookup_locals'] = old_lookup_locals
270 return node
255 271
256 def visitListComp(self, node, *args, **kwargs): 272 def visitListComp(self, node, *args, **kwargs):
257 old_lookup_locals = kwargs.get('lookup_locals', False) 273 old_lookup_locals = kwargs.get('lookup_locals', False)
258 kwargs['lookup_locals'] = True 274 kwargs['lookup_locals'] = True
259 node.expr = self.visit(node.expr, *args, **kwargs) 275 node.expr = self.visit(node.expr, *args, **kwargs)
Copyright (C) 2012-2017 Edgewall Software