comparison markup/eval.py @ 27:b8456279c444

* Fix the boilerplate in the Python source files. * Some more docstrings and cosmetic fixes.
author cmlenz
date Wed, 28 Jun 2006 09:28:09 +0000
parents ebe34f07d3eb
children 2ee9f28e16e5
comparison
equal deleted inserted replaced
26:039fc5b87405 27:b8456279c444
3 # Copyright (C) 2006 Christopher Lenz 3 # Copyright (C) 2006 Christopher Lenz
4 # All rights reserved. 4 # All rights reserved.
5 # 5 #
6 # This software is licensed as described in the file COPYING, which 6 # This software is licensed as described in the file COPYING, which
7 # you should have received as part of this distribution. The terms 7 # you should have received as part of this distribution. The terms
8 # are also available at http://trac.edgewall.com/license.html. 8 # are also available at http://markup.cmlenz.net/wiki/License.
9 # 9 #
10 # This software consists of voluntary contributions made by many 10 # This software consists of voluntary contributions made by many
11 # individuals. For the exact contribution history, see the revision 11 # individuals. For the exact contribution history, see the revision
12 # history and logs, available at http://projects.edgewall.com/trac/. 12 # history and logs, available at http://markup.cmlenz.net/log/.
13
14 """Support for "safe" evaluation of Python expressions."""
13 15
14 import __builtin__ 16 import __builtin__
15 import compiler 17 import compiler
16 import operator 18 import operator
17 19
99 """ 101 """
100 __slots__ = ['source', 'ast'] 102 __slots__ = ['source', 'ast']
101 __visitors = {} 103 __visitors = {}
102 104
103 def __init__(self, source): 105 def __init__(self, source):
106 """Create the expression.
107
108 @param source: the expression as string
109 """
104 self.source = source 110 self.source = source
105 self.ast = None 111 self.ast = None
106 112
107 def evaluate(self, data): 113 def evaluate(self, data):
114 """Evaluate the expression against the given data dictionary.
115
116 @param data: a mapping containing the data to evaluate against
117 @return: the result of the evaluation
118 """
108 if not self.ast: 119 if not self.ast:
109 self.ast = compiler.parse(self.source, 'eval') 120 self.ast = compiler.parse(self.source, 'eval')
110 return self._visit(self.ast.node, data) 121 return self._visit(self.ast.node, data)
111 122
112 def __repr__(self): 123 def __repr__(self):
Copyright (C) 2012-2017 Edgewall Software