def format_bytes(n):
    """ Format bytes as text

    >>> format_bytes(1)
    '1 B'
    >>> format_bytes(1234)
    '1.23 kB'
    >>> format_bytes(12345678)
    '12.35 MB'
    >>> format_bytes(1234567890)
    '1.23 GB'
    >>> format_bytes(1234567890000)
    '1.23 TB'
    >>> format_bytes(1234567890000000)
    '1.23 PB'

    (taken from dask.distributed, where it is not exported)
    """
    if n > 1000000000000000.0:
        return '%0.2f PB' % (n / 1000000000000000.0)
    if n > 1000000000000.0:
        return '%0.2f TB' % (n / 1000000000000.0)
    if n > 1000000000.0:
        return '%0.2f GB' % (n / 1000000000.0)
    if n > 1000000.0:
        return '%0.2f MB' % (n / 1000000.0)
    if n > 1000.0:
        return '%0.2f kB' % (n / 1000)
    return '%d B' % n
