行代码干掉 Debug 和 Print,助力算法学习

开发 开发工具 算法
本文介绍了怎么使用 pysnooper 工具,pysnooper 不仅可以少一些 debug 和 print,更能帮助理解算法题。

[[442725]]

本文转载自微信公众号「Python技术」,作者派森酱。转载本文请联系Python技术公众号。

在写算法的时候,总是要每行每个变量一个个的 debug,有时候还要多写几个 print,一道算法题要花好长时间才能理解。pysnooper 模块可以把在运行中变量值都给打印出来。

模块安装

  1. pip3 install pysnooper 

简单例子

下面是道简单的力扣算法题作为一个简单的例子

  1. import pysnooper 
  2.  
  3. @pysnooper.snoop() 
  4. def longestCommonPrefix(strs): 
  5.     res = '' 
  6.     for i in zip(*strs): 
  7.         print(i) 
  8.         if len(set(i)) == 1: 
  9.             res += i[0] 
  10.         else 
  11.             break 
  12.     return res 
  13.   
  14. if __name__ == 'main'
  15.     longestCommonPrefix(["flower","flow","flight"]) 

结果:

  1. 3:38:25.863579 call         4 def longestCommonPrefix(strs): 
  2. 23:38:25.864474 line         5     res = '' 
  3. New var:....... res = '' 
  4. 23:38:25.864474 line         6     for i in zip(*strs): 
  5. New var:....... i = ('f''f''f'
  6. 23:38:25.865479 line         7         print(i) 
  7. ('f''f''f'
  8. 23:38:25.866471 line         8         if len(set(i))==1: 
  9. 23:38:25.866471 line         9             res+=i[0] 
  10. Modified var:.. res = 'f' 
  11. 23:38:25.866471 line         6     for i in zip(*strs): 
  12. Modified var:.. i = ('l''l''l'
  13. 23:38:25.866471 line         7         print(i) 
  14. ('l''l''l'
  15. 23:38:25.867468 line         8         if len(set(i))==1: 
  16. 23:38:25.867468 line         9             res+=i[0] 
  17. Modified var:.. res = 'fl' 
  18. 23:38:25.868476 line         6     for i in zip(*strs): 
  19. Modified var:.. i = ('o''o''i'
  20. 23:38:25.868476 line         7         print(i) 
  21. ('o''o''i'
  22. 23:38:25.869463 line         8         if len(set(i))==1: 
  23. 23:38:25.869463 line        11             break 
  24. 23:38:25.869463 line        12     return res 
  25. 23:38:25.869463 return      12     return res 
  26. Return value:.. 'fl' 
  27. Elapsed time: 00:00:00.008201 

我们可以看到 pysnooper 把整个执行程序都记录了下来,其中包括行号, 行内容,变量的结果等情况,我们很容易的就看懂了这个算法的真实情况。并且不需要再使用 debug 和 print 调试代码。很是省时省力,只需要在方法上面加一行 @pysnooper.snoop()。

复杂使用

pysnooper 包含了多个参数,一起来看看吧

output

output 默认输出到控制台,设置后输出到文件,在服务器中运行的时候,特定的时间出现代码问题就很容易定位错误了,不然容易抓瞎。小编在实际中已经被这种问题困扰了好几次,每次都掉好多头发。

  1. @pysnooper.snoop('D:\pysnooper.log'
  2. def longestCommonPrefix(strs): 

示例结果:

watch 和 watch_explode

watch 用来设置跟踪的非局部变量,watch_explode 表示设置的变量都不监控,只监控没设置的变量,正好和 watch 相反。

  1. index = 1 
  2. @pysnooper.snoop(watch=('index')) 
  3. def longestCommonPrefix(strs): 

示例结果

没有加 watch 参数

  1. Starting var:.. strs = ['flower''flow''flight'
  2. 00:12:33.715367 call         5 def longestCommonPrefix(strs): 
  3. 00:12:33.717324 line         7     res = '' 
  4. New var:....... res = '' 

加了watch 参数,就会有一个 Starting var:.. index

  1. Starting var:.. strs = ['flower''flow''flight'
  2. Starting var:.. index = 1 
  3. 00:10:35.151036 call         5 def longestCommonPrefix(strs): 
  4. 00:10:35.151288 line         7     res = '' 
  5. New var:....... res = '' 

depth

depth 监控函数的深度

  1. @pysnooper.snoop(depth=2) 
  2. def longestCommonPrefix(strs): 
  3.     otherMethod() 

示例结果

  1. Starting var:.. strs = ['flower''flow''flight'
  2. 00:20:54.059803 call         5 def longestCommonPrefix(strs): 
  3. 00:20:54.059803 line         6     otherMethod() 
  4.     00:20:54.060785 call        16 def otherMethod():         
  5.     00:20:54.060785 line        17     x = 1 
  6.     New var:....... x = 1 
  7.     00:20:54.060785 line        18     x = x + 1 
  8.     Modified var:.. x = 2 
  9.     00:20:54.060785 return      18     x = x + 1 
  10.     Return value:.. None 
  11. 00:20:54.061782 line         7     res = '' 

监控的结果显示,当监控到调用的函数的时候,记录上会加上缩进,并将它的局部变量和返回值打印处理。

prefix

prefix 输出内容的前缀

  1. @pysnooper.snoop(prefix='-------------'
  2. def longestCommonPrefix(strs): 

示例结果

  1. -------------Starting var:.. strs = ['flower', 'flow', 'flight'] 
  2. -------------00:39:13.986741 call         5 def longestCommonPrefix(strs): 
  3. -------------00:39:13.987218 line         6     res = '' 

relative_time

relative_time 代码运行的时间

  1. @pysnooper.snoop(relative_time=True
  2. def longestCommonPrefix(strs): 

示例结果

  1. Starting var:.. strs = ['flower''flow''flight'
  2. 00:00:00.000000 call         5 def longestCommonPrefix(strs): 
  3. 00:00:00.001998 line         6     res = '' 
  4. New var:....... res = '' 
  5. 00:00:00.001998 line         7     for i in zip(*strs): 

max_variable_length

max_variable_length 输出的变量和异常的最大长度,默认是 100 个字符,超过 100 个字符就会被截断,可以设置为 max_variable_length=None 不截断输出

  1. @pysnooper.snoop(max_variable_length=5) 
  2. def longestCommonPrefix(strs): 

示例结果

  1. Starting var:.. strs = [...] 
  2. 00:56:44.343639 call         5 def longestCommonPrefix(strs): 
  3. 00:56:44.344696 line         6     res = '' 
  4. New var:....... res = '' 
  5. 00:56:44.344696 line         7     for i in zip(*strs):       
  6. New var:....... i = (...) 

总结

本文介绍了怎么使用 pysnooper 工具,pysnooper 不仅可以少一些 debug 和 print,更能帮助理解算法题。

 

责任编辑:武晓燕 来源: Python技术
相关推荐

2017-03-17 09:12:13

基础算法路线

2017-05-10 15:41:29

机器学习算法数据

2009-10-14 09:27:30

VB.NET编码算法

2009-08-14 09:41:03

C#遗传算法

2011-11-09 09:53:40

算法

2015-12-07 11:22:00

算法学习指南

2022-12-05 11:44:49

PrintDebugIceCream

2022-04-15 08:07:21

ReactDiff算法

2019-11-12 13:06:20

代码开发工具

2020-05-26 16:31:53

算法可视化Github

2021-02-02 13:35:03

深度学习人工智能机器学习

2009-09-10 13:54:27

LINQ语法

2012-11-23 10:45:04

程序员僵尸代码

2011-08-23 15:57:21

Lua元表元方法

2022-08-10 19:28:40

Hadoop数据库

2023-12-07 19:13:48

Pythonprint

2019-04-24 09:25:14

Python编程语言代码

2009-08-31 16:51:11

C# Main()方法

2009-08-27 09:27:49

C#扩展方法

2015-07-07 10:43:59

Swift语法基础
点赞
收藏

51CTO技术栈公众号