[ZPT] Re: [Vote] PEP308 voting began
Evan Simpson
evan@4-am.com
Tue, 04 Mar 2003 15:21:57 -0600
Guido van Rossum wrote:
> IMO TALES should solve this for itself by introducing an if/then/else
> expression form rather than depending on Python. If you can have a
> "not:.." expression, surely you can have an "if:..:then:..:else:.."
> expression.
Now that you point it out, it's not even hard. Here's a
proof-of-concept, with really awful parsing (it obviously breaks on
nested if: then: else:), that actually works:
class IfExpr:
def __init__(self, name, expr, compiler):
self._s = expr = expr.lstrip()
m = re.match('(.*) then:(.*) else:(.*)', expr)
if m is not None:
condexpr, thenexpr, elseexpr = m.groups()
self._cond = compiler.compile(condexpr)
self._then = compiler.compile(thenexpr)
self._else = compiler.compile(elseexpr)
def __call__(self, econtext):
if econtext.evaluateBoolean(self._cond):
return econtext.evaluate(self._then)
return econtext.evaluate(self._else)
(Tested with
<div tal:replace="if:options/x then:string:yes else:string:no">)
Is this worth a robust implementation, ZPT folks?
Cheers,
Evan @ 4-am