def collapseNestedLists(items):
 pieces = []
 for i in items:
  if (i is None):
   pieces.extend([' ', 'NIL'])
  elif isinstance(i, (DontQuoteMe, int, long)):
   pieces.extend([' ', str(i)])
  elif isinstance(i, types.StringTypes):
   if _needsLiteral(i):
    pieces.extend([' ', '{', str(len(i)), '}', IMAP4Server.delimiter, i])
   else:
    pieces.extend([' ', _quote(i)])
  elif hasattr(i, 'read'):
   d = i.read()
   pieces.extend([' ', '{', str(len(d)), '}', IMAP4Server.delimiter, d])
  else:
   pieces.extend([' ', ('(%s)' % (collapseNestedLists(i),))])
 return ''.join(pieces[1:])

def collapseNestedLists ( items ) : pieces = [ ] for i in items : if ( i is None ) : pieces . extend ( [ ' ' , 'NIL' ] ) elif isinstance ( i , ( DontQuoteMe , int , long ) ) : pieces . extend ( [ ' ' , str ( i ) ] ) elif isinstance ( i , types . StringTypes ) : if _needsLiteral ( i ) : pieces . extend ( [ ' ' , '{' , str ( len ( i ) ) , '}' , IMAP4Server . delimiter , i ] ) else : pieces . extend ( [ ' ' , _quote ( i ) ] ) elif hasattr ( i , 'read' ) : d = i . read ( ) pieces . extend ( [ ' ' , '{' , str ( len ( d ) ) , '}' , IMAP4Server . delimiter , d ] ) else : pieces . extend ( [ ' ' , ( '(%s)' % ( collapseNestedLists ( i ) , ) ) ] ) return '' . join ( pieces [ 1 : ] )
turn a nested list structure into an s-exp-like string .