From 4266c4852ce5e940284085721ca35f97bd578597 Mon Sep 17 00:00:00 2001 From: Aaron Tan Date: Wed, 4 Aug 2021 21:02:43 +1000 Subject: [PATCH 1/2] Improve rendering of size column --- mitmproxy/utils/human.py | 2 +- test/mitmproxy/utils/test_human.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mitmproxy/utils/human.py b/mitmproxy/utils/human.py index 951feef4a..b301ba1b1 100644 --- a/mitmproxy/utils/human.py +++ b/mitmproxy/utils/human.py @@ -20,7 +20,7 @@ def pretty_size(size): if bottom[1] <= size < top[1]: suf = bottom[0] lim = bottom[1] - x = round(size / lim, 2) + x = round(size / lim) if x == int(x): x = int(x) return str(x) + suf diff --git a/test/mitmproxy/utils/test_human.py b/test/mitmproxy/utils/test_human.py index 6f8bf732f..9317388d4 100644 --- a/test/mitmproxy/utils/test_human.py +++ b/test/mitmproxy/utils/test_human.py @@ -29,7 +29,7 @@ def test_pretty_size(): assert human.pretty_size(0) == "0b" assert human.pretty_size(100) == "100b" assert human.pretty_size(1024) == "1k" - assert human.pretty_size(1024 + (1024 / 2.0)) == "1.5k" + assert human.pretty_size(1024 + (1024 / 2.0)) == "2k" assert human.pretty_size(1024 * 1024) == "1m" assert human.pretty_size(10 * 1024 * 1024) == "10m" From 6aa9d8658f12f81d03916829c564448211a2996f Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 4 Aug 2021 17:06:47 +0200 Subject: [PATCH 2/2] size column: add a bit more precision --- mitmproxy/utils/human.py | 42 ++++++++++++++++-------------- test/mitmproxy/utils/test_human.py | 9 ++++--- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/mitmproxy/utils/human.py b/mitmproxy/utils/human.py index b301ba1b1..1c9654b5b 100644 --- a/mitmproxy/utils/human.py +++ b/mitmproxy/utils/human.py @@ -1,30 +1,34 @@ import datetime +import functools import ipaddress import time -import functools import typing -SIZE_TABLE = [ - ("b", 1024 ** 0), - ("k", 1024 ** 1), - ("m", 1024 ** 2), - ("g", 1024 ** 3), - ("t", 1024 ** 4), -] -SIZE_UNITS = dict(SIZE_TABLE) +SIZE_UNITS = { + "b": 1024 ** 0, + "k": 1024 ** 1, + "m": 1024 ** 2, + "g": 1024 ** 3, + "t": 1024 ** 4, +} -def pretty_size(size): - for bottom, top in zip(SIZE_TABLE, SIZE_TABLE[1:]): - if bottom[1] <= size < top[1]: - suf = bottom[0] - lim = bottom[1] - x = round(size / lim) - if x == int(x): - x = int(x) - return str(x) + suf - return "{}{}".format(size, SIZE_TABLE[0][0]) +def pretty_size(size: int) -> str: + """Convert a number of bytes into a human-readable string. + + len(return value) <= 5 always holds true. + """ + s: float = size # type cast for mypy + if s < 1024: + return f"{s}b" + for suffix in ["k", "m", "g", "t"]: + s /= 1024 + if s < 99.95: + return f"{s:.1f}{suffix}" + if s < 1024 or suffix == "t": + return f"{s:.0f}{suffix}" + raise AssertionError @functools.lru_cache() diff --git a/test/mitmproxy/utils/test_human.py b/test/mitmproxy/utils/test_human.py index 9317388d4..91e123f55 100644 --- a/test/mitmproxy/utils/test_human.py +++ b/test/mitmproxy/utils/test_human.py @@ -28,10 +28,11 @@ def test_parse_size(): def test_pretty_size(): assert human.pretty_size(0) == "0b" assert human.pretty_size(100) == "100b" - assert human.pretty_size(1024) == "1k" - assert human.pretty_size(1024 + (1024 / 2.0)) == "2k" - assert human.pretty_size(1024 * 1024) == "1m" - assert human.pretty_size(10 * 1024 * 1024) == "10m" + assert human.pretty_size(1024) == "1.0k" + assert human.pretty_size(1024 + 512) == "1.5k" + assert human.pretty_size(1024 * 1024) == "1.0m" + assert human.pretty_size(10 * 1024 * 1024) == "10.0m" + assert human.pretty_size(100 * 1024 * 1024) == "100m" def test_pretty_duration():