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

熱線(xiàn)電話(huà):13121318867

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

作者:某某白米飯

來(lái)源:Python 技術(shù)

在寫(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é)果:

一行代碼干掉 debug 和 print,助力算法學(xué)習(xí)

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)?,可以設(shè)置為 max_variable_length=None 不截?cái)噍敵?/span>

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

一行代碼干掉 debug 和 print,助力算法學(xué)習(xí)
    

數(shù)據(jù)分析咨詢(xún)請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

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

OK
客服在線(xiàn)
立即咨詢(xún)
客服在線(xiàn)
立即咨詢(xún)
') } 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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶(hù)后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".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 = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+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); }