[Twisted-Python] issues using pb.Copyable
Jean-Paul Calderone
exarkun at divmod.com
Fri Oct 27 09:23:07 EDT 2006
On Fri, 27 Oct 2006 00:33:17 -0400, Deepankar Sharma <deepankar.sharma at gmail.com> wrote:
>Hello,
>
>I am trying to use pb.Copyable to send some python objects back and forth
>between a client and server. I followed the documentation and quickly got
>some simple examples working.
>However i got stuck when i tried sending some python objects which are
>actually wrappers for stuff written in C. Here is an example of what i am
>trying to do
>
>
>Class C (Wrapper for a c data structure)
>
>Class B (inherits from class C and from pb.Copyable)
>
>I have a lot of wrapper functions which give me instances of Class C. Trying
>to do c_instance.__class__ = B does not work.
>I somehow need to cast those instances into instances of class B so that the
>jellying/unjellying can take place. What does one do in such a situation ?
Instead of subclassing C, write a wrapper for it. Implement getStateFor on
the wrapper. For example:
>>> from twisted.spread import pb
>>> class X(object):
... def __init__(self, y):
... self.y = y
...
>>> class Z(pb.Jellyable):
... def __init__(self, x):
... self.x = x
... def getStateFor(self, jellier):
... return {'y': self.x.y}
...
>>> pb.jelly(Z(X(10)))
['__main__.Z', ['dictionary', ['y', 10]]]
>>> from twisted.spread import jelly
>>> def unjellyZ(jellier, state):
... return Z(X(y=pb.unjelly(state[1])['y']))
...
>>> jelly.setUnjellyableForClass('__main__.Z', unjellyZ)
>>> pb.unjelly(['__main__.Z', ['dictionary', ['y', 10]]]).x.y
10
>>>
Jean-Paul
More information about the Twisted-Python
mailing list