From eca8fbcf22ef065bf89d254ed1441c6c8c336f71 Mon Sep 17 00:00:00 2001 From: me Date: Sun, 12 Jan 2020 20:24:18 +0200 Subject: [PATCH 1/3] add dosctring to bumblebee.util.bytefmt() --- bumblebee/util.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bumblebee/util.py b/bumblebee/util.py index 0d12105..b0ce1c0 100644 --- a/bumblebee/util.py +++ b/bumblebee/util.py @@ -46,6 +46,16 @@ def execute(cmd, wait=True): return rv def bytefmt(num): + """ + format a value of bytes to a more human readable pattern + example: 15 * 1024 becomes 15KiB + + Args: + + num (int): bytes + + Return: string + """ for unit in ["", "Ki", "Mi", "Gi"]: if num < 1024.0: return "{:.2f}{}B".format(num, unit) From 5496e307c82852511a3d45eec89db6de8401ae91 Mon Sep 17 00:00:00 2001 From: me Date: Sun, 12 Jan 2020 20:28:49 +0200 Subject: [PATCH 2/3] add format argument to bumblebee.util.bytefmt() it has a default value to be backwards compatible --- bumblebee/util.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bumblebee/util.py b/bumblebee/util.py index b0ce1c0..5f4ae44 100644 --- a/bumblebee/util.py +++ b/bumblebee/util.py @@ -45,7 +45,7 @@ def execute(cmd, wait=True): logging.info(u"command returned '{}'".format("" if not rv else rv)) return rv -def bytefmt(num): +def bytefmt(num, fmt="{:.2f}"): """ format a value of bytes to a more human readable pattern example: 15 * 1024 becomes 15KiB @@ -54,13 +54,15 @@ def bytefmt(num): num (int): bytes + fmt (string): format + Return: string """ for unit in ["", "Ki", "Mi", "Gi"]: if num < 1024.0: - return "{:.2f}{}B".format(num, unit) + return "{}{}B".format(fmt, unit).format(num) num /= 1024.0 - return "{:.2f}GiB".format(num*1024.0) + return "{}GiB".format(fmt).format(num*1024.0) def durationfmt(duration, shorten=False, suffix=False): duration = int(duration) From d952661b282082583a8155b105d8dd059141f108 Mon Sep 17 00:00:00 2001 From: me Date: Sun, 12 Jan 2020 20:33:51 +0200 Subject: [PATCH 3/3] add tests with custom format for bumblebee.util.bytefmt() --- tests/test_util.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_util.py b/tests/test_util.py index 828728b..1a7ee07 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -24,6 +24,13 @@ class TestUtil(unittest.TestCase): self.assertEquals(bu.bytefmt(22 * 1024 * 1024 * 1024), "22.00GiB") self.assertEquals(bu.bytefmt(35 * 1024 * 1024 * 1024 * 1024), "35840.00GiB") + fmt = "{:.0f}" + self.assertEquals(bu.bytefmt(10, fmt), "10B") + self.assertEquals(bu.bytefmt(15 * 1024, fmt), "15KiB") + self.assertEquals(bu.bytefmt(20 * 1024 * 1024, fmt), "20MiB") + self.assertEquals(bu.bytefmt(22 * 1024 * 1024 * 1024, fmt), "22GiB") + self.assertEquals(bu.bytefmt(35 * 1024 * 1024 * 1024 * 1024, fmt), "35840GiB") + def test_durationfmt(self): self.assertEquals(bu.durationfmt(00), "00:00") self.assertEquals(bu.durationfmt(25), "00:25")