Reputation: 3693
The PyGObject API allows to convert a GLib Variant to a Python object using the unpack()
method (see GLib.Variant.unpack).
However, is there a direct way to do the reverse?
E.g. I can create arbitrary GLib Variant objects using the Variant constructors:
>>> myglibvariant = GLib.Variant("a{sv}", { 'test': GLib.Variant('s', 'value')})
And use the unpack()
function to create a Python object in one step:
>>> myglibvariant.unpack()
{'test': 'value'}
However, is there a way to do the reverse in one step? E.g.
>>> myglibvariant = GLib.Variant.pack({'test': 'value'})
Upvotes: 2
Views: 787
Reputation: 173
You can find simple pack implementation in GNOME browser connector project: https://gitlab.gnome.org/nE0sIghT/gnome-browser-connector/-/blob/fb3e8feb7c9c9f8d6442b789531bf7926e930f3f/gnome_browser_connector/helpers.py#L8
Upvotes: 1
Reputation: 57910
No, because you have to specify the type. (In the {'test': 'value'}
example, the pack function would have to guess whether the type is a{ss}
or a{sv}
, or possibly {ss}
or {sv}
as well.
Upvotes: 1