All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, vsementsov@virtuozzo.com,
	qemu-devel@nongnu.org, mreitz@redhat.com,
	andrey.shinkevich@virtuozzo.com, den@openvz.org
Subject: [PATCH v4 05/12] qcow2_format.py: use modern string formatting
Date: Thu,  4 Jun 2020 20:41:28 +0300	[thread overview]
Message-ID: <20200604174135.11042-6-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20200604174135.11042-1-vsementsov@virtuozzo.com>

Use .format and f-strings instead of old %style. Also, the file uses
both '' and "" quotes, for consistency let's use '', except for cases
when we need '' inside the string (use "" to avoid extra escaping).

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 tests/qemu-iotests/qcow2_format.py | 54 +++++++++++++++---------------
 1 file changed, 27 insertions(+), 27 deletions(-)

diff --git a/tests/qemu-iotests/qcow2_format.py b/tests/qemu-iotests/qcow2_format.py
index 5d242c4aa4..80e7c3f81d 100644
--- a/tests/qemu-iotests/qcow2_format.py
+++ b/tests/qemu-iotests/qcow2_format.py
@@ -7,7 +7,7 @@ class QcowHeaderExtension:
     def __init__(self, magic, length, data):
         if length % 8 != 0:
             padding = 8 - (length % 8)
-            data += b"\0" * padding
+            data += b'\0' * padding
 
         self.magic = magic
         self.length = length
@@ -25,26 +25,26 @@ class QcowHeader:
 
     fields = (
         # Version 2 header fields
-        (uint32_t, '%#x',  'magic'),
-        (uint32_t, '%d',   'version'),
-        (uint64_t, '%#x',  'backing_file_offset'),
-        (uint32_t, '%#x',  'backing_file_size'),
-        (uint32_t, '%d',   'cluster_bits'),
-        (uint64_t, '%d',   'size'),
-        (uint32_t, '%d',   'crypt_method'),
-        (uint32_t, '%d',   'l1_size'),
-        (uint64_t, '%#x',  'l1_table_offset'),
-        (uint64_t, '%#x',  'refcount_table_offset'),
-        (uint32_t, '%d',   'refcount_table_clusters'),
-        (uint32_t, '%d',   'nb_snapshots'),
-        (uint64_t, '%#x',  'snapshot_offset'),
+        (uint32_t, '{:#x}', 'magic'),
+        (uint32_t, '{}', 'version'),
+        (uint64_t, '{:#x}', 'backing_file_offset'),
+        (uint32_t, '{:#x}', 'backing_file_size'),
+        (uint32_t, '{}', 'cluster_bits'),
+        (uint64_t, '{}', 'size'),
+        (uint32_t, '{}', 'crypt_method'),
+        (uint32_t, '{}', 'l1_size'),
+        (uint64_t, '{:#x}', 'l1_table_offset'),
+        (uint64_t, '{:#x}', 'refcount_table_offset'),
+        (uint32_t, '{}', 'refcount_table_clusters'),
+        (uint32_t, '{}', 'nb_snapshots'),
+        (uint64_t, '{:#x}', 'snapshot_offset'),
 
         # Version 3 header fields
         (uint64_t, 'mask', 'incompatible_features'),
         (uint64_t, 'mask', 'compatible_features'),
         (uint64_t, 'mask', 'autoclear_features'),
-        (uint32_t, '%d',   'refcount_order'),
-        (uint32_t, '%d',   'header_length'),
+        (uint32_t, '{}', 'refcount_order'),
+        (uint32_t, '{}', 'header_length'),
     )
 
     fmt = '>' + ''.join(field[0] for field in fields)
@@ -102,7 +102,7 @@ class QcowHeader:
 
         fd.seek(self.header_length)
         extensions = self.extensions
-        extensions.append(QcowHeaderExtension(0, 0, b""))
+        extensions.append(QcowHeaderExtension(0, 0, b''))
         for ex in extensions:
             buf = struct.pack('>II', ex.magic, ex.length)
             fd.write(buf)
@@ -113,7 +113,7 @@ class QcowHeader:
             fd.write(self.backing_file)
 
         if fd.tell() > self.cluster_size:
-            raise Exception("I think I just broke the image...")
+            raise Exception('I think I just broke the image...')
 
     def update(self, fd):
         header_bytes = self.header_length
@@ -136,21 +136,21 @@ class QcowHeader:
                         bits.append(bit)
                 value_str = str(bits)
             else:
-                value_str = f[1] % value
+                value_str = f[1].format(value)
 
-            print("%-25s" % f[2], value_str)
+            print(f'{f[2]:<25} {value_str}')
 
     def dump_extensions(self):
         for ex in self.extensions:
 
             data = ex.data[:ex.length]
             if all(c in string.printable.encode('ascii') for c in data):
-                data = "'%s'" % data.decode('ascii')
+                data = f"'{ data.decode('ascii') }'"
             else:
-                data = "<binary>"
+                data = '<binary>'
 
-            print("Header extension:")
-            print("%-25s %#x" % ("magic", ex.magic))
-            print("%-25s %d" % ("length", ex.length))
-            print("%-25s %s" % ("data", data))
-            print("")
+            print('Header extension:')
+            print(f'{"magic":<25} {ex.magic:#x}')
+            print(f'{"length":<25} {ex.length}')
+            print(f'{"data":<25} {data}')
+            print()
-- 
2.21.0



  parent reply	other threads:[~2020-06-04 17:45 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-04 17:41 [PATCH v4 00/12] iotests: Dump QCOW2 dirty bitmaps metadata Vladimir Sementsov-Ogievskiy
2020-06-04 17:41 ` [PATCH v4 01/12] qcow2.py: python style fixes Vladimir Sementsov-Ogievskiy
2020-06-05 10:23   ` Andrey Shinkevich
2020-06-05 19:43   ` Eric Blake
2020-06-06  7:00     ` Vladimir Sementsov-Ogievskiy
2020-06-04 17:41 ` [PATCH v4 02/12] qcow2.py: move qcow2 format classes to separate module Vladimir Sementsov-Ogievskiy
2020-06-05 10:51   ` Andrey Shinkevich
2020-06-05 20:14   ` Eric Blake
2020-06-06  7:03     ` Vladimir Sementsov-Ogievskiy
2020-06-08 15:20       ` Eric Blake
2020-06-04 17:41 ` [PATCH v4 03/12] qcow2_format.py: drop new line printing at end of dump() Vladimir Sementsov-Ogievskiy
2020-06-05 10:54   ` Andrey Shinkevich
2020-06-04 17:41 ` [PATCH v4 04/12] qcow2_format.py: use tuples instead of lists for fields Vladimir Sementsov-Ogievskiy
2020-06-05 11:20   ` Andrey Shinkevich
2020-06-05 20:16   ` Eric Blake
2020-06-06  7:07     ` Vladimir Sementsov-Ogievskiy
2020-06-04 17:41 ` Vladimir Sementsov-Ogievskiy [this message]
2020-06-05 11:27   ` [PATCH v4 05/12] qcow2_format.py: use modern string formatting Andrey Shinkevich
2020-06-04 17:41 ` [PATCH v4 06/12] qcow2_format.py: use strings to specify c-type of struct fields Vladimir Sementsov-Ogievskiy
2020-06-05 11:36   ` Andrey Shinkevich
2020-06-05 20:19   ` Eric Blake
2020-06-04 17:41 ` [PATCH v4 07/12] qcow2_format.py: separate generic functionality of structure classes Vladimir Sementsov-Ogievskiy
2020-06-05 12:29   ` Andrey Shinkevich
2020-06-05 12:37     ` Vladimir Sementsov-Ogievskiy
2020-06-04 17:41 ` [PATCH v4 08/12] qcow2_format.py: add field-formatting class Vladimir Sementsov-Ogievskiy
2020-06-05 13:27   ` Andrey Shinkevich
2020-06-04 17:41 ` [PATCH v4 09/12] qcow2_format.py: QcowHeaderExtension: add dump method Vladimir Sementsov-Ogievskiy
2020-06-05 14:43   ` Andrey Shinkevich
2020-06-04 17:41 ` [PATCH v4 10/12] qcow2_format: refactor QcowHeaderExtension as a subclass of Qcow2Struct Vladimir Sementsov-Ogievskiy
2020-06-05 15:30   ` Andrey Shinkevich
2020-06-04 17:41 ` [PATCH v4 11/12] qcow2: QcowHeaderExtension print names for extension magics Vladimir Sementsov-Ogievskiy
2020-06-05 15:42   ` Andrey Shinkevich
2020-06-05 16:26   ` Vladimir Sementsov-Ogievskiy
2020-06-05 16:41     ` Andrey Shinkevich
2020-06-04 17:41 ` [PATCH v4 12/12] qcow2_format.py: dump bitmaps header extension Vladimir Sementsov-Ogievskiy
2020-06-05 16:06   ` Andrey Shinkevich
2020-06-04 19:14 ` [PATCH v4 00/12] iotests: Dump QCOW2 dirty bitmaps metadata no-reply
2020-06-05 10:02   ` Vladimir Sementsov-Ogievskiy
2020-06-05 20:50 ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200604174135.11042-6-vsementsov@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=andrey.shinkevich@virtuozzo.com \
    --cc=den@openvz.org \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.