[Zope] Problem with a python script

Passin, Tom tpassin@mitretek.org
Fri, 23 May 2003 10:16:40 -0400


[Andreas Pakulat]
> got a really weird problem here:
> Error Type: ValueError
> Error Value: unsupported format character '"' (0x22) at index 45
>=20
> testing the following python script:
>=20
>....
> fmtstr +=3D ' href=3D"index_html?activetab=3D%d">%s</a></td>'=20
> %(lastelem,tabs[lastelem])
> fmtstr +=3D '<td class=3D"headerborderwhite"><img src=3D"%s" ' %(isrc)
> fmtstr +=3D ' width=3D"10" height=3D"10"=20
> style=3D"display:block;width:2em;height:2em"></td></tr></table></td>'
> fmtstr +=3D '<td=20
> style=3D"background-color:#FFFFFF;width:95%"><img src=3D"%s"' %(spsrc)
>=20
> fmtstr +=3D ' width=3D"3" height=3D"3"></td></tr>'
>=20
> return fmtstr

Even though Andreas is happy to have found the error, I would like to
urge that Python scripts not be used to return markup at all.  Instead,
return a data structure to the calling page and let the page format the
data - either with dhtml or zpt, it doesn't matter in this regard.

Writing markup with Python has two big problems, good though Python is -

1) It is really hard to get markup right, as Andreas's example shows.
Little errors are easy to make and hard to see.

2) If you want to change the look of the page, you have to rewrite your
script, which is painful and prone to the same errors all over again.

Using Zope templates, on the other hand, has the following strengths -

1)  They are designed exactly for taking data and formatting it into
markup, so it is much easier to get the markup right and to spot errors.

2) Page maintenance and changes in appearance are easy to do and do not
affect the logic used in the scripts that create the data.

All you have to do is have your script return the results of its
computation in the form of a string, a list, a tuple, or a dictionary.
The template can access all the returned data and format it.

I have done it both ways - with pretty complex pages -  and I can tell
you that it is worth the effort to change your mental habits (which is
what it takes) to find ways to just return data and not markup.  Let
scripts do what they do well, and let the templates do what __they__ do
well.