From ed7af4de697a7d545476d523bdf8f1522c0e706a Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Tue, 2 Sep 2008 12:09:28 +0200 Subject: [PATCH] the binary size is human readable bzr revid: christophe@tinyerp.com-20080902100928-nv9i9e1guq2qpeh2 --- bin/osv/fields.py | 7 +------ bin/tools/misc.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/bin/osv/fields.py b/bin/osv/fields.py index ca30b8bcc74..9044c7f1929 100644 --- a/bin/osv/fields.py +++ b/bin/osv/fields.py @@ -211,11 +211,6 @@ class binary(_column): if not values: values = [] - def convert(b): - if not b: - return b - return '%d bytes' % b - res = {} for i in ids: val = None @@ -225,7 +220,7 @@ class binary(_column): break res.setdefault(i, val) if context.get('get_binary_size', True): - res[i] = convert(val) + res[i] = tools.human_size(val) return res diff --git a/bin/tools/misc.py b/bin/tools/misc.py index d1a90a35ac5..d0cc7398bed 100644 --- a/bin/tools/misc.py +++ b/bin/tools/misc.py @@ -672,6 +672,18 @@ def mod10r(number): return result + str((10 - report) % 10) +def human_size(sz): + """ + Return the size in a human readable format + """ + if not sz: + return False + units = ('bytes', 'Kb', 'Mb', 'Gb') + s, i = float(sz), 0 + while s >= 1024 and i < len(units)-1: + s = s / 1024 + i = i + 1 + return "%0.2f %s" % (s, units[i]) if __name__ == '__main__':