diff options
| author | Brian Waldon <bcwaldon@gmail.com> | 2012-11-19 10:35:04 -0800 |
|---|---|---|
| committer | Brian Waldon <bcwaldon@gmail.com> | 2012-11-19 10:35:04 -0800 |
| commit | fe17d3517482525527d4af9b24df64e7651ebe3a (patch) | |
| tree | 264f23f9c47115d8edaf8574988b17c936240d4e | |
| parent | b24832c22aa44d2f8b5ecddaf12e7878653af28f (diff) | |
| download | python-glanceclient-fe17d3517482525527d4af9b24df64e7651ebe3a.tar.gz | |
Simplify human-readable size output
* Limit human-readable sizes to a single decimal
* Drop trailing zero
* Step one suffix further in the case of a size being 1024
Change-Id: I2eb8ac0571d3d08b52f62155912863870573a37c
| -rw-r--r-- | glanceclient/common/utils.py | 7 | ||||
| -rw-r--r-- | tests/test_utils.py | 8 |
2 files changed, 10 insertions, 5 deletions
diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index 628225d..ac4eef8 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -175,8 +175,11 @@ def make_size_human_readable(size): base = 1024.0 index = 0 - while size > base: + while size >= base: index = index + 1 size = size / base - return "%.3f%s" % (size, suffix[index]) + padded = '%.1f' % size + stripped = padded.rstrip('0').rstrip('.') + + return '%s%s' % (stripped, suffix[index]) diff --git a/tests/test_utils.py b/tests/test_utils.py index d277c3c..f6fe14d 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -45,6 +45,8 @@ class TestUtils(unittest.TestCase): data = ''.join([f for f in utils.integrity_iter(fixture, checksum)]) def test_make_size_human_readable(self): - self.assertEqual("1024.000kB", utils.make_size_human_readable(1048576)) - self.assertEqual("1.375GB", utils.make_size_human_readable(1476395008)) - self.assertEqual("9.309MB", utils.make_size_human_readable(9761280)) + self.assertEqual("106B", utils.make_size_human_readable(106)) + self.assertEqual("1000kB", utils.make_size_human_readable(1024000)) + self.assertEqual("1MB", utils.make_size_human_readable(1048576)) + self.assertEqual("1.4GB", utils.make_size_human_readable(1476395008)) + self.assertEqual("9.3MB", utils.make_size_human_readable(9761280)) |
