[Zope] Why is it so hard to do simple things?
Phil Harris
Phil Harris" <phil@harris-family.info
Wed, 8 May 2002 12:16:04 +0100
Who said Zope was getting more like Perl than Python:
There's more than one way to do it ;)
----- Original Message -----
From: "Chris McDonough" <chrism@zope.com>
To: "Chris McDonough" <chrism@zope.com>
Cc: "John Adams" <jadams@inktomi.com>; <zope@zope.org>
Sent: Wednesday, May 08, 2002 4:44 AM
Subject: Re: [Zope] Why is it so hard to do simple things?
> Whoops. I had it wrong last time around. One more time, with feeling:
>
> <dtml-let button_value="REQUEST.get('perm_or_temp', 'P')">
> <INPUT TYPE='RADIO'
> NAME=perm_or_temp
> VALUE="P"
> <dtml-if "button-value=='P'">
> CHECKED</dtml-if>
> > Permanent
> <INPUT TYPE='RADIO'
> NAME=perm_or_temp
> VALUE="T"
> <dtml-if "button-value=='T'>
> CHECKED</dtml-if>
> > Temporary
> </dtml-let>
>
> .. or ..
>
> button_value = context.REQUEST.get('perm_or_temp', 'P')
> possible = {'P':'Persistent','T':'Temporary'}
> items = possible.items()
> items.sort()
> for key, value in items:
> checked = (key == button_value) and ' CHECKED' or ''
> print ("<input type=radio name=perm_or_temp VALUE='%s'%s> %s" %
> (key, checked, value))
> return printed
>
> .. and a bonus alternate DTML implementation mirroring the Python one ..
>
> <dtml-let button_value="REQUEST.get('perm_or_temp', 'P')"
> possible="{'P':'Persistent','T':'Temporary'}"
> items="possible.items()">
> <dtml-call "items.sort()">
> <dtml-in items>
> <dtml-let "checked = (_[sequence-key] == button_value) and '
> CHECKED' or ''>
> <INPUT TYPE='RADIO'
> NAME=perm_or_temp
> VALUE="<dtml-var sequence-key>"
> <dtml-var checked>> <dtml-var sequence-item>
> </dtml-let>
> </dtml-in>
> </dtml-let>
>
> Chris McDonough wrote:
> > John Adams wrote:
> >
> >> If I was doing this in perl it'd be quite easy; I could check the query
> >> variable, and act on it -- but now I have to deal with things like not
> >> being able to even examine the variable without throwing an exception,
> >> and
> >> the fact that Python doesn't short-circuit blows.
> >>
> >> i.e. You can't examine something like:
> >>
> >> if (hasattr(request,'perm_or_temp') and request.perm_or_temp == 'P')
> >
> >
> > Python does indeed short-circuit this statement and it will work as you
> > seem to expect. Have you tried it?
> >
> > In DTML, here is something that will do what I think you want.
> >
> > <dtml-let button_value="REQUEST.get('perm_or_temp')">
> > <INPUT TYPE='RADIO'
> > NAME=perm_or_temp
> > VALUE="P"
> > <dtml-if "button-value=='P' or button_value==None">
> > CHECKED</dtml-if>
> > > Permanent
> > <INPUT TYPE='RADIO'
> > NAME=perm_or_temp
> > VALUE="T"
> > <dtml-if "button-value=='T'>
> > CHECKED</dtml-if>
> > > Temporary
> > </dtml-let>
> >
> > There's a million variations on this, some better than others. In
> > Python one way to do this would be as:
> >
> > button_value = context.REQUEST.get('perm_or_temp')
> > checked_button = {None: 'P', 'P':'P', 'T':'T'}.get(button_value)
> > for value in ['P', 'T']:
> > checked = (value == checked_button) and ' CHECKED' or ''
> > print ("<input type=radio name=perm_or_temp VALUE='%s'%s>" %
> > (value, checked))
> > return printed
> >
> > You could do something similar to this in DTML if you wanted as well.
> > Since there are only a couple of possible values, it's probably easier
> > just to spell it all out in this case.
> >
> > I'd suggest picking up the "Learning Python" O'Reilly book to learn
> > basic idioms like this. The thing you were missing was that
> > dictionary-like objects (of which REQUEST is one) have a get method that
> > returns None if nothing of that key exists in the dictionary on a
lookup.
> >
>
>
> --
> Chris McDonough Zope Corporation
> http://www.zope.org http://www.zope.com
> "Killing hundreds of birds with thousands of stones"
>
>
>
> _______________________________________________
> Zope maillist - Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> ** No cross posts or HTML encoding! **
> (Related lists -
> http://lists.zope.org/mailman/listinfo/zope-announce
> http://lists.zope.org/mailman/listinfo/zope-dev )
>
>