转载本文,请联系Python技术公众号。写算法的时候,总是要把每一行,每一个变量都一个一个调试,有时还要多写几个打印。理解一个算法问题需要很长时间。pysnooper模块可以在运行过程中打印出变量的值。模块安装pip3installpysnooper简单示例下面是一个简单的snap算法题作为简单示例==1:res+=i[0]elsebreakreturnresif__name__=='main':longestCommonPrefix(["flower","flow","flight"])结果:3:38:25.863579call4deflongestCommonPrefix(strs):23:38:25.864474line5res=''Newvar:.......res=''23:38:25.864474line6foriinzip(*strs):Newvar:......i=('f','f','f')23:38:25.865479line7print(i)('f','f','f')23:38:25.866471line8iflen(set(i))==1:23:38:25.866471line9res+=i[0]Modifiedvar:..res='f'23:38:25.866471line6foriinzip(*strs):Modifiedvar:..i=('l','l','l')23:38:25.866471line7print(i)('l','l','l')23:38:25.867468line8iflen(set(i))==1:23:38:25.867468line9res+=i[0]Modifiedvar:..res='fl'23:38:25.868476line6foriinzip(*strs):Modifiedvar:..i=('o','o','i')23:38:25.868476line7print(i)('o','o','i')23:38:25.869463line8iflen(set(i))==1:23:38:25.869463line11break23:38:25.869463line12rreturnres23:38:25.869463return12returnresReturnvalue:..'fl'Elapsedtime:00:00:00.008201我们可以看到pysnooper记录了整个执行程序,包括行号、行内容、变量结果等,我们可以很容易的理解该算法的真实情况,不需要使用调试和打印来调试代码。非常省时省力,只需要在方法上面加一行@pysnooper.snoop()即可。pysnooper的复杂使用包含多个参数。让我们来看看。output默认输出到控制台,设置后输出到文件。在服务端运行时,如果特定时间出现代码问题,很容易定位错误,否则很容易抓瞎。小编在实践中被这个问题困扰过好几次,每次都掉了不少头发。@pysnooper.snoop('D:\pysnooper.log')deflongestCommonPrefix(strs):示例结果:watch和watch_explodewatch用于设置跟踪的非局部变量,watch_explode表示不监视设置的变量,只监视unset变量被监控,和watch正好相反。index=1@pysnooper.snoop(watch=('index'))deflongestCommonPrefix(strs):示例结果不加watch参数startingvar:..strs=['flower','flow','flight']00:12:33.715367call5deflongestCommonPrefix(strs):00:12:33.717324line7res=''Newvar:.......res=''添加watch参数,会有一个Startingvar:..indexStartingvar:..strs=['flower','flow','flight']Startingvar:..index=100:10:35.151036call5deflongestCommonPrefix(strs):00:10:35.151288line7res=''Newvar:.......res=''depthdepth深度监控函数@pysnooper.snoop(depth=2)deflongestCommonPrefix(strs):otherMethod()示例结果Startingvar:..strs=['flower','flow','flight']00:20:54.059803call5deflongestCommonPrefix(strs):00:20:54.059803line6otherMethod()00:20:54.060785call16defetherMethod():00:20:54.060785line17x=1Newvar:......x=100:20:54.060785line18x=x+1Modifiedvar:..x=200:20:54.060785return18x=x+1Returnvalue:..None00:20:54.061782line7res=''监控结果显示当调用的函数被监控d、将记录缩进及其局部变量和返回值打印处理。prefixprefix输出内容的前缀@pysnooper.snoop(prefix='------------')deflongestCommonPrefix(strs):Exampleresult----------Startingvar:..strs=['flower','flow','flight']------------00:39:13.986741call5deflongestCommonPrefix(strs):---------------00:39:13.987218line6res=''relative_timerelative_time代码运行的时间@pysnooper.snoop(relative_time=True)deflongestCommonPrefix(strs):ExampleresultsStartingvar:..strs=['flower','flow','flight']00:00:00.000000call5deflongestCommonPrefix(strs):00:00:00.001998line6res=''Newvar:......res=''00:00:00.001998line7foriinzip(*strs):max_variable_lengthmax_variable_length输出变量sum异常的最大长度,默认为100个字符,超过100个字符将被截断,可以设置为max_variable_length=None不截断输出@pysnooper.snoop(max_variable_length=5)deflongestCommonPrefix(strs):exampleresultStartingvar:..strs=[...]00:56:44.343639call5deflongestCommonPrefix(strs):00:56:44.344696line6res=''Newvar:.......res=''00:56:44.344696line7foriinzip(*strs):Newvar:......i=(...)总结本文介绍如何使用pysnooper工具,pysnooper不仅可以省去一些debug和print,还可以帮助理解算法题
