Skip to content Skip to sidebar Skip to footer

Dbus Variant: How To Preserve Boolean Datatype In Python?

I've been experimenting with dbus lately. But I can't seem to get my dbus Service to guess the correct datatypes for boolean values. Consider the following example: import gtk impo

Solution 1:

I'm not sure which part you're having difficulty with. The types can easily be cast from one form to another, as you show in your example dbus.Boolean(val). You can also use isinstance(value, dbus.Boolean) to test if the value is a dbus boolean, not an integer.

The Python native types are converted into dbus types in order to enable communicate between DBus clients and services written in any language. So any data sent to / received from a DBus service will consist of dbus.* data types.

def perform(self, data):
    for key in ['true-property', 'false-property']:
        val = data['test'][key]
        newval = bool(val)

        print '%s type: %s' % (key, type(val))
        print 'is dbus.Boolean: %s' % isinstance(val, dbus.Boolean)
        print 'Python:', newval
        print '  Dbus:', dbus.Boolean(newval)
    returndata

Output:

true-property type: <type 'dbus.Boolean'>is dbus.Boolean: TruePython:True
  Dbus: 1false-property type: <type 'dbus.Boolean'>is dbus.Boolean: TruePython:False
  Dbus: 0

Post a Comment for "Dbus Variant: How To Preserve Boolean Datatype In Python?"