def cast_bytes ( s , encoding = None ) : if not isinstance ( s , bytes ) : return encode ( s , encoding ) return s
python bytes auto detect
Source : https : // github . com / ipython / ipython_genutils
def cast_bytes(s, encoding=None):
    """Source: https://github.com/ipython/ipython_genutils"""
    if not isinstance(s, bytes):
        return encode(s, encoding)
    return s