betabug... Sascha Welter

home english | home deutsch | Site Map | Sascha | Kontakt | Pro | Weblog | Wiki

15 July 2005

Zope Methods with a Dot in the name

Playing the game with setattr
 

Again one of those boring work problems: I have a bunch of Zope python / filesystem products. Most of them when instantiated contain an image called "preview.jpg". One of the little critters is different though. It wants to have a Zope Page Template (ZPT) instead of "preview.jpg". But Zope (and I think python) don't allow dots in method names. I banged my head on this particular wall for a while, until first Peter Bengtsson and then Chris McDonough gave me a big push on #zope. Read on for the solution...


What I have in the end is a class definition (in the .py file of my zope product, e.g. mythingy.py), like:

class mythingy( based_on_thingy_class ):
    """
    Pretends to have a preview.jpg
    """ 
    def __init__(self, id, title):
    ... and so on ...
    ... and so forth ...

    security.declareProtected( view, 'index_html' )
    index_html = PageTemplateFile(_www+'/thingy_index.zpt', \
        globals())

... and at the end of the file, ...
... outside the class definition, after the ...
... manage_add and manage_addForm methods, comes this:

setattr( \
    mythingy, \
    "preview.jpg", \
    PageTemplateFile(_www+'/zoom_index_html.zpt', globals()))
The magic part is the setattr, outside the class definition. This adds the attribute "preview.jpg" to the class. The attribute is the same for all instances of this class. That is fine with me, it's a ZPT, so it adjusts to whatever the instance has to show.

In the "setattr" line, the parameters might need some explaining (they needed for me :-). mythingy is the name of the class, naked without quotes. "preview.jpg" is the name that the "faked" method should be callable by, this one is a string, it has quotes around it. The last one (PageTemplateFile...) is the object that will be called. If I had a method called "gogo_method" on my class, I could as well have said:

setattr(mythingy, "preview.jpg", mythingy.gogo_method)
and the gogo_method would be called anytime someone requested preview.jpg. In my example above I could have done the same with mythingy.index_html to make it shorter.

Posted by betabug at 14:04 | Comments (0) | Trackbacks (0)
ch athens
Life in Athens (Greece) for a foreigner from the other side of the mountains. And with an interest in digital life and the feeling of change in a big city. Multilingual English - German - Greek.
Main blog page
Recent Entries
Best of
Some of the most sought after posts, judging from access logs and search engine queries.

Apple & Macintosh:
Security & Privacy:
Misc technical:
Athens for tourists and visitors:
Life in general:
<< Report from Karpenisi HelMUG Excursion | Main | Feierabend at the Beach >>
Comments
There are no comments.
Trackbacks
You can trackback to: http://betabug.ch/blogs/ch-athens/144/tbping
There are no trackbacks.
Leave a comment