(sequence, aliases)
Translates a flat sequence of items into a set of prefixed aliases.

    This allows the value set by `.QuerySet.order_by` to be translated into
    a list of columns that would have the same result. These are called
    "order by aliases" which are optionally prefixed column names::

        >>> list(segment(('a', '-b', 'c'),
        ...              {'x': ('a'),
        ...               'y': ('b', '-c'),
        ...               'z': ('-b', 'c')}))
        [('x', '-y'), ('x', 'z')]
Translates a flat sequence of items into a set of prefixed aliases.
Translates a flat sequence of items into a set of prefixed aliases .
def segment(sequence, aliases):
    """
    Translates a flat sequence of items into a set of prefixed aliases.

    This allows the value set by `.QuerySet.order_by` to be translated into
    a list of columns that would have the same result. These are called
    "order by aliases" which are optionally prefixed column names::

        >>> list(segment(('a', '-b', 'c'),
        ...              {'x': ('a'),
        ...               'y': ('b', '-c'),
        ...               'z': ('-b', 'c')}))
        [('x', '-y'), ('x', 'z')]

    """
    if not (sequence or aliases):
        return
    for alias, parts in aliases.items():
        variants = {
            # alias: order by tuple
            alias: OrderByTuple(parts),
            OrderBy(alias).opposite: OrderByTuple(parts).opposite,
        }
        for valias, vparts in variants.items():
            if list(sequence[: len(vparts)]) == list(vparts):
                tail_aliases = dict(aliases)
                del tail_aliases[alias]
                tail_sequence = sequence[len(vparts) :]
                if tail_sequence:
                    for tail in segment(tail_sequence, tail_aliases):
                        yield tuple(chain([valias], tail))
                    else:
                        continue
                else:
                    yield tuple([valias])
def segment ( sequence , aliases ) : if not ( sequence or aliases ) : return for alias , parts in aliases . items ( ) : variants = { # alias: order by tuple alias : OrderByTuple ( parts ) , OrderBy ( alias ) . opposite : OrderByTuple ( parts ) . opposite , } for valias , vparts in variants . items ( ) : if list ( sequence [ : len ( vparts ) ] ) == list ( vparts ) : tail_aliases = dict ( aliases ) del tail_aliases [ alias ] tail_sequence = sequence [ len ( vparts ) : ] if tail_sequence : for tail in segment ( tail_sequence , tail_aliases ) : yield tuple ( chain ( [ valias ] , tail ) ) else : continue else : yield tuple ( [ valias ] )