[mod_python] get post to a class

maker joe makerjoe at gmail.com
Thu Jun 21 21:35:05 EDT 2007


perhaps a little bit simpler would be

go.py
-------
import myclasses
c=dispatch()
c.message(req)

myclasses.py
--------------------
class dispatch:
    def message(self,req)
         req.write('hello %s'%req.form['name'])

http://localhost/my.py/go?name=joe

bye
joseluis


On 6/21/07, Graham Dumpleton <graham.dumpleton at gmail.com> wrote:
> On 22/06/07, maker joe <makerjoe at gmail.com> wrote:
> > hi graham
> >
> > is it possible to get/post to a class?
> >
> > my.py
> > ---------
> > class A:
> >     def go(self,req):
> >          req.write('hello'+%s)%name
> >
> > http://localhost/my.py/A/go?name=joe
>
> Classes will not auto instantiate.
>
> If your intention is to be able to create an instance of A per
> request, use something like:
>
>   class Instance:
>
>     def __init__(self, type, **kwargs):
>         self.__type = type
>         self.__kwargs = kwargs
>
>     def __call__(self, **args):
>         assert(type(self.__type) in [types.ClassType, types.TypeType])
>         return self.__type(**self.__kwargs)(**args)
>
>     class A:
>         def __call__(self, req):
>             req.write('hello'+%s)%name
>
>     go = Instance(A)
>
> URL would then be:
>
>    http://localhost/my.py/go?name=joe
>
> If the class has a constructor, use keyword arguments to Instance() to
> pass any arguments:
>
>     class A:
>         def __init__(self, arg1, arg2):
>             ...
>         def __call__(self, req):
>             req.write('hello'+%s)%name
>
>     go = Instance(A, arg1='value', arg2='value')
>
> This presumes you are using publisher.
>
> Graham
>


More information about the Mod_python mailing list