summaryrefslogtreecommitdiff
path: root/swiftclient/utils.py
diff options
context:
space:
mode:
authorTim Burke <tim.burke@gmail.com>2015-09-09 17:41:21 -0700
committerTim Burke <tim.burke@gmail.com>2015-10-07 22:33:42 -0700
commit9fed7ed5e1f6dd3e589a35e3ee4abecb676f2188 (patch)
tree9fc610c348fc753a98422b3f8551ca2a8a3776d4 /swiftclient/utils.py
parent43b2c6bfe5140f32a37638985bd4cb7b73988160 (diff)
downloadpython-swiftclient-9fed7ed5e1f6dd3e589a35e3ee4abecb676f2188.tar.gz
Miscellaneous (mostly test) cleanup
* Always use testtools.TestCase, since we're relying on testtools * Always use mock (as opposed to unittest.mock) since we're relying on mock * Add note about when a missing logging handler was added * Stop %-formatting the giant usage string that doesn't actually need any formatting * Prefer assertIs, assertIn, assertIsInstance over assertTrue * Use else-self.fail instead of sentinel values * Check resp.get('error') is None before checking resp['success'] is True, so test failures actually tell you something useful * Tighten some isinstance assertions * Import MockHttpTest from correct location * Only populate clean_os_environ once * Use setUp for setup, not __init__ * Replace assertIn(key, dict) and assertEqual(foo, dict[key]) with assertEqual(foo, dict.get(key)) when key is a literal and foo is not None * Use mock.patch.object instead of manually patching for tests * Use six.binary_type instead of type(''.encode('utf-8')) * Stop shadowing builtin bytes * Reclaim some margin * Stop checking the return-type of encode_utf8; we already know it's bytes Change-Id: I2138ea553378ce88810b7353147c8645a8f8c90e
Diffstat (limited to 'swiftclient/utils.py')
-rw-r--r--swiftclient/utils.py38
1 files changed, 17 insertions, 21 deletions
diff --git a/swiftclient/utils.py b/swiftclient/utils.py
index 8316a8f..8ef0403 100644
--- a/swiftclient/utils.py
+++ b/swiftclient/utils.py
@@ -35,34 +35,30 @@ def config_true_value(value):
(isinstance(value, six.string_types) and value.lower() in TRUE_VALUES)
-def prt_bytes(bytes, human_flag):
+def prt_bytes(num_bytes, human_flag):
"""
convert a number > 1024 to printable format, either in 4 char -h format as
with ls -lh or return as 12 char right justified string
"""
- if human_flag:
- suffix = ''
- mods = list('KMGTPEZY')
- temp = float(bytes)
- if temp > 0:
- while temp > 1023:
- try:
- suffix = mods.pop(0)
- except IndexError:
- break
- temp /= 1024.0
- if suffix != '':
- if temp >= 10:
- bytes = '%3d%s' % (temp, suffix)
- else:
- bytes = '%.1f%s' % (temp, suffix)
- if suffix == '': # must be < 1024
- bytes = '%4s' % bytes
+ if not human_flag:
+ return '%12s' % num_bytes
+
+ num = float(num_bytes)
+ suffixes = [None] + list('KMGTPEZY')
+ for suffix in suffixes[:-1]:
+ if num <= 1023:
+ break
+ num /= 1024.0
else:
- bytes = '%12s' % bytes
+ suffix = suffixes[-1]
- return bytes
+ if not suffix: # num_bytes must be < 1024
+ return '%4s' % num_bytes
+ elif num >= 10:
+ return '%3d%s' % (num, suffix)
+ else:
+ return '%.1f%s' % (num, suffix)
def generate_temp_url(path, seconds, key, method, absolute=False):