自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

行代碼干掉 Debug 和 Print,助力算法學(xué)習(xí)

開(kāi)發(fā) 開(kāi)發(fā)工具 算法
本文介紹了怎么使用 pysnooper 工具,pysnooper 不僅可以少一些 debug 和 print,更能幫助理解算法題。

[[442725]]

本文轉(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)。

模塊安裝

  1. pip3 install pysnooper 

簡(jiǎn)單例子

下面是道簡(jiǎn)單的力扣算法題作為一個(gè)簡(jiǎn)單的例子

  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"]) 

結(jié)果:

  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 把整個(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ā)。

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

示例結(jié)果:

watch 和 watch_explode

watch 用來(lái)設(shè)置跟蹤的非局部變量,watch_explode 表示設(shè)置的變量都不監(jiān)控,只監(jiān)控沒(méi)設(shè)置的變量,正好和 watch 相反。

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

示例結(jié)果

沒(méi)有加 watch 參數(shù)

  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 參數(shù),就會(huì)有一個(gè) 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 監(jiān)控函數(shù)的深度

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

示例結(jié)果

  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 = '' 

監(jiān)控的結(jié)果顯示,當(dāng)監(jiān)控到調(diào)用的函數(shù)的時(shí)候,記錄上會(huì)加上縮進(jìn),并將它的局部變量和返回值打印處理。

prefix

prefix 輸出內(nèi)容的前綴

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

示例結(jié)果

  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 代碼運(yùn)行的時(shí)間

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

示例結(jié)果

  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 輸出的變量和異常的最大長(zhǎng)度,默認(rèn)是 100 個(gè)字符,超過(guò) 100 個(gè)字符就會(huì)被截?cái)啵梢栽O(shè)置為 max_variable_length=None 不截?cái)噍敵?/p>

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

示例結(jié)果

  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 = (...) 

總結(jié)

本文介紹了怎么使用 pysnooper 工具,pysnooper 不僅可以少一些 debug 和 print,更能幫助理解算法題。

 

責(zé)任編輯:武曉燕 來(lái)源: Python技術(shù)
相關(guān)推薦

2017-03-17 09:12:13

基礎(chǔ)算法路線(xiàn)

2017-05-10 15:41:29

機(jī)器學(xué)習(xí)算法數(shù)據(jù)

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

算法學(xué)習(xí)指南

2022-12-05 11:44:49

PrintDebugIceCream

2025-03-07 07:20:00

JavaScript異步編程Promise

2020-05-26 16:31:53

算法可視化Github

2022-04-15 08:07:21

ReactDiff算法

2021-02-02 13:35:03

深度學(xué)習(xí)人工智能機(jī)器學(xué)習(xí)

2019-11-12 13:06:20

代碼開(kāi)發(fā)工具

2009-09-10 13:54:27

LINQ語(yǔ)法

2011-08-23 15:57:21

Lua元表元方法

2012-11-23 10:45:04

程序員僵尸代碼

2022-08-10 19:28:40

Hadoop數(shù)據(jù)庫(kù)

2015-07-07 10:43:59

Swift語(yǔ)法基礎(chǔ)

2015-07-07 10:58:29

Swift語(yǔ)法高級(jí)

2009-08-27 09:27:49

C#擴(kuò)展方法

2009-08-31 16:51:11

C# Main()方法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)