Mercurial > genshi > mirror
comparison genshi/eval.py @ 388:31dc3d18b39e stable-0.3.x
0.3.x branch: add unit test for `Undefined` checks in template expressions.
author | cmlenz |
---|---|
date | Mon, 11 Dec 2006 12:06:28 +0000 |
parents | b12c045c1e81 |
children |
comparison
equal
deleted
inserted
replaced
373:a52452a65cd7 | 388:31dc3d18b39e |
---|---|
59 expressions: | 59 expressions: |
60 | 60 |
61 >>> data = dict(items=[1, 2, 3]) | 61 >>> data = dict(items=[1, 2, 3]) |
62 >>> Expression('len(items)').evaluate(data) | 62 >>> Expression('len(items)').evaluate(data) |
63 3 | 63 3 |
64 | |
65 Undefined variables can be accessed in expressions, and evaluate to a | |
66 non-truth value for tests. To check whether a particular variable is | |
67 defined, its type can be compared to the special builtin class `Undefined`: | |
68 | |
69 >>> Expression('foo').evaluate({}) | |
70 undefined | |
71 >>> Expression('type(foo) is Undefined').evaluate({}) | |
72 True | |
64 """ | 73 """ |
65 __slots__ = ['source', 'code'] | 74 __slots__ = ['source', 'code'] |
66 | 75 |
67 def __init__(self, source, filename=None, lineno=-1): | 76 def __init__(self, source, filename=None, lineno=-1): |
68 if isinstance(source, basestring): | 77 if isinstance(source, basestring): |