dbus 变体:如何在 Python 中保留布尔数据类型?

发布于 2024-11-03 13:16:41 字数 1210 浏览 0 评论 0原文

我最近一直在尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦笑流年记忆 2024-11-10 13:16:41

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

Python 本机类型被转换为 dbus 类型,以便能够在 DBus 客户端和用任何语言编写的服务之间进行通信。因此,发送到 DBus 服务或从 DBus 服务接收的任何数据都将包含 dbus.* 数据类型。

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

输出:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文