PySnooper Python 打印函数执行流程模块

2019-10-22  本文已影响0人  河码匠

PySnooper 可以打印出 Python 执行代码的全流程和每行代码的执行时间,以及执行过程中变量的变化。

安装 PySnooper

$ pip install pysnooper

简单示例

import pysnooper

@pysnooper.snoop()
def text(number):
    new_list = []
    for i in range(number):
        new_list.append(i * 2)
    return new_list
text(6)

效果

Source path:... text.py
Starting var:.. number = 6
10:45:31.237191 call         5 def text(number):
10:45:31.237644 line         6     new_list = []
New var:....... new_list = []
10:45:31.237732 line         7     for i in range(number):
New var:....... i = 0
10:45:31.237849 line         8         new_list.append(i * 2)
Modified var:.. new_list = [0]
10:45:31.237941 line         7     for i in range(number):
Modified var:.. i = 1
10:45:31.238024 line         8         new_list.append(i * 2)
Modified var:.. new_list = [0, 2]
10:45:31.238088 line         7     for i in range(number):
Modified var:.. i = 2
10:45:31.238157 line         8         new_list.append(i * 2)
Modified var:.. new_list = [0, 2, 4]
10:45:31.238229 line         7     for i in range(number):
Modified var:.. i = 3
10:45:31.238303 line         8         new_list.append(i * 2)
Modified var:.. new_list = [0, 2, 4, 6]
10:45:31.238364 line         7     for i in range(number):
Modified var:.. i = 4
10:45:31.238432 line         8         new_list.append(i * 2)
Modified var:.. new_list = [0, 2, 4, 6, 8]
10:45:31.238500 line         7     for i in range(number):
Modified var:.. i = 5
10:45:31.238570 line         8         new_list.append(i * 2)
Modified var:.. new_list = [0, 2, 4, 6, 8, 10]
10:45:31.238640 line         7     for i in range(number):
10:45:31.238713 line         9     return new_list
10:45:31.238767 return       9     return new_list
Return value:.. [0, 2, 4, 6, 8, 10]

可以很明显看到每次循环的操作和变量的变化。

其他操作

@pysnooper.snoop('/my/log/file.log')
@pysnooper.snoop(depth=2)

示例

import pysnooper

class Text():
    @pysnooper.snoop(depth=2)
    def a(self, param):
        return self.b(param)

    def b(self, param):
        new_list = []
        for i in range(param):
            new_list.append(i * 2)

Text().a(2)
@pysnooper.snoop(prefix='aaa ')

示例

import pysnooper

class Text():
    @pysnooper.snoop(prefix='aaa ')
    @pysnooper.snoop(depth=2)
    def a(self, param):
        return self.b(param)

    def b(self, param):
        new_list = []
        for i in range(param):
            new_list.append(i * 2)

Text().a(2)

结果
\color{red}{注意}:这里的 pysnooper.snoop 顺序对结果有影响。

aaa Source path:... /Users/machao/Documents/PythonVirtual/SpeedyCloud/lib/python2.7/site-packages/pysnooper/tracer.py
aaa Starting var:.. args = (<__main__.Text instance at 0x101da2bd8>, 2)
aaa Starting var:.. kwargs = {}
aaa Starting var:.. function = <function a at 0x101da96e0>
aaa Starting var:.. self = <pysnooper.tracer.Tracer instance at 0x101da2b48>
aaa 11:15:53.951363 call       254         def simple_wrapper(*args, **kwargs):
aaa 11:15:53.954333 line       255             with self:
aaa 11:15:53.954468 line       256                 return function(*args, **kwargs)
    Source path:... /Users/machao/Desktop/licens.py
    Starting var:.. self = <__main__.Text instance at 0x101da2bd8>
    Starting var:.. param = 2
    11:15:53.954581 call         6     def a(self, param):
    11:15:53.954993 line         7         return self.b(param)
        Starting var:.. self = <__main__.Text instance at 0x101da2bd8>
        Starting var:.. param = 2
        11:15:53.955131 call         9     def b(self, param):
        11:15:53.955316 line        10         new_list = []
        New var:....... new_list = []
        11:15:53.955506 line        11         for i in range(param):
        New var:....... i = 0
        11:15:53.955675 line        12             new_list.append(i * 2)
        Modified var:.. new_list = [0]
        11:15:53.955818 line        11         for i in range(param):
        Modified var:.. i = 1
        11:15:53.956045 line        12             new_list.append(i * 2)
        Modified var:.. new_list = [0, 2]
        11:15:53.956168 line        11         for i in range(param):
        11:15:53.956281 return      11         for i in range(param):
        Return value:.. None
    11:15:53.956454 return       7         return self.b(param)
    Return value:.. None
aaa Call ended by exception
@pysnooper.snoop(thread_info=True)
@pysnooper.snoop(max_variable_length=200)
上一篇下一篇

猜你喜欢

热点阅读