def cast_bytes(data, encoding='utf8'):
    """
    Cast str, int, float to bytes.
    """
    if isinstance(data, str) is True:
        return data.encode(encoding)

    elif isinstance(data, int) is True:
        return str(data).encode(encoding)

    elif isinstance(data, float) is True:
        return str(data).encode(encoding)

    elif isinstance(data, bytes) is True:
        return data

    elif data is None:
        return None

    else:
        raise TypeError("Expected unicode or bytes, got %r" % data)