共计 4190 个字符,预计需要花费 11 分钟才能阅读完成。
在写算法的时候,总是要每行每个变量一个个的 debug,有时候还要多写几个 print,一道算法题要花好长时间能力了解。pysnooper 模块能够把在运行中变量值都给打印进去。\
模块装置
pip3 install pysnooper
简略例子
上面是道简略的力扣算法题作为一个简略的例子
import pysnooper
@pysnooper.snoop()
def longestCommonPrefix(strs):
res = ''
for i in zip(*strs):
print(i)
if len(set(i)) == 1:
res += i[0]
else
break
return res
if __name__ == 'main':
longestCommonPrefix(["flower","flow","flight"])
后果:
3:38:25.863579 call 4 def longestCommonPrefix(strs):
23:38:25.864474 line 5 res = ''New var:....... res =''
23:38:25.864474 line 6 for i in zip(*strs):
New var:....... i = ('f', 'f', 'f')
23:38:25.865479 line 7 print(i)
('f', 'f', 'f')
23:38:25.866471 line 8 if len(set(i))==1:
23:38:25.866471 line 9 res+=i[0]
Modified var:.. res = 'f'
23:38:25.866471 line 6 for i in zip(*strs):
Modified var:.. i = ('l', 'l', 'l')
23:38:25.866471 line 7 print(i)
('l', 'l', 'l')
23:38:25.867468 line 8 if len(set(i))==1:
23:38:25.867468 line 9 res+=i[0]
Modified var:.. res = 'fl'
23:38:25.868476 line 6 for i in zip(*strs):
Modified var:.. i = ('o', 'o', 'i')
23:38:25.868476 line 7 print(i)
('o', 'o', 'i')
23:38:25.869463 line 8 if len(set(i))==1:
23:38:25.869463 line 11 break
23:38:25.869463 line 12 return res
23:38:25.869463 return 12 return res
Return value:.. 'fl'
Elapsed time: 00:00:00.008201
咱们能够看到 pysnooper 把整个执行程序都记录了下来,其中包含行号,行内容,变量的后果等状况,咱们很容易的就看懂了这个算法的真实情况。并且不须要再应用 debug 和 print 调试代码。很是省时省力,只须要在办法下面加一行 @pysnooper.snoop()。
简单应用
pysnooper 蕴含了多个参数,一起来看看吧
output
output 默认输入到控制台,设置后输入到文件,在服务器中运行的时候,特定的工夫呈现代码问题就很容易定位谬误了,不然容易抓瞎。小编在理论中曾经被这种问题困扰了好几次,每次都掉好多头发。
@pysnooper.snoop('D:\pysnooper.log')
def longestCommonPrefix(strs):
示例后果:
watch 和 watch_explode
watch 用来设置跟踪的非局部变量,watch_explode 示意设置的变量都不监控,只监控没设置的变量,正好和 watch 相同。
index = 1
@pysnooper.snoop(watch=('index'))
def longestCommonPrefix(strs):
示例后果
没有加 watch 参数
Starting var:.. strs = ['flower', 'flow', 'flight']
00:12:33.715367 call 5 def longestCommonPrefix(strs):
00:12:33.717324 line 7 res = ''New var:....... res =''
加了 watch 参数,就会有一个 Starting var:.. index
Starting var:.. strs = ['flower', 'flow', 'flight']
Starting var:.. index = 1
00:10:35.151036 call 5 def longestCommonPrefix(strs):
00:10:35.151288 line 7 res = ''New var:....... res =''
depth
depth 监控函数的深度
@pysnooper.snoop(depth=2)
def longestCommonPrefix(strs):
otherMethod()
示例后果
Starting var:.. strs = ['flower', 'flow', 'flight']
00:20:54.059803 call 5 def longestCommonPrefix(strs):
00:20:54.059803 line 6 otherMethod()
00:20:54.060785 call 16 def otherMethod():
00:20:54.060785 line 17 x = 1
New var:....... x = 1
00:20:54.060785 line 18 x = x + 1
Modified var:.. x = 2
00:20:54.060785 return 18 x = x + 1
Return value:.. None
00:20:54.061782 line 7 res = ''
监控的结果显示,当监控到调用的函数的时候,记录上会加上缩进,并将它的局部变量和返回值打印解决。
prefix
prefix 输入内容的前缀
@pysnooper.snoop(prefix='-------------')
def longestCommonPrefix(strs):
示例后果
-------------Starting var:.. strs = ['flower', 'flow', 'flight']
-------------00:39:13.986741 call 5 def longestCommonPrefix(strs):
-------------00:39:13.987218 line 6 res = ''
relative_time
relative_time 代码运行的工夫
@pysnooper.snoop(relative_time=True)
def longestCommonPrefix(strs):
示例后果
Starting var:.. strs = ['flower', 'flow', 'flight']
00:00:00.000000 call 5 def longestCommonPrefix(strs):
00:00:00.001998 line 6 res = ''New var:....... res =''
00:00:00.001998 line 7 for i in zip(*strs):
max_variable_length
max_variable_length 输入的变量和异样的最大长度,默认是 100 个字符,超过 100 个字符就会被截断,能够设置为 max_variable_length=None
不截断输入
@pysnooper.snoop(max_variable_length=5)
def longestCommonPrefix(strs):
示例后果
Starting var:.. strs = [...]
00:56:44.343639 call 5 def longestCommonPrefix(strs):
00:56:44.344696 line 6 res = ''New var:....... res =''
00:56:44.344696 line 7 for i in zip(*strs):
New var:....... i = (...)
总结
本文介绍了怎么应用 pysnooper 工具,pysnooper 不仅能够少一些 debug 和 print,更能帮忙了解算法题。
如果有播种,还请点赞、在看、转发,感激你的浏览和反对。