def bytes2human(n, fmt='%(value).1f %(symbol)s'):
    """Convert from a size in bytes to a human readable string.

    Examples
    --------

    .. testcode::

       from nengo.utils.cache import bytes2human

       print(bytes2human(10000))
       print(bytes2human(100001221))

    .. testoutput::

       9.8 KB
       95.4 MB

    """
    symbols = 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'
    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 fmt % {'value': value, 'symbol': symbol}
    return fmt % {'value': n, 'symbol': symbols[0]}
