在寫算法的時候,總是要每行每個變量一個個的 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,更能幫助理解算法題。
原文鏈接:https://mp.weixin.qq.com/s/rTuxElffIw24GQj_-lAdcw