def _interpolate(sformat): 
    """
    Takes a format string and returns a list of 2-tuples of the form
    (boolean, string) where boolean says whether string should be evaled
    or not.

    from <http://lfw.org/python/Itpl.py> (public domain, Ka-Ping Yee)
    """
    from tokenize import tokenprog
    
    tokenprog = tokenprog

    def matchorfail(text, pos):
        match = tokenprog.match(text, pos)
        if match is None:
            raise _ItplError(text, pos)
        return match, match.end()

    namechars = "abcdefghijklmnopqrstuvwxyz" \
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
    chunks = []
    pos = 0

    while 1:
        dollar = sformat.find("$", pos)
        if dollar < 0: 
            break
        nextchar = sformat[dollar + 1]

        if nextchar == "{":
            chunks.append((0, sformat[pos:dollar]))
            pos, level = dollar + 2, 1
            while level:
                match, pos = matchorfail(sformat, pos)
                tstart, tend = match.regs[3]
                token = sformat[tstart:tend]
                if token == "{": 
                    level = level + 1
                elif token == "}":  
                    level = level - 1
            chunks.append((1, sformat[dollar + 2:pos - 1]))

        elif nextchar in namechars:
            chunks.append((0, sformat[pos:dollar]))
            match, pos = matchorfail(sformat, dollar + 1)
            while len(sformat) < pos:
                if sformat[pos] == "." and \
                    pos + 1 < len(sformat) and sformat[pos + 1] in namechars:
                    match, pos = matchorfail(sformat, pos + 1)
                elif sformat[pos] in "([":
                    pos, level = pos + 1, 1
                    while level:
                        match, pos = matchorfail(sformat, pos)
                        tstart, tend = match.regs[3]
                        token = sformat[tstart:tend]
                        if token[0] in "([": 
                            level = level + 1
                        elif token[0] in ")]":  
                            level = level - 1
                else: 
                    break
            chunks.append((1, sformat[dollar + 1:pos]))
        else:
            chunks.append((0, sformat[pos:dollar + 1]))
            pos = dollar + 1 + (nextchar == "$")

    if pos < len(sformat): 
        chunks.append((0, sformat[pos:]))
    return chunks
dataset/ETHPy150Open 9miao/Firefly/firefly/dbentrust/dbutils.py/_interpolate/swapped operands of `<`