#NEWLINE# def _canonicalize( tokens ) : #NEWLINE# #INDENT# 'Collapse redundant steps from token lists containing UP ops.' #NEWLINE# canonical = [ ] #NEWLINE# for token in tokens : #NEWLINE# #INDENT# if ( ( token [ 0 ] is not UP ) or ( not canonical ) ) : #NEWLINE# #INDENT# canonical . append ( token ) #NEWLINE# continue #NEWLINE# #UNINDENT# last = canonical [ ( - 1 ) ] [ 0 ] #NEWLINE# if ( last is TOP ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( last is not UP ) : #NEWLINE# #INDENT# canonical . pop ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# canonical . append ( token ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return canonical