写算法的时候,总是需要把每一行,每个变量一个一个调试,有时还要多写几个print。理解一个算法问题需要很长时间。pysnooper模块可以在运行过程中打印出变量的值。\模块安装pip3installpysnooper简单示例下面是一个简单的snap算法题作为简单示例importpysnooper@pysnooper.snoop()deflongestCommonPrefix(strs):res=''foriinzip(*strs):print(i)iflen(set(i))==1:res+=i[0]elseelsebreakreturnresif__name__=='main':longestCommonPrefix(["flower","flow","flight"])结果:3:38:25.863579call4deflongestCommonPrefix(strs):23:38:25.864474line5res=''Newvar:......res=''23:38:25.864474lineinifor6zip(*strs):Newvar:.......i=('f','f','f')23:38:25.865479第7行print(i)('f','f','f')23:38:25.866471line88iflen(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.867468第8行如果len(set(i))==1:23:38:25.867468line9res+=i[0]Modifiedvar:..res='fl'23:38:25.868476line6foriinzip(*strs):Modifiedvar:..i=(','o','i')23:38:25.868476line7print(i)('o','o','i')23:38:25.869463line8iflen(set(i))==1:23:38:25.869463LINE11Break23:38:25.869463LINE12ReturnRes23:38:25.869463ReturnRes返回值:.'Fl'ElapSedTime:00:00.008201我们可以看到Pys程序被记录下来,包括行号,行内容,变量结果等,我们可以很容易的了解算法的真实情况,不需要使用debug和print调试代码。非常省时省力,只需要在方法上面加一行@pysnooper.snoop()即可。pysnooper的复杂使用包含多个参数。让我们来看看。output默认输出到控制台,设置后输出到文件。在服务端运行时,如果特定时间出现代码问题,很容易定位错误,否则很容易抓瞎。小编在实践中被这个问题困扰过好几次,每次都掉了不少头发。@pysnooper.snoop('D:\pysnooper.log')deflongestCommonPrefix(strs):示例结果:watch和watch_explodewatch用于设置跟踪的非局部变量,watch_explode表示设置的变量不被监听,只监听监视未设置的变量,与监视相反。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.151288line7=res''Newvar:.......res=''depth深度监控函数depth@pysnooper.snoop(depth=2)deflongestCommonPrefix(strs):otherMethod()exampleresultStartingvar:..strs=['flower'Flow','Flight']00:20:54.059803Call5DEFLongestCommonPrefix(STRS):00:20:54.059803LINE6Othermethod()00:20:5406078555555555555:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00第17行x=1新变量:......x=100:20:54.060785第18行x=x+1修改后的变量:..x=200:20:54.060785return18x=x+1Returnvalue:..None00:20:54.061782line7res=''监控结果显示调用的函数被监控时,会添加到记录中缩进并打印其局部变量和返回值prefixprefix输出内容的前缀@pysnooper.snoop(prefix='------------')deflongestCommonPrefix(strs):exampleresult--------------Startingvar:..strs=['flower','flow','flight']------------00:39:13.986741call5deflongestCommonPrefix(strs):-----------00:39:13.987218第6行res=''relative_timerelative_time代码运行的时间@pysnooper.snoop(relative_time=True)deflongestCommonPrefix(strs):ExampleresultStartingvar:..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输出变量和异常的最大长度,默认为100个字符,超过100个字符将被截断,可以设置max_variable_length=None不截断output@pysnooper.snoop(max_variable_length=5)deflongestCommonPrefix(strs):ExampleresultStartingvar:..strs=[...]00:56:44.343639call5deflongestCommonPrefix(strs):00:56:44.344696lineres=6''Newvar:.......res=''00:56:44.344696line7foriinzip(*strs):Newvar:......i=(...)总结本文介绍如何使用pysnooper工具,pysnooper不仅可以省去一些debug打印,还可以帮助你理解算法题。有收获请点赞、关注、转发。感谢您的阅读和支持。
