详解DeBug Python神级工具PySnooper

yipeiwu_com5年前Python基础

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

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

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

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

GitHub项目地址

安装

pip3 install pysnooper
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)

返回日志如下

Starting var:.. number = 6
21:14:32.099769 call         3 @pysnooper.snoop()
21:14:32.099769 line         5     if number:
21:14:32.099769 line         6         bits = []
New var:....... bits = []
21:14:32.099769 line         7         while number:
21:14:32.099769 line         8             number, remainder = divmod(number, 2)
New var:....... remainder = 0
Modified var:.. number = 3
21:14:32.099769 line         9             bits.insert(0, remainder)
Modified var:.. bits = [0]
21:14:32.099769 line         7         while number:
21:14:32.099769 line         8             number, remainder = divmod(number, 2)
Modified var:.. number = 1
Modified var:.. remainder = 1
21:14:32.099769 line         9             bits.insert(0, remainder)
Modified var:.. bits = [1, 0]
21:14:32.099769 line         7         while number:
21:14:32.099769 line         8             number, remainder = divmod(number, 2)
Modified var:.. number = 0
21:14:32.099769 line         9             bits.insert(0, remainder)
Modified var:.. bits = [1, 1, 0]
21:14:32.099769 line         7         while number:
21:14:32.099769 line        10         return bits
21:14:32.099769 return      10         return bits

PySnooper特征

如果标准错误输出难以获得,或者太长了,那么可以将输出定位到本地文件:

@pysnooper.snoop('/my/log/file.log')

查看一些非本地变量的值:

@pysnooper.snoop(variables=('foo.bar', 'self.whatever'))

展示我们函数中调用函数的 snoop 行:

@pysnooper.snoop(depth=2)

将所有 snoop 行以某个前缀开始,更容易定位和找到:

@pysnooper.snoop(prefix='ZZZ ')

更可以用来获取TensorFlow 的各种张量信息,十分强大。妈妈再也不用担心我找不到bug啦!
(2019.5.7更新:有时会不起作用,不知是自己姿势不对还是其他原因。)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

在matplotlib的图中设置中文标签的方法

在matplotlib的图中设置中文标签的方法

其实就是通过 FontProperties来设置的,请参考以下代码: import matplotlib.pyplot as plt from matplotlib.font_man...

使用Python的urllib2模块处理url和图片的技巧两则

获取带有中文参数的url内容 对于中文的参数如果不进行编码的话,python的urllib2直接处理会报错,我们可以先将中文转换成utf- 8编码,然后使用urllib2.quote方法...

这可能是最好玩的python GUI入门实例(推荐)

这可能是最好玩的python GUI入门实例(推荐)

简单的说,GUI编程就是给程序加上图形化界面. python的脚本开发简单,有时候只需几行代码就能实现丰富的功能,而且python本身是跨平台的,所以深受程序员的喜爱. 如果给程序加一...

python 连接sqlite及简单操作

废话不多说了,直接给大家贴代码了,具体代码如下所示: import sqlite3 #查询 def load(table): #连接数据库 con = sqlite3.con...

布隆过滤器的概述及Python实现方法

布隆过滤器 布隆过滤器是一种概率空间高效的数据结构。它与hashmap非常相似,用于检索一个元素是否在一个集合中。它在检索元素是否存在时,能很好地取舍空间使用率与误报比例。正是由于这个特...