Skip to content

Instantly share code, notes, and snippets.

@joshland
Last active January 16, 2017 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshland/87b07ea63a250491561dde5b887a516d to your computer and use it in GitHub Desktop.
Save joshland/87b07ea63a250491561dde5b887a516d to your computer and use it in GitHub Desktop.
import collections
def humanize_bibytes(n, prefixes=collections.OrderedDict((
(0, 'B'),
(1024, 'KiB'),
(1048576, 'MiB'),
(1073741824, 'GiB'),
(1099511627776, 'TiB'),
(1125899906842624, 'PiB'),
(1152921504606846976, 'EiB'),
(1180591620717411303424, 'ZiB'),
(1208925819614629174706176, 'YiB')))):
''' Convert big numbers into easily-human-readable ones.
'''
for value, prefix in reversed(prefixes.items()):
if n >= value:
return '{:.2f} {}'.format(float(n) / value, prefix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment