Great exercise for python expert !

manatlan manatlan at gmail.com
Fri Nov 28 09:19:25 EST 2008


On 28 nov, 14:58, George Sakkis <george.sak... at gmail.com> wrote:
> On Nov 28, 5:36 am, manatlan <manat... at gmail.com> wrote:
>
>
>
> > I'd like to make a "jquery python wrapper" ...
>
> > here is my code :
> > ===================================================================
> > #!/usr/bin/env python
> > # -*- coding: utf-8 -*-
>
> > class JQueryCaller(object):
> >     def __init__(self,callback):
> >         self.__callback=callback
> >         self._s=[]
>
> >     def __getattr__(self,name):
> >         def _caller(*args):
> >             sargs=["'%s'"%i for i in args]
> >             self._s.append("%s(%s)"%(name,",".join(sargs)))
> >             return self
> >         return _caller
>
> >     def __call__(self):
> >         return self.__callback(".".join(self._s))
>
> > class MyObject(object):
> >     def __init__(self):
> >         self.js = JQueryCaller(self.__add)
>
> >     def __add(self,j):
> >         print "Add:"+j
>
> > if __name__ == "__main__":
> >     o=MyObject()
>
> >     o.js.kiki(12).kuku()()
> > ===================================================================
> > If i run the script : it will display :
>
> > Add:kiki('12').kuku()
>
> > Because the JQueryCaller caller is called, by the "()" trick at the
> > end of the last line
>
> > I'd like to display the same thing, but without the need to put the
> > "()" at then end !
> > (by calling simply : "o.js.kiki(12).kuku()" not "o.js.kiki(12).kuku()
> > ()")
> > (or how to call the MyObject._add (callback) without using the caller
> > on my JQueryCaller)
>
> Why don't you rename __call__ to __str__ and have MyObject.__add
> return a string instead of printing it directly?
>
> class MyObject(object):
>     def __add(self,j):
>         return "Add:"+j
>
> if __name__ == "__main__":
>     o = MyObject()
>     s = o.js.kiki(12).kuku()
>     print s
>
> HTH,
> George

sure, it works like you said ... but it's not what I want.
by doing that, you create an action ... when you will call "print" it
will call the __str__ (__repr__ is better in that case), which will
call the callback of myobject.
In my preceding post, the action was called by the "()" trick at the
end of line


In fact, MyObject will handle a list of all "js call"
If i do :
  o.js.toto()
  o.js.toto().titi(12,13)

I'd like my MyObject contains a list like that ["toto()","toto().titi
(12,23)"]

another idea ?



More information about the Python-list mailing list