def str_join_safe(delim, str_vec, append=False):
    """Version of `str.join` that is guaranteed to be invertible.

    Parameters
    ----------
    delim : str
        Delimiter to join the strings.
    str_vec : list(str)
        List of strings to join. A `ValueError` is raised if `delim` is present in any of these strings.
    append : bool
        If true, assume the first element is already joined and we are appending to it. So, `str_vec[0]` can contain
        `delim`.

    Returns
    -------
    joined_str : str
        Joined version of `str_vec`, which is always recoverable with ``joined_str.split(delim)``.

    Examples
    --------
    Append is required because,

    .. code-block:: pycon

        ss = str_join_safe('_', ('foo', 'bar'))
        str_join_safe('_', (ss, 'baz', 'qux'))

    would fail because we are appending ``'baz'`` and ``'qux'`` to the already joined string ``ss = 'foo_bar'``.

    In this case, we use

    .. code-block:: pycon

        ss = str_join_safe('_', ('foo', 'bar'))
        str_join_safe('_', (ss, 'baz', 'qux'), append=True)
    """
    chk_vec = str_vec[1:] if append else str_vec

    for ss in chk_vec:
        if delim in ss:
            raise ValueError("%s cannot contain delimeter %s" % (ss, delim))

    joined_str = delim.join(str_vec)
    return joined_str