【问题标题】:dbus-python how to return array of dictionariesdbus-python如何返回字典数组
【发布时间】:2017-08-02 12:22:53
【问题描述】:

我在 Ubuntu 16.04 上使用 Python Dbus。我想通过 DBus 向我的客户返回一个字典列表,但似乎只能返回一个字符串数组。如果我将我的 dbus 签名装饰器更改为“as{v}”,则会出现异常:“ValueError: Corrupt type signature”。如何通过 DBus 返回字典列表?

   @dbus.service.method("com.example.service.BtScanList", in_signature='', out_signature='as')
   def getScanList(self):
      btMsg("Starting BT Scan List...")
      # Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
      self.discoveredDevs = self.getScannedDevices()
      returnList = []
      for dev in self.discoveredDevs:
          returnList.append(dev["name"])
      return returnList

编辑:这也不起作用:

   @dbus.service.method("com.example.service.BtScanList", in_signature='', out_signature='a{sv}')
   def getScanList(self):
      btMsg("Starting BT Scan List...")
      # Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
      self.discoveredDevs = self.getScannedDevices()
      returnList = dbus.Array()
      for dev in self.discoveredDevs:
          btMsg(dev)
          returnList.append(dbus.Dictionary(dev, signature='sv'))
      return returnList

【问题讨论】:

    标签: python linux python-2.7 dbus dbus-python


    【解决方案1】:

    我想通了,答案就在这里:

       @dbus.service.method("com.example.service.BtPairedList", in_signature='', out_signature='aa{ss}')
       def getPairedList(self):
          btMsg("Starting BT Paired List...")
          # Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
          self.pairedDevs = self.getPairedDevices()
          returnList = dbus.Array()
          for dev in self.pairedDevs:
              btMsg(dev)
              returnList.append(dbus.Dictionary(dev, signature='sv'))
          return returnList
    

    【讨论】:

    • 您能解释一下您在 out_signature 中使用的 'aa{ss}' 的含义吗?
    猜你喜欢
    • 1970-01-01
    • 2017-05-07
    • 2018-05-15
    • 2022-10-05
    • 1970-01-01
    • 2012-11-29
    • 2022-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多