[ZPT] dictionaries, has_key, and the test function

Tim Lynch lynch@gould.mannlib.cornell.edu
Mon, 16 Dec 2002 12:31:35 EST


thanks to all who responded to my question!  All the proposed solutions
worked in my situation.


if I could ask a follow-up, of the proposed solutions that have come in
over the past few days, does anyone see an approach that might be
significantly faster?  Can't say I've done any benchmarking to verify
my impression, but Willem's solution below does seem the fastest.


thanks again everyone for your help

- Tim Lynch




> Tim Lynch wrote:
> > <span tal:condition="python:test(results.has_key('parents'), 1, 0)">
> >    <span tal:define="parents python:results['parents']">
> >        do stuff
> >    </span>
> > </span>
> 
> The following seems to be equivalent and quite elegant:
> 
> 
>    <span tal:define="parents results/parents | nothing"
>          tal:condition="parents">
> 
>      do stuff
> 
>    </span>
> 
> 
> - It uses "|" to give an alternative value in case the first one
>    fails (I probably got this trick from the Zope Book Advanced
>    ZPT page).
> 
> - "object/x" will first try "object.x" (attribute), then
>    "object[x]" (key lookup), so no "python:" necessary to access
>    the key.
> 
> - As 'tal:define' is executed before 'tal:condition', no need
>    for a subtag just for the logic.
> 
> 
> Would be great if Evan could decide not to remove that tal:define ;)
> 
> 
> - Willem
> 
> 
> _______________________________________________
> ZPT mailing list
> ZPT@zope.org
> http://lists.zope.org/mailman/listinfo/zpt
> 


--
Tim Lynch                            tim.lynch@cornell.edu
Information Technology Section                607.255.9570
Albert R. Mann Library
Cornell University
Ithaca, NY 14853