def open_to_close(st, startpos):
    """ 
        Finds end of a substring enclosed by braces starting at startpos.
        Used by get_substring.
    """
    pos = startpos
    openchar = st[pos]
    if openchar == '{':
        closechar = '}'
    count = 1
    while count > 0:
        pos += 1
        if st[pos] == openchar:
            count += 1
        if st[pos] == closechar:
            count -= 1
    return pos
