def collapse_braces(src_text):
 nesting = 0
 start_index = 0
 collapsed_src_text = u''
 for (index, char) in enumerate(src_text):
  if (nesting == 0):
   collapsed_src_text += char
  if (char == u'{'):
   if (nesting == 0):
    start_index = (index + 1)
   nesting += 1
  if (char == u'}'):
   if (nesting > 0):
    nesting -= 1
   if (nesting == 0):
    collapsed_src_text += char
 if (nesting != 0):
  collapsed_src_text += src_text[start_index:]
 return collapsed_src_text

def collapse_braces ( src_text ) : nesting = 0 start_index = 0 collapsed_src_text = u'' for ( index , char ) in enumerate ( src_text ) : if ( nesting == 0 ) : collapsed_src_text += char if ( char == u'{' ) : if ( nesting == 0 ) : start_index = ( index + 1 ) nesting += 1 if ( char == u'}' ) : if ( nesting > 0 ) : nesting -= 1 if ( nesting == 0 ) : collapsed_src_text += char if ( nesting != 0 ) : collapsed_src_text += src_text [ start_index : ] return collapsed_src_text
removes the contents of all top-level curly brace pairs {} .