import six


def _convert_str(data, to_binary=False):
    """Helper to convert str to corresponding string or binary type.

    `data` has `str` type (in both Python 2/3), the function converts it
    to corresponding string or binary representation depending on Python
    version and boolean `to_binary` parameter.
    """
    if to_binary and six.PY3:
        return data.encode('utf-8')
    elif not to_binary and six.PY2:
        return data.decode('utf-8')
    return data