[mod_python] using callable classes instead of plain functions in handlers

Martin Slouf xslom03 at vse.cz
Wed Sep 29 15:28:30 EDT 2004

hi daniel,

first of all, thanks for the answer.

finally i got some time and i am looking at the mod_python.servlet module right
now (browsing the tutorial) and it looks good with some nice features (form
controls parsing and even the call to Session.save() is nice :), but let me
tell you how i handle requests now and pls tell me if there might be some
problems with that, or if there are better ways how to do it in

1. im bound to use PSP -- graphics and gui features are done by another guy, we
just cooperate on form fields, i think that it is no problem with
mod_python.servlet (i agree, that code generated HTML definitely has its
advances, but i cant use it now.)

to use PSP properly i would just call return the psp page from the
'write_content' method, right?

2. i use mod_python.publisher to great extend and the logic behind most forms

say, we got 1 simple registration form that collects just user's login and
password, all is in 'registration.py' file with two handlers:

- registration.py/registration (html form page)
- registration.py/registrate (processing of the request)

the 1st one does the PSP page output (sets form controls to whatever the user
typed in or to defaults, if presented for the 1st time)

the 2nd handles the actual request of 'sending a form', typically it lokks like

	a) does whatever it has to do if data are ok

	b) if any errors are found, any cleanup actions are done if required
	and then the 1st method is called with additional parameter specifying
	all the raised errors -- so the only PSP involved is used to inform
	user about the errors also and the form is with no special care filled
	with whatever the user typed in

this behaviour i found very usefull and suits for most cases. (and this
behaviour i would implement in my servlet base class).  thx to
mod_python.publisher i can have several functions in one file, sharing some
data, and what is best, call several handlers even though im processing single
request and handle errors in this transparent way (case ad b)).

now, finally, the question for you:

with mod_python.servlet it seems like just one class is involved in request
processing and im probably unable to "call" another class in case of an error,
am i right? or can i just instantiate a new class (new servlet) to handle the
request in 'write_content' method, initialize it somehow (how?) and proccess it
'write_content' method?

or, better, i should ask how you deal with input form checks when the form is
not simple a and it can raise several errors and you need to take some cleanup
and inform user as well?  are there any tools (contributed) for this already in

regards, martin.

P.S. The mod_python.servlet is realy nice and if i wont use it in this project,
i will definitely be using it somewhere else.

P.P.S. I also encountered an error during the install (setup.py install) -- the
import directive was complaining about nonexistent module '_apache', whih is
really nonexistent, well manual copying the file was fine :)


On Mon, Sep 27, 2004 at 03:20:57PM +0200, Daniel Popowich wrote:
> Martin Slouf writes:
> > it would be _very_ nice if one could just create a class, use its () operator
> > (the __call__() method) and put some standard behavior in that call.  then
> > create an object of this class and publisher handler with request
> > http://somewhere.org/index.py/my_handler
> > would instead of a function 'my_handler' defined in 'index.py' would call my
> > callable object 'my_handler' created in 'index.py'
> > 
> > the MyHandler class could look like this:
> > 
> > class MyHandler:
> >       def __call__(req, ...):
> > 	  self._handle_errors()
> > 	  self._do_some_special_stuff() # ready to override
> > 	  self._log_request()
> > 	  self._generate_html()
> > 
> > if i want a special behaviour, no problem -- just subclass 'MyHandler' and
> > change the default behaviour.
> > 
> > anyone has tried that?  are there any frameworks using it?  is it slow?
> You may want to take a look at mod_python servlets (link below,
> complete with on-line tutorial) where instances of classes handle each
> request.  If all your servlets subclass (or mix-in) from a class that
> has such behaviour, then you're golden.
> Daniel Popowich
> -----------------------------------------------
> http://home.comcast.net/~d.popowich/mpservlets/
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list