def find_top_level_bracket_locations(string_toparse):
        """Get the locations of top-level brackets in a string."""
        bracket_stack = []
        replace_args_list = []
        bracket_type = None
        literal_type = ''
        brackets = {'(': ')', '[': ']', '{': '}'}
        for idx, character in enumerate(string_toparse):
            if (not bracket_stack and character in brackets.keys()
                    or character == bracket_type):
                bracket_stack.append(idx)
                bracket_type = character
            elif bracket_type and character == brackets[bracket_type]:
                begin_idx = bracket_stack.pop()
                if not bracket_stack:
                    if not literal_type:
                        if bracket_type == '(':
                            literal_type = '(None)'
                        elif bracket_type == '[':
                            literal_type = '[list]'
                        elif bracket_type == '{':
                            if idx - begin_idx <= 1:
                                literal_type = '{dict}'
                            else:
                                literal_type = '{set}'
                    replace_args_list.append(
                        (string_toparse[begin_idx:idx + 1],
                         literal_type, 1))
                    bracket_type = None
                    literal_type = ''
            elif len(bracket_stack) == 1:
                if bracket_type == '(' and character == ',':
                    literal_type = '(tuple)'
                elif bracket_type == '{' and character == ':':
                    literal_type = '{dict}'
                elif bracket_type == '(' and character == ':':
                    literal_type = '[slice]'

        if bracket_stack:
            raise IndexError('Bracket mismatch')
        for replace_args in replace_args_list:
            string_toparse = string_toparse.replace(*replace_args)
        return string_toparse
def find_top_level_bracket_locations ( string_toparse ) : bracket_stack = [ ] replace_args_list = [ ] bracket_type = None literal_type = '' brackets = { '(' : ')' , '[' : ']' , '{' : '}' } for idx , character in enumerate ( string_toparse ) : if ( not bracket_stack and character in brackets . keys ( ) or character == bracket_type ) : bracket_stack . append ( idx ) bracket_type = character elif bracket_type and character == brackets [ bracket_type ] : begin_idx = bracket_stack . pop ( ) if not bracket_stack : if not literal_type : if bracket_type == '(' : literal_type = '(None)' elif bracket_type == '[' : literal_type = '[list]' elif bracket_type == '{' : if idx - begin_idx <= 1 : literal_type = '{dict}' else : literal_type = '{set}' replace_args_list . append ( ( string_toparse [ begin_idx : idx + 1 ] , literal_type , 1 ) ) bracket_type = None literal_type = '' elif len ( bracket_stack ) == 1 : if bracket_type == '(' and character == ',' : literal_type = '(tuple)' elif bracket_type == '{' and character == ':' : literal_type = '{dict}' elif bracket_type == '(' and character == ':' : literal_type = '[slice]' if bracket_stack : raise IndexError ( 'Bracket mismatch' ) for replace_args in replace_args_list : string_toparse = string_toparse . replace ( * replace_args ) return string_toparse
Get the locations of top-level brackets in a string.