[Libguestfs] [PATCH 1/3] python: modernize inspect_vm example

Pino Toscano ptoscano at redhat.com
Wed Apr 24 13:32:27 UTC 2019


Since we already assume Python >= 2.7, modernize this example to make it
work also on Python 3:
- use print() as function
- sort the mount points using a key for sorted(), instead of a
  comparison function
- remove extra newline escape
- reident two lines according to the PEP 8 style
---
 python/examples/inspect_vm.py | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/python/examples/inspect_vm.py b/python/examples/inspect_vm.py
index 077157c19..22cd70f28 100644
--- a/python/examples/inspect_vm.py
+++ b/python/examples/inspect_vm.py
@@ -24,36 +24,34 @@ if len(roots) == 0:
     raise(Error("inspect_vm: no operating systems found"))
 
 for root in roots:
-    print "Root device: %s" % root
+    print("Root device: %s" % root)
 
     # Print basic information about the operating system.
-    print "  Product name: %s" % (g.inspect_get_product_name(root))
-    print "  Version:      %d.%d" % \
-        (g.inspect_get_major_version(root),
-         g.inspect_get_minor_version(root))
-    print "  Type:         %s" % (g.inspect_get_type(root))
-    print "  Distro:       %s" % (g.inspect_get_distro(root))
+    print("  Product name: %s" % (g.inspect_get_product_name(root)))
+    print("  Version:      %d.%d" %
+          (g.inspect_get_major_version(root),
+           g.inspect_get_minor_version(root)))
+    print("  Type:         %s" % (g.inspect_get_type(root)))
+    print("  Distro:       %s" % (g.inspect_get_distro(root)))
 
     # Mount up the disks, like guestfish -i.
     #
     # Sort keys by length, shortest first, so that we end up
     # mounting the filesystems in the correct order.
     mps = g.inspect_get_mountpoints(root)
-    def compare(a, b):
-        return len(a) - len(b)
-    for device in sorted(mps.keys(), compare):
+    for device, mp in sorted(mps.items(), key=lambda k: len(k[0])):
         try:
-            g.mount_ro(mps[device], device)
+            g.mount_ro(mp, device)
         except RuntimeError as msg:
-            print "%s (ignored)" % msg
+            print("%s (ignored)" % msg)
 
     # If /etc/issue.net file exists, print up to 3 lines.
     filename = "/etc/issue.net"
     if g.is_file(filename):
-        print "--- %s ---" % filename
+        print("--- %s ---" % filename)
         lines = g.head_n(3, filename)
         for line in lines:
-            print line
+            print(line)
 
     # Unmount everything.
     g.umount_all()
-- 
2.20.1




More information about the Libguestfs mailing list