def humansize(num, power=1024, sep=' ', precision=2, unit=None):
    """Converts raw bytes to a human friendly format."""
    if power == 1024:
        units = ['B  ', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
    else:
        units = ['B ', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    if unit in units:
        if power == 1024:
            num = num >> units.index(unit) * 10
        else:
            div = power ** units.index(unit)
            num /= float(div)
        return f'{num:.{precision}f}{sep}{unit}'
    for unit in units[:-1]:
        if abs(round(num, precision)) < power:
            return f'{num:.{precision}f}{sep}{unit}'
        num /= float(power)
    return f'{num:.{precision}f}{sep}{units[-1]}'
