[ZPT] dictionaries, has_key, and the test function

Tino Wildenhain tino@wildenhain.de
Mon, 16 Dec 2002 19:21:58 +0100


Hi Tim,

--On Montag, 16. Dezember 2002 12:31 -0500 Tim Lynch 
<lynch@gould.mannlib.cornell.edu> wrote:

>
> 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.

The problem is: you are screwing up your template with logic. You
are introducing extraneus tags just to define variables.

Are you sure you dont have any other option to do what you want?

Just give the whole picture, not only the surrounding problem as
you described. Efficiency is not only to look at cpu cycles but
also to look at development cycles, because if you stick to a
clean and well tested application design you save time you can
invest in optimizing your algorithms.

Regards
Tino

>
>
> 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
>
> _______________________________________________
> ZPT mailing list
> ZPT@zope.org
> http://lists.zope.org/mailman/listinfo/zpt