我有这样的字典
{key: {
key1: '',
key2: [{
key3: ''
}]
}}
我已经尝试过 a{sv},但是出现错误:
**to message with signature a{sv}: <type 'exceptions.TypeError'>: Expected a string or unicode object**
dbus.Dictionary(self._manager.get_feeds(),签名='sv')
.ERROR:dbus.service:Unable to append (dbus.Dictionary({u'1d63adf6-77ad-11e1-99a0-001c259909fc': {u'refresh_rate': 15, u'title': u'Omg! Ubuntu!', u'url': u'http://feeds.feedburner.com/d0od?format=xml', u'items': {u'0e34b3e2-7e9d-11e1-913c-001c259909fc': {u'date': u'2012-04-04 22:21:51', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/9tHuL6dx_O8/', u'title': u'Linux Tycoon \u2013 A Simulation Game for Distro Dreamers'}, u'93c26f5a-7e92-11e1-913c-001c259909fc': {u'date': u'2012-03-28 19:22:29', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/6Hwdvjuy3MA/', u'title': u'75% of Ubuntu Users Also Use Windows (And Other Fun Stats)'}, u'93c1c37a-7e92-11e1-913c-001c259909fc': {u'date': u'2012-04-03 23:32:44', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/N-lMhNJvKpY/', u'title': u'Elementary GTK, Icon Theme to Be Pantheon-Only'}, u'93c278b0-7e92-11e1-913c-001c259909fc': {u'date': u'2012-03-28 13:44:00', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/Aj6qBOzcBFQ/', u'title': u'[How To] Easily Skin Ubuntu\u2019s Unity Desktop'}, u'93c20d8a-7e92-11e1-913c-001c259909fc': {u'date': u'2012-04-02 13:09:04', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/PpqV7E7eMKI/', u'title': u'Ubuntu 12.04 Default Wallpaper Revealed'}, u'93c111f0-7e92-11e1-913c-001c259909fc': {u'date': u'2012-04-04 00:36:19', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/TlbmfB7Vb0Q/', u'title': u'[Video] How Linux Is Built'}, u'93c17190-7e92-11e1-913c-001c259909fc': {u'date': u'2012-04-04 00:00:48', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/8hVWQHMAgAc/', u'title': u'Smarten Up Your Desktop With This Conky & Wallpaper Combo'}, u'93c257e0-7e92-11e1-913c-001c259909fc': {u'date': u'2012-04-01 17:39:30', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/0kE8ObCm4ig/', u'title': u'Rdio & Pandora Support Added to Nuvola Player'}, u'1d6277c4-77ad-11e1-99a0-001c259909fc': {u'date': u'2012-03-26 16:48:23', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/1jizNwZr2vw/', u'title': u'Ubuntu\u2019s uTouch Tech Brings Smooth Scrolling to Chromium'}, u'1d62cf8a-77ad-11e1-99a0-001c259909fc': {u'date': u'2012-03-25 15:13:53', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/DcmDEwg1j48/', u'title': u'KDE Tablet \u2018Vivaldi\u2019 Gets Gutted'}, u'93c27be4-7e92-11e1-913c-001c259909fc': {u'date': u'2012-03-28 11:22:04', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/MfoQ1lXzyOI/', u'title': u'Linux Photo Manager Shotwell Updates, Adds Straighten Tool'}, u'93c268fc-7e92-11e1-913c-001c259909fc': {u'date': u'2012-03-29 19:02:55', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/lP86lxGnAzU/', u'title': u'Ubuntu 12.04 Development Update'}, u'93c27270-7e92-11e1-913c-001c259909fc': {u'date': u'2012-03-28 16:34:17', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/Xq6R0FYEqfE/', u'title': u'New GNOME 3.4 Release Offers Features, Fun & UI Finesse'}, u'93c265b4-7e92-11e1-913c-001c259909fc': {u'date': u'2012-04-01 11:21:57', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/fq0qQdrwWSI/', u'title': u'Important Notice For All OMG! Ubuntu! Readers'}, u'93c27ee6-7e92-11e1-913c-001c259909fc': {u'date': u'2012-03-27 11:46:14', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/UCviWQD6IH8/', u'title': u'Elementary Launch Luna Wallpaper Experiment'}, u'93c26c3a-7e92-11e1-913c-001c259909fc': {u'date': u'2012-03-29 19:00:33', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/VjYRjJeahbM/', u'title': u'Ubuntu 12.04 Beta 2 Released'}, u'1d621f2c-77ad-11e1-99a0-001c259909fc': {u'date': u'2012-03-26 18:02:45', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/fyK1QXRRISE/', u'title': u'\u2018Snuggle Truck\u2019 Added to Latest Humble Bundle'}, u'90f51042-7f1a-11e1-913c-001c259909fc': {u'date': u'2012-04-05 11:40:25', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/fFaFM29rCz8/', u'title': u'15 New Wallpapers Land in Ubuntu 12.04'}, u'93c26208-7e92-11e1-913c-001c259909fc': {u'date': u'2012-04-01 14:48:14', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/Npoh61ja3ww/', u'title': u'The Official Ubuntu 12.04 CD Cover'}, u'93c2759a-7e92-11e1-913c-001c259909fc': {u'date': u'2012-03-28 15:21:00', u'read': False, u'link': u'http://feedproxy.google.com/~r/d0od/~3/i4BhVzSzCpk/', u'title': u'Grab The Lubuntu 12.04 Countdown Widget'}}, u'limit': 20, u'link': u'http://www.omgubuntu.co.uk'}, u'26865c26-77ad-11e1-90cb-001c259909fc': {u'refresh_rate': 15, u'title': u"Pavol Kla\u010dansk\xfd's blog - Opinions and development", u'url': u'feed.xml', u'items': {u'26864d94-77ad-11e1-90cb-001c259909fc': {u'date': u'2010-01-24 13:48:38', u'read': False, u'link': u'http://blog.klacansky.com/mind-scanner/logo-design-reevolution', u'title': u'Logo & design (re)evolution'}, u'26865028-77ad-11e1-90cb-001c259909fc': {u'date': u'2010-01-13 20:46:59', u'read': False, u'link': u'http://blog.klacansky.com/matter-code/wordpress-plugin-added-facebook-share-support', u'title': u'WordPress plugin: Added Facebook Share support'}, u'268652bc-77ad-11e1-90cb-001c259909fc': {u'date': u'2009-12-30 20:46:34', u'read': False, u'link': u'http://blog.klacansky.com/matter-code/wordpress-plugin-unique-image-for-article', u'title': u'WordPress plugin: Unique Image for article'}, u'2685c004-77ad-11e1-90cb-001c259909fc': {u'date': u'2010-12-05 10:56:13', u'read': False, u'link': u'http://blog.klacansky.com/matter-code/intro-to-my-new-project-called-speedyrss', u'title': u'Intro to my new project called SpeedyRSS'}, u'26860622-77ad-11e1-90cb-001c259909fc': {u'date': u'2010-11-04 14:22:25', u'read': False, u'link': u'http://blog.klacansky.com/my-hardware/how-fast-does-hamburger-get-spoiled-in-rain-forest', u'title': u'How fast does hamburger get spoiled in rain forest?'}, u'26858206-77ad-11e1-90cb-001c259909fc': {u'date': u'2010-12-28 19:48:47', u'read': False, u'link': u'http://blog.klacansky.com/my-hardware/do-you-really-love-hamburger-i-dont', u'title': u'Do you really love hamburger? I don\u2019t'}, u'26864aba-77ad-11e1-90cb-001c259909fc': {u'date': u'2010-10-10 17:24:04', u'read': False, u'link': u'http://blog.klacansky.com/mind-scanner/farewell-to-facebook-chat-has-survived', u'title': u'Farewell to Facebook, chat has survived'}, u'26853652-77ad-11e1-90cb-001c259909fc': {u'date': u'2011-08-09 13:26:53', u'read': False, u'link': u'http://blog.klacansky.com/matter-code/current-status-of-speedyrss', u'title': u'Current status of SpeedyRSS'}, u'268642a4-77ad-11e1-90cb-001c259909fc': {u'date': u'2010-10-17 14:44:15', u'read': False, u'link': u'http://blog.klacansky.com/matter-code/ant-taskdef-class-com-android-ant-setuptask-cannot-be-found', u'title': u'ANT: taskdef class com.android.ant.SetupTask cannot be found'}}, u'limit': 20, u'link': u'http://blog.klacansky.com'}}, signature=dbus.Signature('sv')),) to message with signature a{sv}: <type 'exceptions.TypeError'>: an integer is required
答案1
如果您尝试传递像这样的复杂结构,则 dbus-python 绑定中的类型推断可能会出现错误。
如果您想确保将字典视为a{sv}
,您可以在dbus.Dictionary()
将其传递给方法调用或信号广播之前用将其包装起来:
d = dbus.Dictionary({'key1': value1, 'key2': value2}, signature='sv')
这会绕过类型推断并使用您指定的类型代码。
答案2
您可以像这样使用嵌套字典:
self._properties = dbus.Dictionary({
'Metadata': dbus.Dictionary({
'mpris:length': dbus.Int64(2400000000),
'mpris:artUrl': 'file:///home/user/1.jpg',
'xesam:artist': ['1', '2'],
'xesam:title': 'hello world',
'xesam:url': '',
'xesam:album': 'hehe'
}, signature='sv'),
'CanControl': True
}, signature='sv')
它应该可以工作。