def StripWhitespace(stream):
 last_type = None
 has_space = False
 ignore_group = frozenset((Comparison, Punctuation))
 for (token_type, value) in stream:
  if last_type:
   if (token_type in Whitespace):
    has_space = True
    continue
  elif (token_type in (Whitespace, Whitespace.Newline, ignore_group)):
   continue
  if has_space:
   if (not ignore_group.intersection((last_type, token_type))):
    (yield (Whitespace, ' '))
   has_space = False
  (yield (token_type, value))
  last_type = token_type

def StripWhitespace ( stream ) : last_type = None has_space = False ignore_group = frozenset ( ( Comparison , Punctuation ) ) for ( token_type , value ) in stream : if last_type : if ( token_type in Whitespace ) : has_space = True continue elif ( token_type in ( Whitespace , Whitespace . Newline , ignore_group ) ) : continue if has_space : if ( not ignore_group . intersection ( ( last_type , token_type ) ) ) : ( yield ( Whitespace , ' ' ) ) has_space = False ( yield ( token_type , value ) ) last_type = token_type
strip the useless whitespaces from a stream leaving only the minimal ones .