简体   繁体   English

在python文件中搜索所有if条件并在下一行添加打印语句

[英]Search for all the if conditions in a python file and adding a print statement in the next line

I have to edit a python file such that after every if condition, i need to add a line which says我必须编辑一个 python 文件,以便在每个 if 条件之后,我需要添加一行

if condition_check:
    if self.debug == 1: print "COVERAGE CONDITION #8.3 True (condition_check)"
    #some other code
else:
    if self.debug == 1: print "COVERAGE CONDITION #8.4 False (condition_check)"
    #some other code

The number 8.4(generally yx) refer to the fact that this if condition is in function number 8(y) (the functions are just sequentially numbers, nothing special about 8) and x is xth if condition in yth function.数字 8.4(通常是 yx)指的是这个 if 条件在函数编号 8(y) 中(这些函数只是顺序数字,8 没有什么特别之处)并且 x 是 yth 函数中的第 x 个 if 条件。

and of course, the line that will be added will have to be added with proper indentation.当然,将添加的行必须以适当的缩进添加。 The condition_check is the condition being checked. condition_check 是被检查的条件。

For example:例如:

if (self.order_in_cb):
         self.ccu_process_crossing_buffer_order()

becomes:变成:

if (self.order_in_cb):
         if self.debug == 1: print "COVERAGE CONDITION #8.2 TRUE (self.order_in_cb)"
         self.ccu_process_crossing_buffer_order()

How do i achieve this?我如何实现这一目标?

EXTRA BACKGROUND: I have about 1200 lines of python code with about 180 if conditions - i need to see if every if condition is hit during the execution of 47 test cases.额外背景:我有大约 1200 行 Python 代码,其中大约有 180 个 if 条件 - 我需要查看在 47 个测试用例的执行过程中是否每个 if 条件都被满足。 In other words i need to do code coverage.换句话说,我需要进行代码覆盖。 The complication is - i am working with cocotb stimulus for RTL verification.复杂的是 - 我正在使用 cocotb 刺激进行 RTL 验证。 As a result, there is no direct way to drive the stimulus, so i dont see an easy way to use the standard coverage.py way to test coverage.结果,没有直接的方法来驱动刺激,所以我没有看到使用标准coverage.py 方法来测试覆盖率的简单方法。 Is there a way to check the coverage so other way?有没有办法以其他方式检查覆盖范围? I feel i am missing something.我觉得我错过了一些东西。

If you truly can't use coverage.py, then I would write a helper function that used inspect.stack to find the caller, then linecache to read the line of source, and log that way.如果你真的不能使用coverage.py,那么我会写一个帮助函数,它使用inspect.stack来查找调用者,然后使用linecache来读取源代码行,并以这种方式记录。 Then you only have to change if something: to if condition(something): throughout your file, which should be fairly easy.然后你只需要改变if something:if condition(something):整个文件,这应该很容易。

Here's a proof of concept:这是一个概念证明:

import inspect
import linecache
import re

debug = True

def condition(label, cond):
    if debug:
        caller = inspect.stack()[1]
        line = linecache.getline(caller.filename, caller.lineno)
        condcode = re.search(r"if condition\(.*?,(.*)\):", line).group(1)
        print("CONDITION {}: {}".format(label, condcode))
    return cond


x = 1
y = 1
if condition(1.1, x + y == 2):
    print("it's two!")

This prints:这打印:

CONDITION 1.1:  x + y == 2
it's two!

I have about 1200 lines of python code with about 180 if conditions - i need to see if every if condition is hit during the execution of 47 test cases.我有大约 1200 行 python 代码,其中包含大约 180 个 if 条件——我需要查看在 47 个测试用例的执行过程中是否每个 if 条件都被满足。 In other words i need to do code coverage.换句话说,我需要进行代码覆盖。 The complication is - i am working with cocotb stimulus for RTL verification.复杂的是 - 我正在使用 cocotb 刺激进行 RTL 验证。

Cocotb has support for coverage built in ( docs ) Cocotb 支持内置的覆盖范围文档

export COVERAGE=1
# run cocotb however you currently invoke it

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM