def bytes2human(n, format='%(value).1f %(symbol)sB'):
    '\n    Convert n bytes into a human readable string based on format.\n    symbols can be either "customary", "customary_ext", "iec" or "iec_ext",\n    see: http://goo.gl/kTQMs\n\n      >>> from datalad.utils import bytes2human\n      >>> bytes2human(1)\n      \'1.0 B\'\n      >>> bytes2human(1024)\n      \'1.0 KB\'\n      >>> bytes2human(1048576)\n      \'1.0 MB\'\n      >>> bytes2human(1099511627776127398123789121)\n      \'909.5 YB\'\n\n      >>> bytes2human(10000, "%(value).1f %(symbol)s/sec")\n      \'9.8 K/sec\'\n\n      >>> # precision can be adjusted by playing with %f operator\n      >>> bytes2human(10000, format="%(value).5f %(symbol)s")\n      \'9.76562 K\'\n\n    Taken from: http://goo.gl/kTQMs and subsequently simplified\n    Original Author: Giampaolo Rodola\' <g.rodola [AT] gmail [DOT] com>\n    License: MIT\n    '
    n = int(n)
    if (n < 0):
        raise ValueError('n < 0')
    symbols = ('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
    prefix = {}
    for (i, s) in enumerate(symbols[1:]):
        prefix[s] = (1 << ((i + 1) * 10))
    for symbol in reversed(symbols[1:]):
        if (n >= prefix[symbol]):
            value = (float(n) / prefix[symbol])
            return (format % locals())
    return (format % dict(symbol=symbols[0], value=n))
Convert n bytes into a human readable string based on format.
symbols can be either "customary", "customary_ext", "iec" or "iec_ext",
see: http://goo.gl/kTQMs

  >>> from datalad.utils import bytes2human
  >>> bytes2human(1)
  '1.0 B'
  >>> bytes2human(1024)
  '1.0 KB'
  >>> bytes2human(1048576)
  '1.0 MB'
  >>> bytes2human(1099511627776127398123789121)
  '909.5 YB'

  >>> bytes2human(10000, "%(value).1f %(symbol)s/sec")
  '9.8 K/sec'

  >>> # precision can be adjusted by playing with %f operator
  >>> bytes2human(10000, format="%(value).5f %(symbol)s")
  '9.76562 K'

Taken from: http://goo.gl/kTQMs and subsequently simplified
Original Author: Giampaolo Rodola' <g.rodola [AT] gmail [DOT] com>
License: MIT
def bytes2human(n, format='%(value).1f %(symbol)sB'):
    '\n    Convert n bytes into a human readable string based on format.\n    symbols can be either "customary", "customary_ext", "iec" or "iec_ext",\n    see: http://goo.gl/kTQMs\n\n      >>> from datalad.utils import bytes2human\n      >>> bytes2human(1)\n      \'1.0 B\'\n      >>> bytes2human(1024)\n      \'1.0 KB\'\n      >>> bytes2human(1048576)\n      \'1.0 MB\'\n      >>> bytes2human(1099511627776127398123789121)\n      \'909.5 YB\'\n\n      >>> bytes2human(10000, "%(value).1f %(symbol)s/sec")\n      \'9.8 K/sec\'\n\n      >>> # precision can be adjusted by playing with %f operator\n      >>> bytes2human(10000, format="%(value).5f %(symbol)s")\n      \'9.76562 K\'\n\n    Taken from: http://goo.gl/kTQMs and subsequently simplified\n    Original Author: Giampaolo Rodola\' <g.rodola [AT] gmail [DOT] com>\n    License: MIT\n    '
    n = int(n)
    if (n < 0):
        raise ValueError('n < 0')
    symbols = (, 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
    prefix = {}
    for (i, s) in enumerate(symbols[1:]):
        prefix[s] = (1 << ((i + 1) * 10))
    for symbol in reversed(symbols[1:]):
        if (n >= prefix[symbol]):
            value = (float(n) / prefix[symbol])
            return (format % locals())
    return (format % dict(symbol=symbols[0], value=n))