[mod_python] PSP

Graham Dumpleton grahamd at dscpl.com.au
Thu Jul 27 18:12:49 EDT 2006

Julien Cigar wrote ..
> Hello,
> I'm using PSP as a template engine (with SQLAlchemy and a home-made 
> handler).
> One thing which is _VERY_ annoying in PSP is that if a variable doesn't
> exist in the current global namespace, the script fails with a stack 
> trace ... rather than output a blank for example.
> I had resolved this problem partially with a custom function (a sort of
> templatetag) : http://rafb.net/paste/results/0BqTEk48.html
> With this function I could do things like <%= 
> access('invasive.scientific_name.name', globals()) %>, however this 
> doesn't work with SQLAlchemy because the variable doesn't exists in the
> current namespace until it has been accessed (to avoid useless database
> accesses) ...
> I think it will be a good idea to have a sort of access() function in 
> the next mod_python release which output a blank (or ...) if the 
> variable doesn't exist ..
> Does anyone has an elegant solution to resolve this problem (other than
> a <% try: %> .... <% except: %> ... ?

You could use:

  'y' in globals() and y or ''

or in your case:

  'name' in invasive.scientific_name and invasive.scientific_name.name or ''

Either way it is going to be messy.

In respect of changing PSP, don't think it could be changed to automatically
translate non existent variable references to an empty string as it is being
executed as Python code and not by some higher level translation layer that
could meaningfully generate such an alternate form.


More information about the Mod_python mailing list