dbus Variant:如何在Python中保留布尔数据类型? [英] dbus Variant: How to preserve boolean datatype in Python?

查看:276
本文介绍了dbus Variant:如何在Python中保留布尔数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在试验dbus。但我似乎无法让我的dbus服务为布尔值猜测正确的数据类型。考虑下面的例子:

 进口gtk 
进口dbus
进口dbus.service
从dbus.mainloop.glib import DBusGMainLoop

class Service(dbus.service.Object):
$ b $ def __init __(self):
bus_name = dbus.service.BusName (org.foo.bar,bus = dbus.SessionBus())
dbus.service.Object .__ init __(self,bus_name,/ org / foo / bar)


@ dbus.service.method(org.foo.bar,in_signature =a {sa {sv}},
out_signature =a {sa {sv}})
def执行(self,data):
返回数据


if __name__ ==__main__:
DBusGMainLoop(set_as_default = True)
s = Service()
gtk.main()

这段代码创建了一个dbus服务提供了接受一个参数的执行方法,该参数是一个从字符串映射到其他字典的字典,字典又将字符串映射到变体。由于我的字典的格式不同,我选择了这种格式:

  {
key1:{
type:(tuple,value)
},
key2:{
name:John Doe,
gender :男,
年龄:23
},
测试:{
真实属性:真,
假属性 :False
}
}

当我通过我的服务传递这个字典时,布尔值被转换为整数。在我看来,支票应该不那么难。考虑这个(是要转换为dbus类型的变量):

 如果isinstance(value,bool):
return dbus.Boolean(value)

如果在检查 isinstance(value,int)之前完成检查,那么就没有问题了。
有什么想法?

解决方案

我不确定你遇到困难的部分。类型可以很容易地从一种形式转换为另一种形式,如您在示例 dbus.Boolean(val)中所示。您也可以使用 isinstance(value,dbus.Boolean)来测试这个值是否是一个dbus布尔值,而不是整数。



Python本机类​​型转换为 dbus 类型,以便实现DBus客户机和用任何语言编写的服务之间的通信。因此,从DBus服务发送/接收的任何数据将由 dbus。* 数据类型组成。

  def执行(self,data):
作为['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)
返回数据


$输出:

  true-属性类型:< type'dbus.Boolean'> 
是dbus.Boolean:True
Python:True
Dbus:1
false属性类型:< type'dbus.Boolean'>
是dbus.Boolean:True
Python:False
Dbus:0


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
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop

class Service(dbus.service.Object):

  def __init__(self):
    bus_name = dbus.service.BusName("org.foo.bar", bus = dbus.SessionBus())
    dbus.service.Object.__init__(self, bus_name, "/org/foo/bar")


  @dbus.service.method("org.foo.bar", in_signature = "a{sa{sv}}",
    out_signature = "a{sa{sv}}")
  def perform(self, data):   
    return data


if __name__ == "__main__":
  DBusGMainLoop(set_as_default = True)
  s = Service()
  gtk.main()

This piece of code creates a dbus service that provides the perform method which accepts one parameter that is a dictionary which maps from strings to other dictionaries, which in turn map strings to variants. I have chosen this format because of the format my dictionaries are in:

{
  "key1": {
    "type": ("tuple", "value")
  },
  "key2": {
    "name": "John Doe",
    "gender": "male",
    "age": 23
  },
  "test": {
    "true-property": True,
    "false-property": False
  }
}

When I pass this dictionary through my service, the boolean values are converted to integers. In my eyes, the check should not be that difficult. Consider this (value is the variable to be converted to a dbus type):

if isinstance(value, bool):
  return dbus.Boolean(value)

If this check is done before checking for isinstance(value, int) then there would be no problem. Any ideas?

解决方案

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)
    return data

Output:

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

这篇关于dbus Variant:如何在Python中保留布尔数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆