6

I'm playing with dbus-python and I need to get the reponses (f.e dbus.Dictionary - but generally any response) as native Python type. Yes, one can write recursive converter but I think I must have been missing something obvious? There must be a way how to convert these monstrosities back to Python native types using dbus. Or it is not?

dbus.Dictionary({dbus.String(u'CanGoNext'): dbus.Boolean(True, variant_level=1), dbus.String(u'CanPause'): dbus.String(u'MinimumRate'): dbus.Int32(14, variant_level=1) ...
4

3 回答 3

3

虽然 Martin 是对的,但它不适用于由 python 转换为 int 的布尔值(因为 bool不能被子类化)。

In [3]: print(dbus.Boolean(False))
0

不幸的是,递归转换器是保留布尔值的唯一方法。

这是一个示例实现:

import dbus


def python_to_dbus(data):
    '''
        convert python data types to dbus data types
    '''
    if isinstance(data, str):
        data = dbus.String(data)
    elif isinstance(data, bool):
        # python bools are also ints, order is important !
        data = dbus.Boolean(data)
    elif isinstance(data, int):
        data = dbus.Int64(data)
    elif isinstance(data, float):
        data = dbus.Double(data)
    elif isinstance(data, list):
        data = dbus.Array([python_to_dbus(value) for value in data], signature='v')
    elif isinstance(data, dict):
        data = dbus.Dictionary(data, signature='sv')
        for key in data.keys():
            data[key] = python_to_dbus(data[key])
    return data


def dbus_to_python(data):
    '''
        convert dbus data types to python native data types
    '''
    if isinstance(data, dbus.String):
        data = str(data)
    elif isinstance(data, dbus.Boolean):
        data = bool(data)
    elif isinstance(data, dbus.Int64):
        data = int(data)
    elif isinstance(data, dbus.Double):
        data = float(data)
    elif isinstance(data, dbus.Array):
        data = [dbus_to_python(value) for value in data]
    elif isinstance(data, dbus.Dictionary):
        new_data = dict()
        for key in data.keys():
            new_data[key] = dbus_to_python(data[key])
        data = new_data
    return data

为了使它更容易在处理 dbus 源数据的类中使用,您可以使用装饰器:

def convert_to_python(func):
    def wrapper(*args, **kwargs):
        return dbus_to_python(func(*args, **kwargs))
    return wrapper
...

@convert_to_python
def dbus_method_call(self):
    return self.dbus_proxy.Method()

这样,上面 dbus_method_call 返回的任何数据都将转换为原生 python。

请参阅此相关问题:dbus Variant: How to preserve boolean datatype in Python?

于 2018-01-31T16:01:53.477 回答
1

You don't need to. They are subclasses of the native types, so you just use them as if they were native types:

>>> import dbus
>>> v = dbus.Dictionary({dbus.String(u'CanGoNext'): dbus.Boolean(True, variant_level=1), dbus.String(u'CanPause'): dbus.Boolean(False, variant_level=1), dbus.String(u'MinimumRate'): dbus.Int32(14, variant_level=1)})
>>> print v['MinimumRate']
14
>>> if v['CanGoNext']: print 'Go'
... 
Go
>>> if v['CanPause']: print 'Pause'
... 
>>> 
于 2012-09-27T15:27:16.600 回答
1

正如@Martin Vidner 所说,在您的代码中,您无需执行任何操作即可使用它们。

要漂亮地打印,您可以使用现有的“递归转换器”,例如json

import json
print(json.dumps(d, indent=2))

输出

{
  "MinimumRate": 14, 
  "CanGoNext": 1, 
  "CanPause": 0
}

它丢失了有关类型的信息。

于 2012-09-27T15:45:53.877 回答