跳转至

PySnooper库

1. 介绍

 PySnooper 在 GitHub 上自嘲是一个“乞丐版”调试工具(poor man's debugger)。

 一般情况下,在编写 Python 代码时,如果想弄清楚为什么 Python 代码没有按照预期执行、哪些代码在运行哪些没在运行、局部变量又是什么,我们会使用包含断点和观察模式等功能的调试器,或者直接使用 print 语句打印出来。

 但上面的方法都比较麻烦,例如使用调试器需要进行繁琐的设置,使用 print 打印也要很仔细。与它们相比,使用 PySnooper 只需为要调试的函数添加一个装饰器即可,这样就能获得运行函数详细的 log,包括执行的代码行和执行时间,以及局部变量发生变化的确切时间。

 之所以称为“乞丐版”,相信是因为 PySnooper 使用起来十分简单,开发者可以在任何庞大的代码库中使用它,而无需进行任何设置。只需添加装饰器,并为日志输出地址指定路径。

PyPi

github

2. 安装

root@leco:~/code/py# pip3 install pysnooper
Collecting pysnooper
  Downloading https://files.pythonhosted.org/packages/82/b8/6838637882b27a32afd67c8a1a83e92ae9c1c05c53a40fc6b0100b4f268d/PySnooper-0.0.38-py2.py3-none-any.whl
Installing collected packages: pysnooper
Successfully installed pysnooper-0.0.38

3. 测试

  我们看一下官网的例子

import pysnooper

@pysnooper.snoop()
def number_to_bits(number):
    if number:
        bits = []
        while number:
            number, remainder = divmod(number, 2)
            bits.insert(0, remainder)
        return bits
    else:
        return [0]

number_to_bits(6)
运行过程
root@leco:~/code/py# python3 demo.py 
Starting var:.. number = 6
15:21:12.611209 call         4 def number_to_bits(number):
15:21:12.611372 line         5     if number:
15:21:12.611460 line         6         bits = []
New var:....... bits = []
15:21:12.611540 line         7         while number:
15:21:12.611588 line         8             number, remainder = divmod(number, 2)
Modified var:.. number = 3
New var:....... remainder = 0
15:21:12.611677 line         9             bits.insert(0, remainder)
Modified var:.. bits = [0]
15:21:12.611739 line         7         while number:
15:21:12.611796 line         8             number, remainder = divmod(number, 2)
Modified var:.. number = 1
Modified var:.. remainder = 1
15:21:12.611885 line         9             bits.insert(0, remainder)
Modified var:.. bits = [1, 0]
15:21:12.611964 line         7         while number:
15:21:12.612013 line         8             number, remainder = divmod(number, 2)
Modified var:.. number = 0
15:21:12.612083 line         9             bits.insert(0, remainder)
Modified var:.. bits = [1, 1, 0]
15:21:12.612142 line         7         while number:
15:21:12.612189 line        10         return bits
15:21:12.612237 return      10         return bits
Return value:.. [1, 1, 0]
再看一个例子
root@leco:~/code/py# cat demo1.py 
#!/bin/python
import pysnooper

@pysnooper.snoop()
def add(data):
    sum = 0
    while data > 0:
        sum = sum + data
        data = data - 1
    return sum

ret = add(3)
print('ret = ',ret)
root@leco:~/code/py# python3 demo1.py 
Starting var:.. data = 3
15:27:09.844357 call         5 def add(data):
15:27:09.844510 line         6     sum = 0
New var:....... sum = 0
15:27:09.844649 line         7     while data > 0:
15:27:09.844724 line         8         sum = sum + data
Modified var:.. sum = 3
15:27:09.844805 line         9         data = data - 1
Modified var:.. data = 2
15:27:09.844865 line         7     while data > 0:
15:27:09.844924 line         8         sum = sum + data
Modified var:.. sum = 5
15:27:09.845004 line         9         data = data - 1
Modified var:.. data = 1
15:27:09.845071 line         7     while data > 0:
15:27:09.845130 line         8         sum = sum + data
Modified var:.. sum = 6
15:27:09.845199 line         9         data = data - 1
Modified var:.. data = 0
15:27:09.845256 line         7     while data > 0:
15:27:09.845305 line        10     return sum
15:27:09.845350 return      10     return sum
Return value:.. 6
ret =  6
也可以将输入重定向到文件
root@leco:~/code/py# cat demo2.py 
#!/bin/python
import pysnooper

@pysnooper.snoop('/tmp/file.log')
def add(data):
    sum = 0
    while data > 0:
        sum = sum + data
        data = data - 1
    return sum

ret = add(3)
print('ret = ',ret)
root@leco:~/code/py# python3 demo2.py 
ret =  6
root@leco:~/code/py# cat /tmp/file.log 
Starting var:.. data = 3
15:29:51.102736 call         5 def add(data):
15:29:51.102915 line         6     sum = 0
New var:....... sum = 0
15:29:51.103042 line         7     while data > 0:
15:29:51.103120 line         8         sum = sum + data
Modified var:.. sum = 3
15:29:51.103237 line         9         data = data - 1
Modified var:.. data = 2
15:29:51.103353 line         7     while data > 0:
15:29:51.103427 line         8         sum = sum + data
Modified var:.. sum = 5
15:29:51.103541 line         9         data = data - 1
Modified var:.. data = 1
15:29:51.103654 line         7     while data > 0:
15:29:51.103728 line         8         sum = sum + data
Modified var:.. sum = 6
15:29:51.103840 line         9         data = data - 1
Modified var:.. data = 0
15:29:51.103953 line         7     while data > 0:
15:29:51.104026 line        10     return sum
15:29:51.104100 return      10     return sum
Return value:.. 6

是不是很强大,牛逼不。