Graham Dumpleton
graham.dumpleton at gmail.com
Thu Jun 21 21:52:22 EDT 2007
On 22/06/07, maker joe <makerjoe at gmail.com> wrote: > 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 I don't see how that will work at all. At least not if you are using publisher. If you have instead written your own handler which performs its own URL dispatch then maybe it may work, but I don't know how your dispatcher works. If I try and read into your code as to what you perhaps meant, there is a big difference in that with the approach I was suggested, there would be a separate instance of the class per request. In your code, by creating the instance at global scope within the module there will be one instance for the life of the process and all requests will be handled within that once instance. The consequences of using a single instance are that class attributes can't be used for transient data for a single request where one wants to use that as a means of communicating data between member functions. What you really need to do is explain what behaviour you want. One class instance to be used for all requests, or one per request. For all I know, all you were perhaps after was to know how to make a URL traverse into objects, but your URL examples haven't been suggestive of that. So, maybe you should explain in english what you expect to happen. Graham > 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 > > >
|