Python 我可以让跟踪模块将genexp计算为每次调用运行一次吗?

Python 我可以让跟踪模块将genexp计算为每次调用运行一次吗?,python,code-coverage,Python,Code Coverage,如何将python-m trace--count sample.py的输出更改为这个 输入:sample.py a = (i for i in (1,2,3)) list(a) xxx = "This first line of code is needed for working my trace.py because of my poor knowledge" def c(): return [i for i in (1,2,3)] def d(): for i in

如何将
python-m trace--count sample.py的输出更改为这个

输入:
sample.py

a = (i for i in (1,2,3))
list(a)
xxx = "This first line of code is needed for working my trace.py because of my poor knowledge"

def c():
    return [i for i in (1,2,3)]

def d():
    for i in (1,2,3):
        pass

a = (i for i in (1,2,3))
b = iter([i for i in (1,2,3)])

list(a)
list(b)

for i in (1,2,3,4,5):
    c()

d()
当前输出:
sample.cover

    5: a = (i for i in (1,2,3))
    1: list(a)
       xxx = "This first line of code is needed for working my trace.py because of my poor knowledge"

    1: def c():
    5:     return [i for i in (1,2,3)]

    1: def d():
    4:     for i in (1,2,3):
    3:         pass

    1: a = (i for i in (1,2,3))
    1: b = iter([i for i in (1,2,3)])

    1: list(a)
    1: list(b)

    6: for i in (1,2,3,4,5):
    5:     c()

    1: d()
我想如下所示更改此输出,这样它只显示行的运行次数,而不将循环的计算作为额外迭代计算:

    1: a = (i for i in (1,2,3))
    1: list(a)
可能吗? 我是否需要修改
跟踪
模块? 如果是,在
跟踪
模块中的何处修改?

警告: 我根本不知道
跟踪
模块的机制。 而且根本没有经过测试 所以请不要相信这个代码

我修改了
跟踪
模块。 我不知道为什么,但它似乎工作,除了第一条毫无意义的线需要

输入:
sample.py

a = (i for i in (1,2,3))
list(a)
xxx = "This first line of code is needed for working my trace.py because of my poor knowledge"

def c():
    return [i for i in (1,2,3)]

def d():
    for i in (1,2,3):
        pass

a = (i for i in (1,2,3))
b = iter([i for i in (1,2,3)])

list(a)
list(b)

for i in (1,2,3,4,5):
    c()

d()
输出:
sample.cover

    5: a = (i for i in (1,2,3))
    1: list(a)
       xxx = "This first line of code is needed for working my trace.py because of my poor knowledge"

    1: def c():
    5:     return [i for i in (1,2,3)]

    1: def d():
    4:     for i in (1,2,3):
    3:         pass

    1: a = (i for i in (1,2,3))
    1: b = iter([i for i in (1,2,3)])

    1: list(a)
    1: list(b)

    6: for i in (1,2,3,4,5):
    5:     c()

    1: d()

这是您的完整代码吗?@user1767754:是的。接下来,请尝试写一个问题标题,该标题实际上描述了您的单个问题,而无需阅读文本来了解“像这样”是什么。(我在这里做了适当的编辑)。@charlesduff:对不起,谢谢。标题现在是我应该写的。只是出于好奇,你为什么需要它?我的意思是每行都会运行一次,对吗?我建议使用
key
而不是
lineno
——据我所知,如果被调用方与调用方在同一行,则在处的更改将忽略从一个模块中的函数到另一个模块中的函数的调用。不过,除此之外,这看起来是一个很好的解决办法。