From e067bfd8a24ced35030670c5df01d46b1060504c Mon Sep 17 00:00:00 2001 From: Ponnuvel Palaniyappan Date: Thu, 8 Aug 2024 16:31:47 +0100 Subject: [PATCH] [common] Fix a bug in f-string usage The commit d65e135c8 introduced a bug when converting formats to use f-strings. This appears to have broken the convert_bytes function. Signed-off-by: Ponnuvel Palaniyappan --- sos/utilities.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/sos/utilities.py b/sos/utilities.py index 37994d8218..e61b18f95a 100644 --- a/sos/utilities.py +++ b/sos/utilities.py @@ -121,18 +121,13 @@ def fileobj(path_or_file, mode='r'): return closing(path_or_file) -def convert_bytes(bytes_, K=1 << 10, M=1 << 20, G=1 << 30, T=1 << 40): +def convert_bytes(num_bytes): """Converts a number of bytes to a shorter, more human friendly format""" - fn = float(bytes_) - if bytes_ >= T: - return f'{(fn / T):.1fT}' - if bytes_ >= G: - return f'{(fn / G):.1fG}' - if bytes_ >= M: - return f'{(fn / M):.1fM}' - if bytes_ >= K: - return f'{(fn / K):.1fK}' - return f'{bytes_}' + sizes = {'T': 1 << 40, 'G': 1 << 30, 'M': 1 << 20, 'K': 1 << 10} + for symbol, size in sizes.items(): + if num_bytes >= size: + return f"{float(num_bytes) / size:.1f}{symbol}" + return f"{num_bytes}" def file_is_binary(fname):