diff options
author | Justus Winter <4winter@informatik.uni-hamburg.de> | 2011-10-07 17:47:56 +0200 |
---|---|---|
committer | Justus Winter <4winter@informatik.uni-hamburg.de> | 2011-10-07 17:47:56 +0200 |
commit | 5c21dc0335881f53a54c0cd40a2d7ad650afd3b6 (patch) | |
tree | 1575e053da2754bc65d971a8ce4775b7bf65a67f | |
parent | 1a88e868617593007c13966e46331e619b2e8611 (diff) |
Add humanize_size to helper.py that humanizes sizes
-rw-r--r-- | alot/helper.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/alot/helper.py b/alot/helper.py index ce9a1b61..a06236c2 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -222,3 +222,23 @@ def tag_cmp(a, b): return cmp(len(a), len(b)) else: return cmp(a, b) + +def humanize_size(size): + r''' + >>> humanize_size(1) + '1' + >>> humanize_size(123) + '123' + >>> humanize_size(1234) + '1K' + >>> humanize_size(1234 * 1024) + '1.2M' + >>> humanize_size(1234 * 1024 * 1024) + '1234.0M' + ''' + for factor, format_string in ((1, '%i'), + (1024, '%iK'), + (1024 * 1024, '%.1fM')): + if size / factor < 1024: + return format_string % (float(size) / factor) + return format_string % (size / factor) |