[ZPT] Re: Acquiring PT local context (e.g. state of defines, repeat) within python scripts?

Jeff Kowalczyk jtk@yahoo.com
Fri, 27 Dec 2002 20:54:21 -0500


"Jeffrey P Shell" <jeffrey@cuemedia.com> wrote:
> I'd rather keep the argument lists long and specific than have
> unexpected consequences as the result of namespace pollution.  For one
> thing, the ``context.mystring`` bit could become confusing to another
> maintainer that's trying to determine where ``mystring`` is defined.
> This was a big problem with DTML - the "try to guess where THIS name
> came from!" game could lead to many hours of frustration.
>
> If your argument lists are getting too long and complex to manage, it
> could be considered a "bad smell" in refactoring terms.

You're right about that. I came to Zope too late to really get steeped in implicit
acquisition-think, and I don't want to pick up bad habits with Zope3 around the corner.

The scenario that caused this specific line of questioning is a tabular data-editing
application that tries to be very helpful to users with limited training. I'm customizing
the presentation of each row in the RDB table results to reflect business rules (e.g, how
the command hyperlinks are captioned, conditional input boxes vs rendered field values,
conditional CSS style of numeric values), and I end up passing several verbosely named
field values and repeat count/index values to all these short python scripts which are
self-documenting in their brevity but quickly become too numerous to keep all in one's
head.

I've avoided passing expansive parameters such as the entire result row, to keep the
python script with a defined interface. You extract specific values out of your 'extended'
structure to pass as parameters to the scripts, correct?

> It might be worthwhile to examine your code situation and evaluate
> different ways of doing it.  For some of my really complex pages,
> I use some pre-loader scripts like ``getExtendedDataFor(...)``,
> which usually return a mapping full of complex data.  I use them at
> the beginning of a page template (before calling any macros even)
> with something like::
>    <tal:pre define="global extended
> python:here.getExtendedDataForFoo(...)" />
>
> I've had decent success with this setup.
> "Explicit is better than implicit"