dbus 变体:如何在 Python 中保留布尔数据类型?
我最近一直在尝试 dbus。但我似乎无法让我的 dbus 服务猜测布尔值的正确数据类型。考虑以下示例:
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()
这段代码创建了一个 dbus 服务,该服务提供了一个 Perform 方法,该方法接受一个参数,该参数是一个字典,该字典从字符串映射到其他字典,而其他字典又将字符串映射到变体。我选择这种格式是因为我的字典采用的格式:
{
"key1": {
"type": ("tuple", "value")
},
"key2": {
"name": "John Doe",
"gender": "male",
"age": 23
},
"test": {
"true-property": True,
"false-property": False
}
}
当我通过我的服务传递此字典时,布尔值将转换为整数。在我看来,检查应该不那么困难。考虑一下(value
是要转换为 dbus 类型的变量):
if isinstance(value, bool):
return dbus.Boolean(value)
如果在检查 isinstance(value, int)
之前完成此检查,那么就不会有问题。 有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您在哪一部分遇到困难。这些类型可以轻松地从一种形式转换为另一种形式,如示例
dbus.Boolean(val)
中所示。您还可以使用 isinstance(value, dbus.Boolean) 来测试该值是否是 dbus 布尔值,而不是整数。Python 本机类型被转换为 dbus 类型,以便能够在 DBus 客户端和用任何语言编写的服务之间进行通信。因此,发送到 DBus 服务或从 DBus 服务接收的任何数据都将包含 dbus.* 数据类型。
输出:
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 useisinstance(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 ofdbus.*
data types.Output: