From f02d24d8d2c79a8f053874b373e33e011fd11f13 Mon Sep 17 00:00:00 2001 From: Pierre Mdawar Date: Tue, 28 Dec 2021 03:38:31 +0530 Subject: [PATCH] [utils] Fix `format_bytes` output for Bytes (#2132) Authored by: pukkandan, mdawar --- test/test_utils.py | 13 +++++++++++++ yt_dlp/utils.py | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/test/test_utils.py b/test/test_utils.py index 4edf4e1b1..2e33308c7 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -37,6 +37,7 @@ ExtractorError, find_xpath_attr, fix_xml_ampersands, + format_bytes, float_or_none, get_element_by_class, get_element_by_attribute, @@ -1688,6 +1689,18 @@ def test(ll, idx, val, cache): ll = reversed(ll) test(ll, -15, 14, range(15)) + def test_format_bytes(self): + self.assertEqual(format_bytes(0), '0.00B') + self.assertEqual(format_bytes(1000), '1000.00B') + self.assertEqual(format_bytes(1024), '1.00KiB') + self.assertEqual(format_bytes(1024**2), '1.00MiB') + self.assertEqual(format_bytes(1024**3), '1.00GiB') + self.assertEqual(format_bytes(1024**4), '1.00TiB') + self.assertEqual(format_bytes(1024**5), '1.00PiB') + self.assertEqual(format_bytes(1024**6), '1.00EiB') + self.assertEqual(format_bytes(1024**7), '1.00ZiB') + self.assertEqual(format_bytes(1024**8), '1.00YiB') + if __name__ == '__main__': unittest.main() diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index cdc9a0ecf..5ce6df7ac 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -2118,11 +2118,11 @@ def format_decimal_suffix(num, fmt='%d%s', *, factor=1000): exponent = 0 if num == 0 else int(math.log(num, factor)) suffix = ['', *'KMGTPEZY'][exponent] converted = num / (factor ** exponent) - return fmt % (converted, suffix) + return fmt % (converted, f'{suffix}i' if suffix and factor == 1024 else suffix) def format_bytes(bytes): - return format_decimal_suffix(bytes, '%.2f%siB', factor=1024) or 'N/A' + return format_decimal_suffix(bytes, '%.2f%sB', factor=1024) or 'N/A' def lookup_unit_table(unit_table, s):