This is a use-case where it is beneficial to read connection settings using object attributes.
In a loop over connections, I've these checks, which would be much harder to read when acessing connection settings by dict instead. In the case of settings.connection.autoconnect, which is an optional setting, defaulting to True, the code also has to be able to handle settings.connection.autoconnect not being set, which would make the dict access even harder to read:
for connection_path in connection_paths:
connection_service = NetworkConnectionSettings(connection_path)
settings = NmMetaDict(await connection_service.get_settings())
connection = settings.connection
# Only connections with matching type and where autoconnect isn't False
if connection.type != type or connection.autoconnect is False:
continue
# Only connections without interface-name or where it matches:
if connection.interface_name and connection.interface_name != ifname:
continue
# Update the dict of timestamps from the last use of the connection
conns[connection.timestamp] = connection.id
# Update the dict of autoconnect-priorities and last-use timestamps
if connection.autoconnect_priority not in prios:
prios[connection.autoconnect_priority] = {}
prios[connection.autoconnect_priority][connection.timestamp] = settings
This PR is mostly to provide a use-case for demonstrating this. The implementation of providing attributes in this example is kept to a minimum.
MetaDict is a good base layer for because it supports nested key access and the MetaDict object itself does not clutter the output when printing the nested MetaDict.
Features:
key auto-completion in IDEs
is really a dict, even nested
can be dumped with json.dumps()
Prodict is similar and supports defining default values and data types, is also a possible base layer.
It can also be extended to have variant tuples instead of plan values instead: https://github.com/ramazanpolat/prodict/pull/37
This is a use-case where it is beneficial to read connection settings using object attributes.
In a loop over connections, I've these checks, which would be much harder to read when acessing connection settings by dict instead. In the case of
settings.connection.autoconnect
, which is an optional setting, defaulting toTrue
, the code also has to be able to handlesettings.connection.autoconnect
not being set, which would make the dict access even harder to read:This PR is mostly to provide a use-case for demonstrating this. The implementation of providing attributes in this example is kept to a minimum.
MetaDict
is a good base layer for because it supports nested key access and the MetaDict object itself does not clutter the output when printing the nested MetaDict.Features:
json.dumps()
Prodict is similar and supports defining default values and data types, is also a possible base layer. It can also be extended to have variant tuples instead of plan values instead: https://github.com/ramazanpolat/prodict/pull/37