aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichał Górny <mgorny@gentoo.org>2022-06-13 18:09:48 +0200
committerMichał Górny <mgorny@gentoo.org>2022-06-13 19:59:22 +0200
commit1539b683b880ee63bd6c5f7ea43ab81d5ee44e31 (patch)
tree7d7deb6617cc65ff2eb4cafaba86f3a5f28de2a3
parentosutils: add tests for sizeof_fmt() (diff)
downloadsnakeoil-1539b683b880ee63bd6c5f7ea43ab81d5ee44e31.tar.gz
snakeoil-1539b683b880ee63bd6c5f7ea43ab81d5ee44e31.tar.bz2
snakeoil-1539b683b880ee63bd6c5f7ea43ab81d5ee44e31.zip
osutils: simplify sizeof_fmt() and fix handling sizes > 1000 YB
-rw-r--r--src/snakeoil/osutils/__init__.py13
-rw-r--r--tests/test_osutils.py5
2 files changed, 10 insertions, 8 deletions
diff --git a/src/snakeoil/osutils/__init__.py b/src/snakeoil/osutils/__init__.py
index b034a36..52156f2 100644
--- a/src/snakeoil/osutils/__init__.py
+++ b/src/snakeoil/osutils/__init__.py
@@ -340,16 +340,19 @@ def unlink_if_exists(path):
def sizeof_fmt(size, binary=True):
if binary:
- units = ('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')
+ units = ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')
increment = 1024.0
else:
- units = ('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
+ units = ('kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
increment = 1000.0
- for i, x in enumerate(units):
- if size < increment or i == len(units):
- return "%3.1f %s" % (size, x)
+ unit = "B"
+ for x in units:
+ if size < increment:
+ break
size /= increment
+ unit = x
+ return "%3.1f %s" % (size, unit)
def stat_mtime_long(path, st=None):
diff --git a/tests/test_osutils.py b/tests/test_osutils.py
index ca57f72..2a1668b 100644
--- a/tests/test_osutils.py
+++ b/tests/test_osutils.py
@@ -497,9 +497,8 @@ class TestSizeofFmt:
1024**3: ("1.1 GB", "1.0 GiB"),
1000**8: ("1.0 YB", "847.0 ZiB"),
1024**8: ("1.2 YB", "1.0 YiB"),
- # TODO: fix sizeof_fmt()
- # 1000**9: ("1000.0 YB", "827.2 YiB"),
- # 1024**9: ("1237.9 YB", "1024.0 YiB"),
+ 1000**9: ("1000.0 YB", "827.2 YiB"),
+ 1024**9: ("1237.9 YB", "1024.0 YiB"),
}
@pytest.mark.parametrize("binary", (False, True))