99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁大數(shù)據時代一行代碼干掉 debug 和 print,助力算法學習(CDA干貨分享)
一行代碼干掉 debug 和 print,助力算法學習(CDA干貨分享)
2021-12-29
收藏

作者:某某白米飯

來源:Python 技術

在寫算法的時候,總是要每行每個變量一個個的 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 把整個執(zhí)行程序都記錄了下來,其中包括行號, 行內容,變量的結果等情況,我們很容易的就看懂了這個算法的真實情況。并且不需要再使用 debug 和 print 調試代碼。很是省時省力,只需要在方法上面加一行 @pysnooper.snoop()。

復雜使用

pysnooper 包含了多個參數(shù),一起來看看吧

output

output 默認輸出到控制臺,設置后輸出到文件,在服務器中運行的時候,特定的時間出現(xiàn)代碼問題就很容易定位錯誤了,不然容易抓瞎。小編在實際中已經被這種問題困擾了好幾次,每次都掉好多頭發(fā)。

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

示例結果:

一行代碼干掉 debug 和 print,助力算法學習

watch 和 watch_explode

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

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

示例結果

沒有加 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ù),就會有一個 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()

示例結果

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ān)控到調用的函數(shù)的時候,記錄上會加上縮進,并將它的局部變量和返回值打印處理。

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,更能幫助理解算法題。

一行代碼干掉 debug 和 print,助力算法學習
    

數(shù)據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }