用注释注释Python print()输出

时间:2016-07-06 18:24:15

标签: python python-3.x comments stdout code-formatting

鉴于带有print()语句的Python脚本,我希望能够在每个显示每个语句输出的语句之后运行脚本并插入注释。要演示,请使用名为example.py的脚本:

a, b = 1, 2

print('a + b:', a + b)

c, d = 3, 4

print('c + d:', c + d)

所需的输出是:

a, b = 1, 2

print('a + b:', a + b)
# a + b: 3

c, d = 3, 4

print('c + d:', c + d)
# c + d: 7

这是我的尝试,适用于上述简单示例:

import sys
from io import StringIO

def intercept_stdout(func):
    "redirect stdout from a target function"
    def wrapper(*args, **kwargs):
        "wrapper function for intercepting stdout"
        # save original stdout
        original_stdout = sys.stdout

        # set up StringIO object to temporarily capture stdout
        capture_stdout = StringIO()
        sys.stdout = capture_stdout

        # execute wrapped function
        func(*args, **kwargs)

        # assign captured stdout to value
        func_output = capture_stdout.getvalue()

        # reset stdout
        sys.stdout = original_stdout

        # return captured value
        return func_output

    return wrapper


@intercept_stdout
def exec_target(name):
    "execute a target script"
    with open(name, 'r') as f:    
        exec(f.read())


def read_target(name):
    "read source code from a target script & return it as a list of lines"
    with open(name) as f:
        source = f.readlines()

    # to properly format last comment, ensure source ends in a newline
    if len(source[-1]) >= 1 and source[-1][-1] != '\n':
        source[-1] += '\n'

    return source


def annotate_source(target):
    "given a target script, return the source with comments under each print()"
    target_source = read_target(target)

    # find each line that starts with 'print(' & get indices in reverse order
    print_line_indices = [i for i, j in enumerate(target_source)
                              if len(j) > 6 and j[:6] == 'print(']
    print_line_indices.reverse()

    # execute the target script and get each line output in reverse order
    target_output = exec_target(target)
    printed_lines = target_output.split('\n')
    printed_lines.reverse()

    # iterate over the source and insert commented target output line-by-line
    annotated_source = []
    for i, line in enumerate(target_source):
        annotated_source.append(line)
        if print_line_indices and i == print_line_indices[-1]:
            annotated_source.append('# ' + printed_lines.pop() + '\n')
            print_line_indices.pop()

    # return new annotated source as a string
    return ''.join(annotated_source)


if __name__ == '__main__':
    target_script = 'example.py'
    with open('annotated_example.py', 'w') as f:
        f.write(annotate_source(target_script))

但是,对于包含print()语句跨越多行的脚本以及不在行开头的print()语句的脚本失败。在最好的情况下,它甚至可以用于函数内的print()语句。请看以下示例:

print('''print to multiple lines, first line
second line
third line''')

print('print from partial line, first part') if True else 0

1 if False else print('print from partial line, second part')

print('print from compound statement, first part'); pass

pass; print('print from compound statement, second part')

def foo():
    print('bar')

foo()

理想情况下,输出看起来像这样:

print('''print to multiple lines, first line
second line
third line''')
# print to multiple lines, first line
# second line
# third line

print('print from partial line, first part') if True else 0
# print from partial line, first part

1 if False else print('print from partial line, second part')
# print from partial line, second part

print('print from compound statement, first part'); pass
# print from compound statement, first part

pass; print('print from compound statement, second part')
# print from compound statement, second part

def foo():
    print('bar')

foo()
# bar

但是上面的剧本就像这样破坏它:

print('''print to multiple lines, first line
# print to multiple lines, first line
second line
third line''')

print('print from partial line, first part') if True else 0
# second line

1 if False else print('print from partial line, second part')

print('print from compound statement, first part'); pass
# third line

pass; print('print from compound statement, second part')

def foo():
    print('bar')

foo()

什么方法可以使这个过程更加健壮?

5 个答案:

答案 0 :(得分:6)

您是否考虑过使用inspect模块?如果您愿意说您总是希望最顶层呼叫旁边的注释,并且您正在注释的文件很简单,您可以获得合理的结果。以下是我的尝试,它会覆盖内置的打印功能并查看堆栈跟踪以确定调用打印的位置:

import inspect
import sys
from io import StringIO

file_changes = {}

def anno_print(old_print, *args, **kwargs):
    (frame, filename, line_number,
     function_name, lines, index) = inspect.getouterframes(inspect.currentframe())[-2]
    if filename not in file_changes:
        file_changes[filename] = {}
    if line_number not in file_changes[filename]:
        file_changes[filename][line_number] = []
    orig_stdout = sys.stdout
    capture_stdout = StringIO()
    sys.stdout = capture_stdout
    old_print(*args, **kwargs)
    output = capture_stdout.getvalue()
    file_changes[filename][line_number].append(output)
    sys.stdout = orig_stdout
    return

def make_annotated_file(old_source, new_source):
    changes = file_changes[old_source]
    old_source_F = open(old_source)
    new_source_F = open(new_source, 'w')
    content = old_source_F.readlines()
    for i in range(len(content)):
        line_num = i + 1
        new_source_F.write(content[i])
        if content[i][-1] != '\n':
            new_source_F.write('\n')
        if line_num in changes:
            for output in changes[line_num]:
                output = output[:-1].replace('\n', '\n#') + '\n'
                new_source_F.write("#" + output)
    new_source_F.close()



if __name__=='__main__':
    target_source = "foo.py"
    old_print = __builtins__.print
    __builtins__.print = lambda *args, **kwargs: anno_print(old_print, *args, **kwargs)
    with open(target_source) as f:
        code = compile(f.read(), target_source, 'exec')
        exec(code)
    __builtins__.print = old_print
    make_annotated_file(target_source, "foo_annotated.py")

如果我在以下文件" foo.py":

上运行它
def foo():
    print("a")
    print("b")

def cool():
    foo()
    print("c")

def doesnt_print():
    a = 2 + 3

print(1+2)
foo()
doesnt_print()
cool()

输出是" foo_annotated.py":

def foo():
    print("a")
    print("b")

def cool():
    foo()
    print("c")

def doesnt_print():
    a = 2 + 3

print(1+2)
#3
foo()
#a
#b
doesnt_print()
cool()
#a
#b
#c

答案 1 :(得分:1)

感谢来自@Lennart的反馈,我已经几乎让它正常工作......它逐行迭代,只要当前的线条就会将线条聚集成更长和更长的块加到SyntaxError时,阻止包含exec()。这是为了以防其他人使用它:

import sys
from io import StringIO

def intercept_stdout(func):
    "redirect stdout from a target function"
    def wrapper(*args, **kwargs):
        "wrapper function for intercepting stdout"
        # save original stdout
        original_stdout = sys.stdout

        # set up StringIO object to temporarily capture stdout
        capture_stdout = StringIO()
        sys.stdout = capture_stdout

        # execute wrapped function
        func(*args, **kwargs)

        # assign captured stdout to value
        func_output = capture_stdout.getvalue()

        # reset stdout
        sys.stdout = original_stdout

        # return captured value
        return func_output

    return wrapper

@intercept_stdout
def exec_line(source, block_globals):
    "execute a target block of source code and get output" 
    exec(source, block_globals)

def read_target(name):
    "read source code from a target script & return it as a list of lines"
    with open(name) as f:
        source = f.readlines()

    # to properly format last comment, ensure source ends in a newline
    if len(source[-1]) >= 1 and source[-1][-1] != '\n':
        source[-1] += '\n'

    return source

def get_blocks(target, block_globals):
    "get outputs for each block of code in source"
    outputs = []
    lines = 1

    @intercept_stdout
    def eval_blocks(start_index, end_index, full_source, block_globals):
        "work through a group of lines of source code and exec each block"
        nonlocal lines
        try:    
            exec(''.join(full_source[start_index:end_index]), block_globals)
        except SyntaxError:
            lines += 1
            eval_blocks(start_index, start_index + lines,
                        full_source, block_globals)

    for i, s in enumerate(target):
        if lines > 1:
            lines -= 1
            continue  
        outputs.append((eval_blocks(i, i+1, target, block_globals), i, lines))

    return [(i[1], i[1] + i[2]) for i in outputs]

def annotate_source(target, block_globals={}):
    "given a target script, return the source with comments under each print()"
    target_source = read_target(target)

    # get each block's start and end indices
    outputs = get_blocks(target_source, block_globals)
    code_blocks = [''.join(target_source[i[0]:i[1]]) for i in outputs]

    # iterate through each
    annotated_source = []
    for c in code_blocks:
        annotated_source.append(c)
        printed_lines = exec_line(c, block_globals).split('\n')
        if printed_lines and printed_lines[-1] == '':
            printed_lines.pop()
        for line in printed_lines:
            annotated_source.append('# ' + line + '\n')

    # return new annotated source as a string
    return ''.join(annotated_source)

def main():
    ### script to format goes here
    target_script = 'example.py'

    ### name of formatted script goes here
    new_script = 'annotated_example.py'

    new_code = annotate_source(target_script)
    with open(new_script, 'w') as f:
        f.write(new_code)

if __name__ == '__main__':
    main()

它适用于上述两个示例中的每一个。但是,在尝试执行以下操作时:

def foo():
    print('bar')
    print('baz')

foo()

而不是给我想要的输出:

def foo():
    print('bar')
    print('baz')

foo()
# bar
# baz

失败了很长的回溯:

Traceback (most recent call last):
  File "ex.py", line 55, in eval_blocks
    exec(''.join(full_source[start_index:end_index]), block_globals)
  File "<string>", line 1
    print('baz')
    ^
IndentationError: unexpected indent

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ex.py", line 55, in eval_blocks
    exec(''.join(full_source[start_index:end_index]), block_globals)
  File "<string>", line 1
    print('baz')
    ^
IndentationError: unexpected indent

During handling of the above exception, another exception occurred:

...

Traceback (most recent call last):
  File "ex.py", line 55, in eval_blocks
    exec(''.join(full_source[start_index:end_index]), block_globals)
  File "<string>", line 1
    print('baz')
    ^
IndentationError: unexpected indent

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ex.py", line 102, in <module>
    main()
  File "ex.py", line 97, in main
    new_code = annotate_source(target_script)
  File "ex.py", line 74, in annotate_source
    outputs = get_blocks(target_source, block_globals)
  File "ex.py", line 65, in get_blocks
    outputs.append((eval_blocks(i, i+1, target, block_globals), i, lines))
  File "ex.py", line 16, in wrapper
    func(*args, **kwargs)
  File "ex.py", line 59, in eval_blocks
    full_source, block_globals)
  File "ex.py", line 16, in wrapper
    func(*args, **kwargs)   

...

  File "ex.py", line 16, in wrapper
    func(*args, **kwargs)
  File "ex.py", line 55, in eval_blocks
    exec(''.join(full_source[start_index:end_index]), block_globals)
RecursionError: maximum recursion depth exceeded while calling a Python object

由于def foo(): print('bar')是有效代码而发生这种情况,因此print('baz')未包含在该函数中,导致其失败并显示IndentationError。关于如何避免这个问题的任何想法?我怀疑它可能需要潜入ast,如上所述,但是会喜欢进一步的输入或用法示例。

答案 2 :(得分:1)

通过使用现有的python解析器从代码中提取顶级语句,可以使它变得更容易。例如,标准库中的ast模块。但是,ast会丢失评论等信息。

使用源代码转换(您正在做的)构建的库可能更适合这里。 redbaron就是一个很好的例子。

要将全局变量带到下一个exec(),您必须使用第二个参数(documentation):

environment = {}
for statement in statements:
    exec(statement, environment)

答案 3 :(得分:1)

看起来except SyntaxError不足以检查完整函数,因为它将完成块的第一行,它不会产生语法错误。你想要的是确保整个功能包含在同一个块中。要做到这一点:

  • 检查当前块是否为函数。检查第一行是否以def开头。

  • 检查full_source中的下一行是否以更高或相等的空格数作为函数的第二行(定义缩进的行)开始。这意味着eval_blocks将检查代码的下一行是否具有更大或相等的间距,因此在函数内部。

get_blocks的代码可能如下所示:

# function for finding num of spaces at beginning (could be in global spectrum)
def get_front_whitespace(string):
    spaces = 0
    for char in string:
        # end loop at end of spaces
        if char not in ('\t', ' '): 
            break
        # a tab is equal to 8 spaces
        elif char == '\t':
            spaces += 8
        # otherwise must be a space
        else:
            spaces += 1
    return spaces

...

def get_blocks(target, block_globals):
    "get outputs for each block of code in source"
    outputs = []
    lines = 1
    # variable to check if current block is a function
    block_is_func = False

    @intercept_stdout
    def eval_blocks(start_index, end_index, full_source, block_globals):
        "work through a group of lines of source code and exec each block"
        nonlocal lines
        nonlocal block_is_func
        # check if block is a function
        block_is_func = ( full_source[start_index][:3] == 'def' )
        try:    
            exec(''.join(full_source[start_index:end_index]), block_globals)
        except SyntaxError:
            lines += 1
            eval_blocks(start_index, start_index + lines,
                        full_source, block_globals)
        else:
            # if the block is a function, check for indents
            if block_is_func:
                # get number of spaces in first indent of function
                func_indent= get_front_whitespace( full_source[start_index + 1] )
                # get number of spaces in the next index 
                next_index_spaces = get_front_whitespace( full_source[end_index + 1] )
                # if the next line is equally or more indented than the function indent, continue to next recursion layer
                if func_indent >= next_index_spaces:
                    lines += 1
                    eval_blocks(start_index, start_index + lines,
                               full_source, block_globals)

    for i, s in enumerate(target):
        # reset the function variable for next block
        if block_is_func: block_is_func = False
        if lines > 1:
            lines -= 1
            continue  
        outputs.append((eval_blocks(i, i+1, target, block_globals), i, lines))

    return [(i[1], i[1] + i[2]) for i in outputs]

如果函数的最后一行是文件的末尾,这可能会产生索引错误,因为end_index_spaces = get_front_whitespace( full_source[end_index + 1] )处的正向索引

这也可以用于选择语句和循环,这可能有同样的问题:只需检查if开头的for whilestart_index行以及def。这将导致注释位于缩进区域之后,但由于缩进区域内的打印输出依赖于用于调用它们的变量,我认为在任何情况下都必须在缩进之外输出。

答案 4 :(得分:0)

尝试https://github.com/eevleevs/hashequal/

我这样做是为了替代Mathcad。不对打印语句起作用,但对#=注释起作用,例如:

a = 1 + 1#=

成为

a = 1 +1#= 2