行代碼干掉 Debug 和 Print,助力算法學(xué)習(xí)
本文轉(zhuǎn)載自微信公眾號(hào)「Python技術(shù)」,作者派森醬。轉(zhuǎn)載本文請(qǐng)聯(lián)系Python技術(shù)公眾號(hào)。
在寫(xiě)算法的時(shí)候,總是要每行每個(gè)變量一個(gè)個(gè)的 debug,有時(shí)候還要多寫(xiě)幾個(gè) print,一道算法題要花好長(zhǎng)時(shí)間才能理解。pysnooper 模塊可以把在運(yùn)行中變量值都給打印出來(lái)。
模塊安裝
- pip3 install pysnooper
簡(jiǎn)單例子
下面是道簡(jiǎn)單的力扣算法題作為一個(gè)簡(jiǎn)單的例子
- 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"])
結(jié)果:
- 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 把整個(gè)執(zhí)行程序都記錄了下來(lái),其中包括行號(hào), 行內(nèi)容,變量的結(jié)果等情況,我們很容易的就看懂了這個(gè)算法的真實(shí)情況。并且不需要再使用 debug 和 print 調(diào)試代碼。很是省時(shí)省力,只需要在方法上面加一行 @pysnooper.snoop()。
復(fù)雜使用
pysnooper 包含了多個(gè)參數(shù),一起來(lái)看看吧
output
output 默認(rèn)輸出到控制臺(tái),設(shè)置后輸出到文件,在服務(wù)器中運(yùn)行的時(shí)候,特定的時(shí)間出現(xiàn)代碼問(wèn)題就很容易定位錯(cuò)誤了,不然容易抓瞎。小編在實(shí)際中已經(jīng)被這種問(wèn)題困擾了好幾次,每次都掉好多頭發(fā)。
- @pysnooper.snoop('D:\pysnooper.log')
- def longestCommonPrefix(strs):
示例結(jié)果:
watch 和 watch_explode
watch 用來(lái)設(shè)置跟蹤的非局部變量,watch_explode 表示設(shè)置的變量都不監(jiān)控,只監(jiān)控沒(méi)設(shè)置的變量,正好和 watch 相反。
- index = 1
- @pysnooper.snoop(watch=('index'))
- def longestCommonPrefix(strs):
示例結(jié)果
沒(méi)有加 watch 參數(shù)
- 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 參數(shù),就會(huì)有一個(gè) 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 監(jiān)控函數(shù)的深度
- @pysnooper.snoop(depth=2)
- def longestCommonPrefix(strs):
- otherMethod()
示例結(jié)果
- 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 = ''
監(jiān)控的結(jié)果顯示,當(dāng)監(jiān)控到調(diào)用的函數(shù)的時(shí)候,記錄上會(huì)加上縮進(jìn),并將它的局部變量和返回值打印處理。
prefix
prefix 輸出內(nèi)容的前綴
- @pysnooper.snoop(prefix='-------------')
- def longestCommonPrefix(strs):
示例結(jié)果
- -------------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 代碼運(yùn)行的時(shí)間
- @pysnooper.snoop(relative_time=True)
- def longestCommonPrefix(strs):
示例結(jié)果
- 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 輸出的變量和異常的最大長(zhǎng)度,默認(rèn)是 100 個(gè)字符,超過(guò) 100 個(gè)字符就會(huì)被截?cái)啵梢栽O(shè)置為 max_variable_length=None 不截?cái)噍敵?/p>
- @pysnooper.snoop(max_variable_length=5)
- def longestCommonPrefix(strs):
示例結(jié)果
- 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 = (...)
總結(jié)
本文介紹了怎么使用 pysnooper 工具,pysnooper 不僅可以少一些 debug 和 print,更能幫助理解算法題。