[mod_python] please explain what's this

Graham Dumpleton grahamd at dscpl.com.au
Mon Jul 24 05:44:27 EDT 2006


First off, am presuming you are using mod_python.publisher as the
response handler. You don't state this.

On 24/07/2006, at 7:15 PM, Sanjeev Divekar wrote:

> hi,
>
> I am new to mod_python using Apache 2.0.54, Python 2.4.2 and
> mod_python 3.2.8 on Windows 2003 Server
>
> Can anybody explain these line of code
>
> index = myclass()
> show = myclass().show_data

The second line is strictly speaking not necessary in the sense that it
is accessible under a different URL anyway without the alias. :-)

Anyway, when you use mod_python.publisher, URLs will map to any
callable objects or data values. If the above is in a file called  
'index.py'
then the URL 'index.py/index' will map to the 'index' object. Because
'myclass' provides the '__call__()" member function, the 'index'  
object is
callable and so that member function is called for that URL.

Similarly, the URL 'index.py/show' maps to 'show' which because it
references a member function of an object instance, it is also callable
and so it also can be accessed.

Note that object instances such as 'index' are traversable, so one can
also use 'index.py/index/show_data' and it will yield the same result as
saying just 'index.py/show'.

Because mod_python.publisher treats 'index.py' as special, it is  
actually
possible to simply use 'index', 'show' and 'index/show_data' to achieve
the same end. Relying on this magic can be a bit troublesome though as
it makes relative URLs difficult to determine in certain cases.

> also i have a question
> what is filters?

Filters are a complex topic, in simple terms they provide the ability  
to process
the response being sent back to the client after the response handler  
has
generated it. When you have mastered the basics of mod_python, then you
may want to have a read of:

   http://www.projectcomputing.com/resources/apacheFilterFAQ/

This isn't mod_python specific and is more about the underlying  
filter mechanism
of Apache, but still quite useful. When you have worked out whether  
you need
them or not, then you might dig further into how the mod_python  
wrappers for
Apache filters work. Probably wasting your time to look at them now  
if you are
only starting out with mod_python as they are really an advanced topic.

> ********************************************************************** 
> *********
> index.py
> ********************************************************************** 
> *********
> from mod_python import apache
> from mod_python import psp
> from mod_python import util
>
> class myclass:
>    def __init__(self):
>        return None
>
>    def __call__(self, req, para=''):
>        tmpl = psp.PSP(req, filename='a.html')
>        tmpl.run(vars={'para':para})
>        return
>
>    def show_data(self, req, para):
>        msg = "Your first name is " + para[0]
>        self(req, para=msg)
>        return
>
> index = myclass()
> show = myclass().show_data
> ********************************************************************** 
> *********
> ********************************************************************** 
> *********
> a.html
> ********************************************************************** 
> *********
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML>
> <HEAD>
> <TITLE> New Document </TITLE>
> </HEAD>
> <BODY>
> <h1><%=para%></h1>
> <form method="post" action="show">
> 	<input type="text" name="para">
> 	<input type="text" name="para">
> 	<input type="submit">
> </form>
> </BODY>
> </HTML>
> ********************************************************************** 
> *********
>
>
> -- 
> Sanjeev Divekar
> Sanmisha Technologies
> Mobile : +91 9820427416
> Email  : sanjeev at sanmisha.com
> _______________________________________________
> 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