def insert_separator(s, sep, group_size, reverse=False):
    """Insert separators in a string.

    >>> insert_separator('008041aefd7e', ':', 2)
    00:80:41:ae:fd:7e
    >>> insert_separator('aaabbbcccd', ':', 3)
    'aaa:bbb:ccc:d'
    >>> insert_separator('aaabbbcccd', ':', 3, True)
    'a:aab:bbc:ccd'
    >>> insert_separator('9783161484100', '-', (3, 1, 2, 6))
    '978-3-16-148410-0'

    :param str s: the string
    :param str sep: the separator character(s)
    :param group_size: the size of each group
    :type group_size: int or sequence of ints
    :param bool reverse: if ``True`` group from right to left instead from
                         left to right
    :return: string with separators
    :rtype: str
    :raises ValueError: if ``group_size < 1``

    .. versionadded:: 0.5.0
    .. versionchanged:: 0.6.0
       Add parameter ``reverse``
    .. versionchanged:: 0.9.0
       Groups can be of different sizes
    """
    if reverse:
        s = s[::-1]
    if isinstance(group_size, int):
        if group_size < 1:
            raise ValueError('group_size must be >= 1')
        r = [s[i:i + group_size] for i in range(0, len(s), group_size)]
    else:
        if not all(map(lambda x: x > 0, group_size)):
            raise ValueError('group_size must be >= 1')
        r = []
        idx = 0
        for size in group_size:
            x = s[idx:idx + size]
            if x:
                r.append(x)
            else:
                break
            idx += size
        else:
            if idx < len(s):
                r.append(s[idx:])
    if reverse:
        return sep.join(r)[::-1]
    else:
        return sep.join(r)