[Zope] Python script check for existence of variable - there has to be a better way to do this.

Ria Marinussen marinus@cs.utwente.nl
Thu, 13 Jun 2002 11:24:06 +0200


Hi,

In a database I have some information about our employees. Some fields are
not filled in, so there value is NULL.
When iterating over the results of a ZSQL query (select * from
"Medewerkers") the non-existent variables (value=NULL) are reported with the
word "None".
I don't want show this keyword in my report-table, so I've created a python
script that checks for the existence of the variable, and when it doesn't
exist, I fill in a non-breaking space html -code.

Basically I do two things in the script: I check the parameter and generate
the name of the variable (that has to be checked)based on the parameter
entered. Next I generate the output and return it.

I'm convinced that the first part (check parameter, generate variablenaam to
check) can be done in a more elegant way (less code), but trying to
concatenate the value of the parameter to "context." isn't working....

Ideas, anyone?


I use this (and more) in my dtml-document:
<table>
 <tr>
     <td><dtml-var "bestaat(test='voorletters')"></td>
     <td><dtml-var "bestaat(test='voorvoegsel')"></td>
     <td><dtml-var "bestaat(test='achternaam')"></td>
 </tr>
</table>

The script is named "bestaat":
## Parameter list  test
## Bound Names  context, container, script, _, traverse_subpath 
# Check the input and set the variable to check
if test == 'voorletters':
   quest = context.voorletters
elif test == 'voorvoegsel':
   quest = context.voorvoegsel
elif test == 'titel':
   quest = context.titel
elif test == 'telefoon':
   quest = context.telefoon
elif test == 'telefoon_bgg':
   quest = context.telefoon_bgg
elif test == 'login':
   quest = context.login
elif test == 'leerstoel':
   quest = context.leerstoel
elif test == 'kamernr':
   quest = context.kamernr
elif test == 'geslacht':
   quest = context.geslacht
elif test == 'foto':
   quest = context.foto
elif test == 'faculteit1':
   quest = context.faculteit1
elif test == 'faculteit2':
   quest = context.faculteit2
elif test == 'emailadres':
   quest = context.emailadres
elif test == 'achternaam':
   quest = context.achternaam
else:
   quest = 'missing'

# Generate the output
if not quest:
   regel='&nbsp;'
else:
   regel=quest

return regel

TIA,

Ria Marinussen
Webmaster Department of Computer Science
University of Twente
The Netherlands