2013-03-24 15:23:18 +01:00
|
|
|
from __future__ import division
|
2015-01-23 22:04:42 +01:00
|
|
|
import json
|
|
|
|
|
|
|
|
from httpie.compat import is_py26, OrderedDict
|
|
|
|
|
|
|
|
|
|
|
|
def load_json_preserve_order(s):
|
|
|
|
if is_py26:
|
|
|
|
return json.loads(s)
|
|
|
|
return json.loads(s, object_pairs_hook=OrderedDict)
|
2013-02-26 15:12:33 +01:00
|
|
|
|
|
|
|
|
2013-03-04 02:35:01 +01:00
|
|
|
def humanize_bytes(n, precision=2):
|
2013-04-10 16:48:18 +02:00
|
|
|
# Author: Doug Latornell
|
|
|
|
# Licence: MIT
|
|
|
|
# URL: http://code.activestate.com/recipes/577081/
|
2013-02-26 15:12:33 +01:00
|
|
|
"""Return a humanized string representation of a number of bytes.
|
|
|
|
|
|
|
|
Assumes `from __future__ import division`.
|
|
|
|
|
|
|
|
>>> humanize_bytes(1)
|
2014-04-24 17:08:40 +02:00
|
|
|
'1 B'
|
|
|
|
>>> humanize_bytes(1024, precision=1)
|
2013-02-26 15:12:33 +01:00
|
|
|
'1.0 kB'
|
2014-04-24 17:08:40 +02:00
|
|
|
>>> humanize_bytes(1024 * 123, precision=1)
|
2013-02-26 15:12:33 +01:00
|
|
|
'123.0 kB'
|
2014-04-24 17:08:40 +02:00
|
|
|
>>> humanize_bytes(1024 * 12342, precision=1)
|
2013-02-26 15:12:33 +01:00
|
|
|
'12.1 MB'
|
2014-04-24 17:08:40 +02:00
|
|
|
>>> humanize_bytes(1024 * 12342, precision=2)
|
2013-02-26 15:12:33 +01:00
|
|
|
'12.05 MB'
|
2014-04-24 17:08:40 +02:00
|
|
|
>>> humanize_bytes(1024 * 1234, precision=2)
|
2013-02-26 15:12:33 +01:00
|
|
|
'1.21 MB'
|
2014-04-24 17:08:40 +02:00
|
|
|
>>> humanize_bytes(1024 * 1234 * 1111, precision=2)
|
2013-02-26 15:12:33 +01:00
|
|
|
'1.31 GB'
|
2014-04-24 17:08:40 +02:00
|
|
|
>>> humanize_bytes(1024 * 1234 * 1111, precision=1)
|
2013-02-26 15:12:33 +01:00
|
|
|
'1.3 GB'
|
|
|
|
|
|
|
|
"""
|
|
|
|
abbrevs = [
|
|
|
|
(1 << 50, 'PB'),
|
|
|
|
(1 << 40, 'TB'),
|
|
|
|
(1 << 30, 'GB'),
|
|
|
|
(1 << 20, 'MB'),
|
|
|
|
(1 << 10, 'kB'),
|
2013-04-13 02:49:27 +02:00
|
|
|
(1, 'B')
|
2013-02-26 15:12:33 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
if n == 1:
|
2013-04-15 05:56:47 +02:00
|
|
|
return '1 B'
|
2013-02-26 15:12:33 +01:00
|
|
|
|
|
|
|
for factor, suffix in abbrevs:
|
|
|
|
if n >= factor:
|
|
|
|
break
|
|
|
|
|
2014-04-27 00:07:13 +02:00
|
|
|
# noinspection PyUnboundLocalVariable
|
2013-02-26 15:12:33 +01:00
|
|
|
return '%.*f %s' % (precision, n / factor, suffix)
|