[Tutor] dbus.Array to string

Amit Saha amitsaha.in at gmail.com
Mon Aug 12 00:53:19 CEST 2013


Hello all,

The other day, I had to convert a dbus.Array [1] object to a string. I
found a way to do it, but I am not sure if that is the best way.

Here is a dbus.Array object:

dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103),
dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100),
dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68),
dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'),
variant_level=1)


And this is how I converted it to it's string representation:

>>> import dbus

>>> ssid = "dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'), variant_level=1)
"

>>> ssid = ''.join([chr(character) for character in ssid])

>>> ssid
'BigPond679D85'

Basically I use the idea that iterating over the array will give me
only the dbus.Byte 's and hence use chr() to convert it back to the
character representation.

Is this is the best way to do it?

[1] http://dbus.freedesktop.org/doc/dbus-python/api/dbus.Array-class.html

Thanks,
Amit.


-- 
http://echorand.me


More information about the Tutor mailing list