Edgewall Software

ApiDocs/0.3.x/genshi.eval

genshi.eval

Support for "safe" evaluation of Python expressions.

Expression

Evaluates Python expressions used in templates.

>>> data = dict(test='Foo', items=[1, 2, 3], dict={'some': 'thing'})
>>> Expression('test').evaluate(data)
'Foo'
>>> Expression('items[0]').evaluate(data)
1
>>> Expression('items[-1]').evaluate(data)
3
>>> Expression('dict["some"]').evaluate(data)
'thing'

Similar to e.g. Javascript, expressions in templates can use the dot notation for attribute access to access items in mappings:

>>> Expression('dict.some').evaluate(data)
'thing'

This also works the other way around: item access can be used to access any object attribute (meaning there's no use for getattr() in templates):

>>> class MyClass(object):
...     myattr = 'Bar'
>>> data = dict(mine=MyClass(), key='myattr')
>>> Expression('mine.myattr').evaluate(data)
'Bar'
>>> Expression('mine["myattr"]').evaluate(data)
'Bar'
>>> Expression('mine[key]').evaluate(data)
'Bar'

All of the standard Python operators are available to template expressions. Built-in functions such as len() are also available in template expressions:

>>> data = dict(items=[1, 2, 3])
>>> Expression('len(items)').evaluate(data)
3

Undefined variables can be accessed in expressions, and evaluate to a non-truth value for tests. To check whether a particular variable is defined, its type can be compared to the special builtin class Undefined:

>>> Expression('foo').evaluate({})
undefined
>>> Expression('type(foo) is Undefined').evaluate({})
True

evaluate(self, data, nocall=False)

Evaluate the expression against the given data dictionary.

@param data: a mapping containing the data to evaluate against @param nocall: if true, the result of the evaluation is not called if

System Message: ERROR/3 (<string>, line 5)

Unexpected indentation.
if it is a callable

System Message: WARNING/2 (<string>, line 6)

Block quote ends without a blank line; unexpected unindent.

@return: the result of the evaluation

Undefined

Represents a reference to an undefined variable.

Unlike the Python runtime, template expressions can refer to an undefined variable without causing a NameError to be raised. The result will be an instance of the Undefined´ class, which is treated the same as `False in conditions, and acts as an empty collection in iterations:

>>> foo = Undefined('foo')
>>> bool(foo)
False
>>> list(foo)
[]
>>> print foo
undefined

However, calling an undefined variable, or trying to access an attribute of that variable, will raise an exception that includes the name used to reference that undefined variable.

>>> foo('bar')
Traceback (most recent call last):
    ...
NameError: Variable "foo" is not defined
>>> foo.bar
Traceback (most recent call last):
    ...
NameError: Variable "foo" is not defined

throw(self)

(Not documented)

ASTTransformer

General purpose base class for AST transformations.

Every visitor method can be overridden to return an AST node that has been altered or replaced in some way.

visit(self, node, **args, *kwargs)

(Not documented)

visitExpression(self, node, **args, *kwargs)

(Not documented)

visitCallFunc(self, node, **args, *kwargs)

(Not documented)

visitLambda(self, node, **args, *kwargs)

(Not documented)

visitGetattr(self, node, **args, *kwargs)

(Not documented)

visitSubscript(self, node, **args, *kwargs)

(Not documented)

visitCompare(self, node, **args, *kwargs)

(Not documented)

visitDict(self, node, **args, *kwargs)

(Not documented)

visitGenExpr(self, node, **args, *kwargs)

(Not documented)

visitGenExprFor(self, node, **args, *kwargs)

(Not documented)

visitGenExprIf(self, node, **args, *kwargs)

(Not documented)

visitGenExprInner(self, node, **args, *kwargs)

(Not documented)

visitKeyword(self, node, **args, *kwargs)

(Not documented)

visitList(self, node, **args, *kwargs)

(Not documented)

visitListComp(self, node, **args, *kwargs)

(Not documented)

visitListCompFor(self, node, **args, *kwargs)

(Not documented)

visitListCompIf(self, node, **args, *kwargs)

(Not documented)

visitSlice(self, node, **args, *kwargs)

(Not documented)

visitSliceobj(self, node, **args, *kwargs)

(Not documented)

visitTuple(self, node, **args, *kwargs)

(Not documented)

ExpressionASTTransformer

Concrete AST transformer that implements the AST transformations needed for template expressions.

visitConst(self, node, locals_=False)

(Not documented)

visitGenExprIf(self, node, **args, *kwargs)

(Not documented)

visitGenExprInner(self, node, **args, *kwargs)

(Not documented)

visitGetattr(self, node, locals_=False)

(Not documented)

visitLambda(self, node, locals_=False)

(Not documented)

visitListComp(self, node, locals_=False)

(Not documented)

visitName(self, node, locals_=False)

(Not documented)

visitSubscript(self, node, locals_=False)

(Not documented)


See ApiDocs/0.3.x, Documentation