def segment(sequence, aliases):
 if (not (sequence or aliases)):
  return
 for (alias, parts) in aliases.items():
  variants = {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 : 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 ] ) )
translates a flat sequence of items into a set of prefixed aliases .