[virt-tools-list] [PATCH virt-manager 09/14] netlist: use NamedTuple for storing a network device

Marc Hartmayer mhartmay at linux.ibm.com
Tue Dec 18 13:44:58 UTC 2018


This has the advantage that we can access the attributes by name and
it's immutable by design.

Signed-off-by: Marc Hartmayer <mhartmay at linux.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy at linux.ibm.com>
---
 virtManager/netlist.py | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/virtManager/netlist.py b/virtManager/netlist.py
index cb043b7e83c4..8b1f12268fb0 100644
--- a/virtManager/netlist.py
+++ b/virtManager/netlist.py
@@ -3,6 +3,7 @@
 # This work is licensed under the GNU GPLv2 or later.
 # See the COPYING file in the top-level directory.
 
+import collections
 import logging
 
 from gi.repository import Gtk
@@ -12,6 +13,9 @@ from . import uiutil
 from .baseclass import vmmGObjectUI
 
 
+NetDev = collections.namedtuple('Netdev', ['name', 'is_bridge', 'slave_names'])
+
+
 class vmmNetworkList(vmmGObjectUI):
     __gsignals__ = {
         "changed": (vmmGObjectUI.RUN_FIRST, None, []),
@@ -165,17 +169,19 @@ class vmmNetworkList(vmmGObjectUI):
 
         netdevs = {}
         for iface in self.conn.list_interfaces():
-            netdevs[iface.get_name()] = [
-                iface.get_name(), iface.is_bridge(), iface.get_slave_names()]
+            name = iface.get_name()
+            netdevs[name] = NetDev(name, iface.is_bridge(),
+                                   iface.get_slave_names())
         for nodedev in self.conn.filter_nodedevs("net"):
             if nodedev.xmlobj.interface not in netdevs:
-                netdevs[nodedev.xmlobj.interface] = [nodedev.xmlobj.interface,
-                    False, []]
+                netdev = NetDev(nodedev.xmlobj.interface, False, [])
+                netdevs[nodedev.xmlobj.interface] = netdev
 
         # For every bridge used by a virtual network, and any slaves of
         # those devices, don't list them.
         for vnet_bridge in vnet_bridges:
-            slave_names = netdevs.pop(vnet_bridge, [None, None, []])[2]
+            slave_names = netdevs.pop(vnet_bridge,
+                                      NetDev(None, None, [])).slave_names
             for slave in slave_names:
                 netdevs.pop(slave, None)
 
-- 
2.17.0




More information about the virt-tools-list mailing list