From b2f63458fcda7878d5cf674c2f1e9ca7db5bf3ce Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 31 May 2016 19:32:08 +1200 Subject: Move human-friendly format functions to netlib.human, remove redundant implementations --- netlib/human.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 netlib/human.py (limited to 'netlib/human.py') diff --git a/netlib/human.py b/netlib/human.py new file mode 100644 index 00000000..f4640c00 --- /dev/null +++ b/netlib/human.py @@ -0,0 +1,51 @@ +SIZE_UNITS = dict( + b=1024 ** 0, + k=1024 ** 1, + m=1024 ** 2, + g=1024 ** 3, + t=1024 ** 4, +) + + +def pretty_size(size): + suffixes = [ + ("B", 2 ** 10), + ("kB", 2 ** 20), + ("MB", 2 ** 30), + ] + for suf, lim in suffixes: + if size >= lim: + continue + else: + x = round(size / float(lim / 2 ** 10), 2) + if x == int(x): + x = int(x) + return str(x) + suf + + +def parse_size(s): + try: + return int(s) + except ValueError: + pass + for i in SIZE_UNITS.keys(): + if s.endswith(i): + try: + return int(s[:-1]) * SIZE_UNITS[i] + except ValueError: + break + raise ValueError("Invalid size specification.") + + +def pretty_duration(secs): + formatters = [ + (100, "{:.0f}s"), + (10, "{:2.1f}s"), + (1, "{:1.2f}s"), + ] + + for limit, formatter in formatters: + if secs >= limit: + return formatter.format(secs) + # less than 1 sec + return "{:.0f}ms".format(secs * 1000) -- cgit v1.2.3 From f62efed304d7ecd8f6149ff98577b381b4a3a3c9 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 31 May 2016 19:45:48 +1200 Subject: Unify and make symmetric pretty_size and parse_size --- netlib/human.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'netlib/human.py') diff --git a/netlib/human.py b/netlib/human.py index f4640c00..9eccd35b 100644 --- a/netlib/human.py +++ b/netlib/human.py @@ -1,26 +1,25 @@ -SIZE_UNITS = dict( - b=1024 ** 0, - k=1024 ** 1, - m=1024 ** 2, - g=1024 ** 3, - t=1024 ** 4, -) + +SIZE_TABLE = [ + ("b", 1024 ** 0), + ("k", 1024 ** 1), + ("m", 1024 ** 2), + ("g", 1024 ** 3), + ("t", 1024 ** 4), +] + +SIZE_UNITS = dict(SIZE_TABLE) def pretty_size(size): - suffixes = [ - ("B", 2 ** 10), - ("kB", 2 ** 20), - ("MB", 2 ** 30), - ] - for suf, lim in suffixes: - if size >= lim: - continue - else: - x = round(size / float(lim / 2 ** 10), 2) + 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, 2) if x == int(x): x = int(x) return str(x) + suf + return "%s%s"%(size, SIZE_TABLE[0][0]) def parse_size(s): -- cgit v1.2.3 From 40a030f215e1943aefdb2eb6fe2a264b9b1ee33c Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 31 May 2016 19:58:28 +1200 Subject: Satisfy flake8 --- netlib/human.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'netlib/human.py') diff --git a/netlib/human.py b/netlib/human.py index 9eccd35b..a007adc7 100644 --- a/netlib/human.py +++ b/netlib/human.py @@ -19,7 +19,7 @@ def pretty_size(size): if x == int(x): x = int(x) return str(x) + suf - return "%s%s"%(size, SIZE_TABLE[0][0]) + return "%s%s" % (size, SIZE_TABLE[0][0]) def parse_size(s): -- cgit v1.2.3