From 1b84102265937425c5a9476f1092d8b14de3a9cc Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Fri, 27 Dec 2013 23:15:22 +0100 Subject: [PATCH 1/3] Remove unused code --- picard/util/bytes2human.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/picard/util/bytes2human.py b/picard/util/bytes2human.py index bfc7ceca7..34dc33459 100644 --- a/picard/util/bytes2human.py +++ b/picard/util/bytes2human.py @@ -18,8 +18,6 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. import locale -import picard.i18n - """ Helper class to convert bytes to human-readable form @@ -118,8 +116,3 @@ def calc_unit(number, multiple=1000): return (sign * n, suffix) else: n /= multiple - - -if __name__ == "__main__": - import doctest - doctest.testmod() From 7c6d3e9c3bfee7caf51c83e6acb28465ac0fc94f Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Fri, 27 Dec 2013 23:16:31 +0100 Subject: [PATCH 2/3] decimal(): fix missing parameter `prec` and update tests --- picard/util/bytes2human.py | 2 +- test/test_bytes2human.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/picard/util/bytes2human.py b/picard/util/bytes2human.py index 34dc33459..9b31c23b3 100644 --- a/picard/util/bytes2human.py +++ b/picard/util/bytes2human.py @@ -51,7 +51,7 @@ def decimal(number, prec=1): >>> [decimal(n) for n in [1000, 1024, 15500]] ['1 kB', '1 kB', '15.5 kB'] """ - return short_string(int(number), 1000) + return short_string(int(number), 1000, prec) def binary(number, prec=1): diff --git a/test/test_bytes2human.py b/test/test_bytes2human.py index 7a37737c2..9f8d53305 100644 --- a/test/test_bytes2human.py +++ b/test/test_bytes2human.py @@ -30,8 +30,11 @@ class Testbytes2human(unittest.TestCase): self.assertEqual(bytes2human.binary(45682), '44.6 KiB') self.assertEqual(bytes2human.binary(-45682), '-44.6 KiB') + self.assertEqual(bytes2human.binary(-45682, 2), '-44.61 KiB') self.assertEqual(bytes2human.decimal(45682), '45.7 kB') + self.assertEqual(bytes2human.decimal(45682, 2), '45.68 kB') self.assertEqual(bytes2human.decimal(9223372036854775807), '9223.4 PB') + self.assertEqual(bytes2human.decimal(9223372036854775807, 3), '9223.372 PB') self.assertEqual(bytes2human.decimal(123.6), '123 B') self.assertRaises(ValueError, bytes2human.decimal, 'xxx') self.assertRaises(ValueError, bytes2human.decimal, '123.6') From 5a02f9d719c1a0588d5eff675371265682782e29 Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Sat, 28 Dec 2013 02:16:05 +0100 Subject: [PATCH 3/3] `prec` -> `scale` --- picard/util/bytes2human.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/picard/util/bytes2human.py b/picard/util/bytes2human.py index 9b31c23b3..ea8fb4eb2 100644 --- a/picard/util/bytes2human.py +++ b/picard/util/bytes2human.py @@ -44,26 +44,26 @@ _BYTES_STRINGS_I18N = ( ) -def decimal(number, prec=1): +def decimal(number, scale=1): """ Convert bytes to short human-readable string, decimal mode >>> [decimal(n) for n in [1000, 1024, 15500]] ['1 kB', '1 kB', '15.5 kB'] """ - return short_string(int(number), 1000, prec) + return short_string(int(number), 1000, scale) -def binary(number, prec=1): +def binary(number, scale=1): """ Convert bytes to short human-readable string, binary mode >>> [binary(n) for n in [1000, 1024, 15500]] ['1000 B', '1 KiB', '15.1 KiB'] """ - return short_string(int(number), 1024, prec) + return short_string(int(number), 1024, scale) -def short_string(number, multiple, prec=1): +def short_string(number, multiple, scale=1): """ Returns short human-readable string for `number` bytes >>> [short_string(n, 1024, 2) for n in [1000, 1100, 15500]] @@ -73,12 +73,12 @@ def short_string(number, multiple, prec=1): """ num, unit = calc_unit(number, multiple) n = int(num) - nr = round(num, prec) + nr = round(num, scale) if n == nr or unit == 'B': fmt = '%d' num = n else: - fmt = '%%0.%df' % prec + fmt = '%%0.%df' % scale num = nr fmtnum = locale.format(fmt, num) return _("%s " + unit) % fmtnum