[ZPT] Re: [Vote] PEP308 voting began

Godefroid Chapelle gotcha@swing.be
Thu, 06 Mar 2003 10:04:46 +0100


Evan Simpson wrote:
> 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?

I think so. Iam much too often using "python:test(...." just for simple 
condition testing.
> 
> Cheers,
> 
> Evan @ 4-am