tokenize / untokenize python字符串代码,以便它与交互模式兼容

时间:2014-01-17 20:53:30

标签: python tokenize interactive-mode

我有像这样的python代码具有这种结构

def main:

    ''' comment '''
    if True:
        print "do"
    print "done

此代码与交互模式不兼容(例如,如果我在交互式会话中复制/粘贴它)。为此,它需要是:

def main:
    ''' comment '''
    if True:
        print "do"

    print "done"

否则交互模式会打破缩进问题。

您是否知道使用generate_token / untokenize链转换代码的简单方法?我在NL / NEWLINE / INDENT / DEDENT语义中有点迷失。

我发现此Script to remove Python comments/docstrings删除了评论/文档字符串。它看起来非常适合我的问题,但它不能解决它在复杂代码上的清晰输出。

1 个答案:

答案 0 :(得分:0)

我能想到的最好的(解决了我的问题)

def _python_interactive_indent(self, code):
    prev_toktype = tokenize.INDENT
    first_line = None
    last_lineno = -1
    last_col = 0

    output = ''

    tokgen = tokenize.generate_tokens(StringIO.StringIO(code).readline)
    indent = 0
    hasNL = False
    prefixed = False
    for toktype, ttext, (slineno, scol), (elineno, ecol), ltext in tokgen:
        done = False
        if toktype == tokenize.INDENT:
            indent = indent + 1
        if toktype == tokenize.DEDENT:
            indent = indent - 1
        if slineno > last_lineno:
            last_col = 0
        if not done and toktype == tokenize.NL:
            hasNL = True
            done = True
        if not done and toktype == tokenize.COMMENT:
            done = True
        if not done and toktype == tokenize.STRING and prev_toktype == tokenize.INDENT:
            done = True
        if not done and hasNL and toktype != tokenize.DEDENT and toktype != tokenize.INDENT:
            hasNL = False
            output = output + ("    " * indent) + '\n'
            output += "    " * indent
            prefixed = True
        if not done:
            if not prefixed and scol > last_col:
                output += (" " * (scol - last_col))
            output += (ttext)
        prefixed = False
        prev_toktype = toktype
        last_col = ecol
        last_lineno = elineno
    return output