From 8df8c9b0eb113b01f3830d1169924dc73d13183d Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Thu, 3 Sep 2020 10:38:48 +0200 Subject: [PATCH 1/9] add force_unit flag --- humanfriendly/__init__.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/humanfriendly/__init__.py b/humanfriendly/__init__.py index 3982aad..2684849 100644 --- a/humanfriendly/__init__.py +++ b/humanfriendly/__init__.py @@ -157,7 +157,7 @@ def coerce_seconds(value): return value -def format_size(num_bytes, keep_width=False, binary=False): +def format_size(num_bytes, keep_width=False, binary=False, force_unit=None): """ Format a byte count as a human readable file size. @@ -166,6 +166,7 @@ def format_size(num_bytes, keep_width=False, binary=False): :data:`False` if they can be stripped. :param binary: :data:`True` to use binary multiples of bytes (base-2), :data:`False` to use decimal multiples of bytes (base-10). + :param force_unit: unit to force when converting the size :returns: The corresponding human readable file size (a string). This function knows how to format sizes in bytes, kilobytes, megabytes, @@ -184,7 +185,31 @@ def format_size(num_bytes, keep_width=False, binary=False): '1 KiB' >>> format_size(1000 ** 3 * 4) '4 GB' + >>> format_size(1000 ** 3 * 4, force_unit='MB') + '4000 MB' """ + if force_unit is not None: + force_unit_found = False + if binary: + symbols = [u.binary.symbol for u in disk_size_units] + else: + symbols = [u.decimal.symbol for u in disk_size_units] + force_unit_found = force_unit in symbols + if force_unit_found: + # this is going to fail for bytes + force_tuple_index = symbols.index(force_unit) + if binary: + number = round_number(float(num_bytes) / disk_size_units[force_tuple_index].binary.divider, keep_width=keep_width) + return pluralize(number, disk_size_units[force_tuple_index].binary.symbol, disk_size_units[force_tuple_index].binary.symbol) + else: + number = round_number(float(num_bytes) / disk_size_units[force_tuple_index].decimal.divider, keep_width=keep_width) + return pluralize(number, disk_size_units[force_tuple_index].decimal.symbol, disk_size_units[force_tuple_index].decimal.symbol) + else: + if force_unit == 'bytes': + return pluralize(round_number(float(num_bytes), keep_width=keep_width), 'byte') + else: + raise InvalidSize("You are trying to convert to an invalid unit: {}".format(force_unit)) + for unit in reversed(disk_size_units): if num_bytes >= unit.binary.divider and binary: number = round_number(float(num_bytes) / unit.binary.divider, keep_width=keep_width) From 24304d4823c0dfa719601bf147d94d19ce4b496a Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Thu, 3 Sep 2020 11:10:36 +0200 Subject: [PATCH 2/9] pep8 --- humanfriendly/__init__.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/humanfriendly/__init__.py b/humanfriendly/__init__.py index 2684849..1a6c2ac 100644 --- a/humanfriendly/__init__.py +++ b/humanfriendly/__init__.py @@ -199,16 +199,33 @@ def format_size(num_bytes, keep_width=False, binary=False, force_unit=None): # this is going to fail for bytes force_tuple_index = symbols.index(force_unit) if binary: - number = round_number(float(num_bytes) / disk_size_units[force_tuple_index].binary.divider, keep_width=keep_width) - return pluralize(number, disk_size_units[force_tuple_index].binary.symbol, disk_size_units[force_tuple_index].binary.symbol) + number = round_number( + float(num_bytes) / + disk_size_units[force_tuple_index].binary.divider, + keep_width=keep_width) + return pluralize( + number, + disk_size_units[force_tuple_index].binary.symbol, + disk_size_units[force_tuple_index].binary.symbol) else: - number = round_number(float(num_bytes) / disk_size_units[force_tuple_index].decimal.divider, keep_width=keep_width) - return pluralize(number, disk_size_units[force_tuple_index].decimal.symbol, disk_size_units[force_tuple_index].decimal.symbol) + number = round_number( + float(num_bytes) / + disk_size_units[force_tuple_index].decimal.divider, + keep_width=keep_width) + return pluralize( + number, + disk_size_units[force_tuple_index].decimal.symbol, + disk_size_units[force_tuple_index].decimal.symbol) else: if force_unit == 'bytes': - return pluralize(round_number(float(num_bytes), keep_width=keep_width), 'byte') + return pluralize( + round_number( + float(num_bytes), + keep_width=keep_width), + 'byte') else: - raise InvalidSize("You are trying to convert to an invalid unit: {}".format(force_unit)) + raise InvalidSize( + "You are trying to convert to an invalid unit: {}".format(force_unit)) for unit in reversed(disk_size_units): if num_bytes >= unit.binary.divider and binary: From c7c2992afe9f36d6abae3a93fa84b53d51e2f7a1 Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Thu, 3 Sep 2020 11:17:28 +0200 Subject: [PATCH 3/9] pep8 for usage.py --- humanfriendly/usage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/humanfriendly/usage.py b/humanfriendly/usage.py index 99a6b59..a045acb 100644 --- a/humanfriendly/usage.py +++ b/humanfriendly/usage.py @@ -258,7 +258,7 @@ def render_usage(text): ('\n\n'.join(render_paragraph(p, meta_variables) for p in split_paragraphs(description))).rstrip(), ]) csv_lines = csv_buffer.getvalue().splitlines() - output.append('\n'.join(' %s' % l for l in csv_lines)) + output.append('\n'.join(' %s' % line for line in csv_lines)) logger.debug("Rendered output: %s", output) return '\n\n'.join(trim_empty_lines(o) for o in output) From 5e63f8d1190150d544ad606b61b33601835b8862 Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Thu, 3 Sep 2020 11:21:48 +0200 Subject: [PATCH 4/9] pep8 for tests.py --- humanfriendly/tests.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py index 4e8c019..850c595 100644 --- a/humanfriendly/tests.py +++ b/humanfriendly/tests.py @@ -787,8 +787,8 @@ def test_spinner(self): .replace(ANSI_HIDE_CURSOR, '')) lines = [line for line in output.split(ANSI_ERASE_LINE) if line] self.assertTrue(len(lines) > 0) - self.assertTrue(all('test spinner' in l for l in lines)) - self.assertTrue(all('%' in l for l in lines)) + self.assertTrue(all('test spinner' in line for line in lines)) + self.assertTrue(all('%' in line for line in lines)) self.assertEqual(sorted(set(lines)), sorted(lines)) def test_automatic_spinner(self): @@ -952,7 +952,7 @@ def test_cli(self): # https://github.com/xolox/python-humanfriendly/issues/28 returncode, output = run_cli(main, '--demo') assert returncode == 0 - lines = [ansi_strip(l) for l in output.splitlines()] + lines = [ansi_strip(l) for line in output.splitlines()] assert "Text styles:" in lines assert "Foreground colors:" in lines assert "Background colors:" in lines From d68beba1e0d8860363efa983b42b2be3e9654e99 Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Thu, 3 Sep 2020 11:26:42 +0200 Subject: [PATCH 5/9] bugfix pep8 for tests.py --- humanfriendly/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py index 850c595..446e300 100644 --- a/humanfriendly/tests.py +++ b/humanfriendly/tests.py @@ -952,7 +952,7 @@ def test_cli(self): # https://github.com/xolox/python-humanfriendly/issues/28 returncode, output = run_cli(main, '--demo') assert returncode == 0 - lines = [ansi_strip(l) for line in output.splitlines()] + lines = [ansi_strip(line) for line in output.splitlines()] assert "Text styles:" in lines assert "Foreground colors:" in lines assert "Background colors:" in lines From 312482ed6264f41bfff034a2104c750540f0e545 Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Thu, 3 Sep 2020 12:53:08 +0200 Subject: [PATCH 6/9] add new tests for format_size --- humanfriendly/tests.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py index 446e300..e8367dc 100644 --- a/humanfriendly/tests.py +++ b/humanfriendly/tests.py @@ -503,6 +503,22 @@ def test_format_size(self): self.assertEqual('1 EB', format_size(1000 ** 6)) self.assertEqual('1 ZB', format_size(1000 ** 7)) self.assertEqual('1 YB', format_size(1000 ** 8)) + self.assertEqual('0 bytes', format_size(0), force_unit='bytes') + self.assertEqual('1 byte', format_size(1), force_unit='bytes') + self.assertEqual('42 bytes', format_size(42), force_unit='bytes') + self.assertEqual('1 KB', format_size(1000 ** 1), force_unit='KB') + self.assertEqual('1 MB', format_size(1000 ** 2), force_unit='MB') + self.assertEqual('1 GB', format_size(1000 ** 3), force_unit='GB') + self.assertEqual('1 TB', format_size(1000 ** 4), force_unit='TB') + self.assertEqual('1 PB', format_size(1000 ** 5), force_unit='PB') + self.assertEqual('1 EB', format_size(1000 ** 6), force_unit='EZ') + self.assertEqual('1 ZB', format_size(1000 ** 7), force_unit='ZB') + self.assertEqual('1 YB', format_size(1000 ** 8), force_unit='YB') + self.assertEqual('4 TB', format_size(1000 ** 4 * 4), force_unit='TB') + self.assertEqual('4000 GB', format_size(1000 ** 4 * 4), force_unit='GB') + self.assertEqual('4000000 MB', format_size(1000 ** 4 * 4), force_unit='MB') + self.assertEqual('4000000000 KB', format_size(1000 ** 4 * 4), force_unit='KB') + self.assertEqual('4000000000000 bytes', format_size(1000 ** 4 * 4), force_unit='bytes') self.assertEqual('1 KiB', format_size(1024 ** 1, binary=True)) self.assertEqual('1 MiB', format_size(1024 ** 2, binary=True)) self.assertEqual('1 GiB', format_size(1024 ** 3, binary=True)) @@ -511,6 +527,19 @@ def test_format_size(self): self.assertEqual('1 EiB', format_size(1024 ** 6, binary=True)) self.assertEqual('1 ZiB', format_size(1024 ** 7, binary=True)) self.assertEqual('1 YiB', format_size(1024 ** 8, binary=True)) + self.assertEqual('1 KiB', format_size(1024 ** 1), binary=True, force_unit='KiB') + self.assertEqual('1 MiB', format_size(1024 ** 2), binary=True, force_unit='MiB') + self.assertEqual('1 GiB', format_size(1024 ** 3), binary=True, force_unit='GiB') + self.assertEqual('1 TiB', format_size(1024 ** 4), binary=True, force_unit='TiB') + self.assertEqual('1 PiB', format_size(1024 ** 5), binary=True, force_unit='PiB') + self.assertEqual('1 EiB', format_size(1024 ** 6), binary=True, force_unit='EiZ') + self.assertEqual('1 ZiB', format_size(1024 ** 7), binary=True, force_unit='ZiB') + self.assertEqual('1 YiB', format_size(1024 ** 8), binary=True, force_unit='YiB') + self.assertEqual('4 TiB', format_size(1024 ** 4 * 4), binary=True, force_unit='TiB') + self.assertEqual('4096 GiB', format_size(1024 ** 4 * 4), binary=True, force_unit='GiB') + self.assertEqual('4194304 MiB', format_size(1024 ** 4 * 4), binary=True, force_unit='MiB') + self.assertEqual('4294967296 KiB', format_size(1024 ** 4 * 4), binary=True, force_unit='KiB') + self.assertEqual('4398046511104 bytes', format_size(1024 ** 4 * 4), binary=True, force_unit='bytes') self.assertEqual('45 KB', format_size(1000 * 45)) self.assertEqual('2.9 TB', format_size(1000 ** 4 * 2.9)) From 6c05a94ceea0bf7830b572efee2adc801e01c9e4 Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Thu, 3 Sep 2020 13:02:31 +0200 Subject: [PATCH 7/9] bugfix new tests for format_size --- humanfriendly/tests.py | 52 +++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py index e8367dc..866c303 100644 --- a/humanfriendly/tests.py +++ b/humanfriendly/tests.py @@ -506,19 +506,19 @@ def test_format_size(self): self.assertEqual('0 bytes', format_size(0), force_unit='bytes') self.assertEqual('1 byte', format_size(1), force_unit='bytes') self.assertEqual('42 bytes', format_size(42), force_unit='bytes') - self.assertEqual('1 KB', format_size(1000 ** 1), force_unit='KB') - self.assertEqual('1 MB', format_size(1000 ** 2), force_unit='MB') - self.assertEqual('1 GB', format_size(1000 ** 3), force_unit='GB') - self.assertEqual('1 TB', format_size(1000 ** 4), force_unit='TB') - self.assertEqual('1 PB', format_size(1000 ** 5), force_unit='PB') - self.assertEqual('1 EB', format_size(1000 ** 6), force_unit='EZ') - self.assertEqual('1 ZB', format_size(1000 ** 7), force_unit='ZB') - self.assertEqual('1 YB', format_size(1000 ** 8), force_unit='YB') - self.assertEqual('4 TB', format_size(1000 ** 4 * 4), force_unit='TB') - self.assertEqual('4000 GB', format_size(1000 ** 4 * 4), force_unit='GB') - self.assertEqual('4000000 MB', format_size(1000 ** 4 * 4), force_unit='MB') - self.assertEqual('4000000000 KB', format_size(1000 ** 4 * 4), force_unit='KB') - self.assertEqual('4000000000000 bytes', format_size(1000 ** 4 * 4), force_unit='bytes') + self.assertEqual('1 KB', format_size(1000 ** 1, force_unit='KB')) + self.assertEqual('1 MB', format_size(1000 ** 2, force_unit='MB')) + self.assertEqual('1 GB', format_size(1000 ** 3, force_unit='GB')) + self.assertEqual('1 TB', format_size(1000 ** 4, force_unit='TB')) + self.assertEqual('1 PB', format_size(1000 ** 5, force_unit='PB')) + self.assertEqual('1 EB', format_size(1000 ** 6, force_unit='EZ')) + self.assertEqual('1 ZB', format_size(1000 ** 7, force_unit='ZB')) + self.assertEqual('1 YB', format_size(1000 ** 8, force_unit='YB')) + self.assertEqual('4 TB', format_size(1000 ** 4 * 4, force_unit='TB')) + self.assertEqual('4000 GB', format_size(1000 ** 4 * 4, force_unit='GB')) + self.assertEqual('4000000 MB', format_size(1000 ** 4 * 4, force_unit='MB')) + self.assertEqual('4000000000 KB', format_size(1000 ** 4 * 4, force_unit='KB')) + self.assertEqual('4000000000000 bytes', format_size(1000 ** 4 * 4, force_unit='bytes')) self.assertEqual('1 KiB', format_size(1024 ** 1, binary=True)) self.assertEqual('1 MiB', format_size(1024 ** 2, binary=True)) self.assertEqual('1 GiB', format_size(1024 ** 3, binary=True)) @@ -527,19 +527,19 @@ def test_format_size(self): self.assertEqual('1 EiB', format_size(1024 ** 6, binary=True)) self.assertEqual('1 ZiB', format_size(1024 ** 7, binary=True)) self.assertEqual('1 YiB', format_size(1024 ** 8, binary=True)) - self.assertEqual('1 KiB', format_size(1024 ** 1), binary=True, force_unit='KiB') - self.assertEqual('1 MiB', format_size(1024 ** 2), binary=True, force_unit='MiB') - self.assertEqual('1 GiB', format_size(1024 ** 3), binary=True, force_unit='GiB') - self.assertEqual('1 TiB', format_size(1024 ** 4), binary=True, force_unit='TiB') - self.assertEqual('1 PiB', format_size(1024 ** 5), binary=True, force_unit='PiB') - self.assertEqual('1 EiB', format_size(1024 ** 6), binary=True, force_unit='EiZ') - self.assertEqual('1 ZiB', format_size(1024 ** 7), binary=True, force_unit='ZiB') - self.assertEqual('1 YiB', format_size(1024 ** 8), binary=True, force_unit='YiB') - self.assertEqual('4 TiB', format_size(1024 ** 4 * 4), binary=True, force_unit='TiB') - self.assertEqual('4096 GiB', format_size(1024 ** 4 * 4), binary=True, force_unit='GiB') - self.assertEqual('4194304 MiB', format_size(1024 ** 4 * 4), binary=True, force_unit='MiB') - self.assertEqual('4294967296 KiB', format_size(1024 ** 4 * 4), binary=True, force_unit='KiB') - self.assertEqual('4398046511104 bytes', format_size(1024 ** 4 * 4), binary=True, force_unit='bytes') + self.assertEqual('1 KiB', format_size(1024 ** 1, binary=True, force_unit='KiB')) + self.assertEqual('1 MiB', format_size(1024 ** 2, binary=True, force_unit='MiB')) + self.assertEqual('1 GiB', format_size(1024 ** 3, binary=True, force_unit='GiB')) + self.assertEqual('1 TiB', format_size(1024 ** 4, binary=True, force_unit='TiB')) + self.assertEqual('1 PiB', format_size(1024 ** 5, binary=True, force_unit='PiB')) + self.assertEqual('1 EiB', format_size(1024 ** 6, binary=True, force_unit='EiZ')) + self.assertEqual('1 ZiB', format_size(1024 ** 7, binary=True, force_unit='ZiB')) + self.assertEqual('1 YiB', format_size(1024 ** 8, binary=True, force_unit='YiB')) + self.assertEqual('4 TiB', format_size(1024 ** 4 * 4, binary=True, force_unit='TiB')) + self.assertEqual('4096 GiB', format_size(1024 ** 4 * 4, binary=True, force_unit='GiB')) + self.assertEqual('4194304 MiB', format_size(1024 ** 4 * 4, binary=True, force_unit='MiB')) + self.assertEqual('4294967296 KiB', format_size(1024 ** 4 * 4, binary=True, force_unit='KiB')) + self.assertEqual('4398046511104 bytes', format_size(1024 ** 4 * 4, binary=True, force_unit='bytes')) self.assertEqual('45 KB', format_size(1000 * 45)) self.assertEqual('2.9 TB', format_size(1000 ** 4 * 2.9)) From b7e024ca9723f2dd5b23be9345f290a51fb5b523 Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Thu, 3 Sep 2020 13:07:19 +0200 Subject: [PATCH 8/9] bugfix new tests for format_size --- humanfriendly/tests.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py index 866c303..e2875fd 100644 --- a/humanfriendly/tests.py +++ b/humanfriendly/tests.py @@ -503,9 +503,9 @@ def test_format_size(self): self.assertEqual('1 EB', format_size(1000 ** 6)) self.assertEqual('1 ZB', format_size(1000 ** 7)) self.assertEqual('1 YB', format_size(1000 ** 8)) - self.assertEqual('0 bytes', format_size(0), force_unit='bytes') - self.assertEqual('1 byte', format_size(1), force_unit='bytes') - self.assertEqual('42 bytes', format_size(42), force_unit='bytes') + self.assertEqual('0 bytes', format_size(0, force_unit='bytes')) + self.assertEqual('1 byte', format_size(1, force_unit='bytes')) + self.assertEqual('42 bytes', format_size(42, force_unit='bytes')) self.assertEqual('1 KB', format_size(1000 ** 1, force_unit='KB')) self.assertEqual('1 MB', format_size(1000 ** 2, force_unit='MB')) self.assertEqual('1 GB', format_size(1000 ** 3, force_unit='GB')) From 57ba6f73f172ef3087a680c4dea6bf518bdb6053 Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Thu, 3 Sep 2020 13:12:02 +0200 Subject: [PATCH 9/9] bugfix new tests for format_size --- humanfriendly/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py index e2875fd..878dc28 100644 --- a/humanfriendly/tests.py +++ b/humanfriendly/tests.py @@ -511,7 +511,7 @@ def test_format_size(self): self.assertEqual('1 GB', format_size(1000 ** 3, force_unit='GB')) self.assertEqual('1 TB', format_size(1000 ** 4, force_unit='TB')) self.assertEqual('1 PB', format_size(1000 ** 5, force_unit='PB')) - self.assertEqual('1 EB', format_size(1000 ** 6, force_unit='EZ')) + self.assertEqual('1 EB', format_size(1000 ** 6, force_unit='EB')) self.assertEqual('1 ZB', format_size(1000 ** 7, force_unit='ZB')) self.assertEqual('1 YB', format_size(1000 ** 8, force_unit='YB')) self.assertEqual('4 TB', format_size(1000 ** 4 * 4, force_unit='TB')) @@ -532,7 +532,7 @@ def test_format_size(self): self.assertEqual('1 GiB', format_size(1024 ** 3, binary=True, force_unit='GiB')) self.assertEqual('1 TiB', format_size(1024 ** 4, binary=True, force_unit='TiB')) self.assertEqual('1 PiB', format_size(1024 ** 5, binary=True, force_unit='PiB')) - self.assertEqual('1 EiB', format_size(1024 ** 6, binary=True, force_unit='EiZ')) + self.assertEqual('1 EiB', format_size(1024 ** 6, binary=True, force_unit='EiB')) self.assertEqual('1 ZiB', format_size(1024 ** 7, binary=True, force_unit='ZiB')) self.assertEqual('1 YiB', format_size(1024 ** 8, binary=True, force_unit='YiB')) self.assertEqual('4 TiB', format_size(1024 ** 4 * 4, binary=True, force_unit='TiB'))