Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Represent object type as

0 views
Skip to first unread message

Jason

unread,
Mar 25, 2010, 11:39:01 AM3/25/10
to
Hi,

I want to send objects (new style) over DBUS. DBUS can only send
fairly primitive types[1] so I turn my objects into dicts and send
that. I'm reusing the __getstate__ function I wrote for pickling like
so:

def __getstate__(self):
attrs = self.__dict__.copy()
return attrs

...which is perfectly adequate to dict-ify and reconstitute this
simple class. That works just fine over DBUS.

The trouble is, the object could actually be of some slightly
different types, so I'd like to include that information as well. I
tried something like:

def __getstate__(self):
attrs = self.__dict__.copy()
attrs.update({'type': type(self)})
return attrs

...but then realised that "type" is not primitive enough for DBUS to
pickle.

So, (a) can I get the type name from the type object, or (b) is there
a better way to do this?

(This pertains to Python 2.5.4.)

Cheers,
— Jason

[1] http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html#data-types

Bruno Desthuilliers

unread,
Mar 25, 2010, 12:00:34 PM3/25/10
to
Jason a écrit :

> Hi,
>
> I want to send objects (new style) over DBUS. DBUS can only send
> fairly primitive types[1] so I turn my objects into dicts and send
> that. I'm reusing the __getstate__ function I wrote for pickling like
> so:
>
> def __getstate__(self):
> attrs = self.__dict__.copy()
> return attrs
>
> ...which is perfectly adequate to dict-ify and reconstitute this
> simple class. That works just fine over DBUS.
>
> The trouble is, the object could actually be of some slightly
> different types, so I'd like to include that information as well. I
> tried something like:
>
> def __getstate__(self):
> attrs = self.__dict__.copy()
> attrs.update({'type': type(self)})

attrs['type'] = type(self)

Do the same thing with less work !-)

Also and while we're at it, using a __magicname__ convention here (ie :
'__type__' instead of 'type') might avoid possible collisions.

> return attrs
>
> ...but then realised that "type" is not primitive enough for DBUS to
> pickle.
>
> So, (a) can I get the type name from the type object,

attrs['__typename__'] = type(self).__name__


Warning: won't be very useful if your code still uses old-style classes.

> or (b) is there
> a better way to do this?

Depends on what you do with this dict, DBUS etc. And of your definition
of "better", of course.


HTH

Jason

unread,
Mar 25, 2010, 8:49:30 PM3/25/10
to
On Mar 26, 12:00 am, Bruno Desthuilliers <bruno.

42.desthuilli...@websiteburo.invalid> wrote:
>            attrs['type'] = type(self)
>
> Do the same thing with less work !-)

Ah, silly me :P

>      attrs['__typename__'] = type(self).__name__

That's exactly what I needed — I was not aware of the "__name__"
attribute.

> Warning: won't be very useful if your code still uses old-style classes.

No, all the objects are new-style classes so that's fine.

> Depends on what you do with this dict, DBUS etc. And of your definition
> of "better", of course.

Simplest possible :P But this looks like it, so thanks very much :)

Cheers,
Jason

Gregory Ewing

unread,
Mar 27, 2010, 1:23:19 AM3/27/10
to
Jason wrote:
> I'm reusing the __getstate__ function I wrote for pickling like
> so:

Instead of reinventing part of pickle, is there some reason
you couldn't just use it?

--
Greg

0 new messages