亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Python調(diào)試神器之PySnooper

 更新時(shí)間:2021年11月11日 08:49:30   作者:Python學(xué)習(xí)與數(shù)據(jù)挖掘  
在程序開發(fā)過程中,代碼的運(yùn)行往往會(huì)和我們預(yù)期的結(jié)果有所差別。于是,我們需要清楚代碼運(yùn)行過程中到底發(fā)生了什么?代碼哪些模塊運(yùn)行了,哪些模塊沒有運(yùn)行?輸出的局部變量是什么樣的?PySnooper,能夠大大減少調(diào)試過程中的工作量

相信很多程序員在調(diào)試代碼時(shí),都用過 print。代碼少還好說,如果是大型項(xiàng)目,面對(duì)眾多 print 的輸出結(jié)果,可能要頭大了。

今天推薦一個(gè) GitHub 熱門開源項(xiàng)目:PySnooper。該項(xiàng)目推出的第一天就收獲 2000+ Star,登上了 GitHub 日榜第一位,如今有近 15k Star??梢娺@是一款 Python 開發(fā)者喜歡的工具。歡迎收藏學(xué)習(xí)、喜歡點(diǎn)贊支持,文末技術(shù)交流可以暢聊!

圖片

鏈接:https://github.com/cool-RR/PySnooper

PySnooper 是個(gè)什么東西?

如果你寫的 Python 代碼不能按如期那樣運(yùn)行,你會(huì)絞盡腦汁想為啥出錯(cuò)了。雖然你希望有支持?jǐn)帱c(diǎn)的成熟調(diào)試器,但或許你現(xiàn)在不想去設(shè)置這樣的調(diào)試器。

你想知道哪些行代碼是正常運(yùn)行,哪些行不正常。據(jù)說大多數(shù)人會(huì)在可疑位置使用 print 輸出語句。

其實(shí) PySnooper 的作用有點(diǎn)類似,你不用小心翼翼地用 print 輸出,只需在想調(diào)試的函數(shù)中引入一個(gè)裝飾器。然后得到函數(shù)的詳細(xì)日志,包括運(yùn)行了哪些行、何時(shí)運(yùn)行,以及何時(shí)更改了局部變量。

為什么 PySnooper 能從其他智能調(diào)試工具中脫穎而出?

因?yàn)槟憧梢栽诓恍枰M(jìn)行任何設(shè)置的情況下將其用于糟糕的、龐大的企業(yè)代碼庫中。只需打開裝飾器(如下示例所示),并將輸出重定向到一個(gè)專用的日志文件,將日志文件路徑指定為第一個(gè)參數(shù)。

PS:如果無法訪問 stderr,那可以將輸出重定向到指定文件,比如 :@pysnooper.snoop('/my/log/file.log')

使用范例

范例是一個(gè)把數(shù)字轉(zhuǎn)成二進(jìn)制的函數(shù)。

import pysnooper

@pysnooper.snoop()
def number_to_bits(number):
    if number:
        bits = []
        while number:
            number, remainder = divmod(number, 2)
            bits.insert(0, remainder)
        return bits
    else:
        return [0]

number_to_bits(6)

輸出結(jié)果

Starting var:.. number = 6
21:14:32.099769 call 3 @pysnooper.snoop()
21:14:32.099769 line 5 if number:
21:14:32.099769 line 6 bits = []
New var:....... bits = []
21:14:32.099769 line 7 while number:
21:14:32.099769 line 8 number, remainder = divmod(number, 2)
New var:....... remainder = 0
Modified var:.. number = 3
21:14:32.099769 line 9 bits.insert(0, remainder)
Modified var:.. bits = [0]
21:14:32.099769 line 7 while number:
21:14:32.099769 line 8 number, remainder = divmod(number, 2)
Modified var:.. number = 1
Modified var:.. remainder = 1
21:14:32.099769 line 9 bits.insert(0, remainder)
Modified var:.. bits = [1, 0]
21:14:32.099769 line 7 while number:
21:14:32.099769 line 8 number, remainder = divmod(number, 2)
Modified var:.. number = 0
21:14:32.099769 line 9 bits.insert(0, remainder)
Modified var:.. bits = [1, 1, 0]
21:14:32.099769 line 7 while number:
21:14:32.099769 line 10 return bits
21:14:32.099769 return 10 return bits

如果你不想追蹤整個(gè)函數(shù),那可以用 with 塊包裝你想追蹤的那部分,如下:

import pysnooper
import random

def foo():
    lst = []
    for i in range(10):
        lst.append(random.randrange(1, 1000))

    with pysnooper.snoop():
        lower = min(lst)
        upper = max(lst)
        mid = (lower + upper) / 2
        print(lower, mid, upper)

foo()

輸出結(jié)果

New var:....... i = 9
New var:....... lst = [681, 267, 74, 832, 284, 678, ...]
09:37:35.881721 line 10 lower = min(lst)
New var:....... lower = 74
09:37:35.882137 line 11 upper = max(lst)
New var:....... upper = 832
09:37:35.882304 line 12 mid = (lower + upper) / 2
74 453.0 832
New var:....... mid = 453.0
09:37:35.882486 line 13 print(lower, mid, upper)
Elapsed time: 00:00:00.000344

如何安裝?

最佳方式:pip

$ pip install pysnooper

其他方式:

  • Conda:$ conda install -c conda-forge pysnooper
  • Arch Linux:$ yay -S python-pysnooper
  • Fedora Linux:dnf install python3-pysnooper

技術(shù)交流

歡迎轉(zhuǎn)載、收藏、有所收獲點(diǎn)贊支持一下!

在這里插入圖片描述

以上就是詳解Python調(diào)試神器之PySnooper的詳細(xì)內(nèi)容,更多關(guān)于Python PySnooper的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python 查找字符在字符串中的位置實(shí)例

    Python 查找字符在字符串中的位置實(shí)例

    下面小編就為大家分享一篇Python 查找字符在字符串中的位置實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 跟老齊學(xué)Python之通過Python連接數(shù)據(jù)庫

    跟老齊學(xué)Python之通過Python連接數(shù)據(jù)庫

    現(xiàn)在在做python的時(shí)候需要用到數(shù)據(jù)庫,于是自己重新整理了一下數(shù)據(jù)庫的知識(shí),并且熟悉了python中MysqlDB模塊的功能和函數(shù)等接口,現(xiàn)在系統(tǒng)地來總結(jié)一下吧
    2014-10-10
  • python中的__slots__使用示例

    python中的__slots__使用示例

    這篇文章主要介紹了python中的__slots__使用示例,__slots__用來限制class能添加的屬性,需要的朋友可以參考下
    2015-02-02
  • python property的使用技巧分享

    python property的使用技巧分享

    這篇文章主要介紹了python property的使用技巧分享,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-05-05
  • python腳本實(shí)現(xiàn)mp4中的音頻提取并保存在原目錄

    python腳本實(shí)現(xiàn)mp4中的音頻提取并保存在原目錄

    這篇文章主要介紹了python腳本實(shí)現(xiàn)mp4中的音頻提取并保存在原目錄,本文給大家通過實(shí)例代碼介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • python中validators庫的使用方法詳解

    python中validators庫的使用方法詳解

    這篇文章主要介紹了python中validators庫的使用方法詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • python實(shí)現(xiàn)給數(shù)組按片賦值的方法

    python實(shí)現(xiàn)給數(shù)組按片賦值的方法

    這篇文章主要介紹了python實(shí)現(xiàn)給數(shù)組按片賦值的方法,實(shí)例分析了Python在指定位置進(jìn)行賦值的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • Python 中的 else詳解

    Python 中的 else詳解

    這篇文章主要介紹了Python 中的 else詳解的相關(guān)資料,需要的朋友可以參考下
    2016-04-04
  • Python基礎(chǔ)之tkinter圖形化界面學(xué)習(xí)

    Python基礎(chǔ)之tkinter圖形化界面學(xué)習(xí)

    這篇文章主要介紹了Python基礎(chǔ)之tkinter圖形化界面學(xué)習(xí),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • 使用Python+Flask開發(fā)博客項(xiàng)目并實(shí)現(xiàn)內(nèi)網(wǎng)穿透

    使用Python+Flask開發(fā)博客項(xiàng)目并實(shí)現(xiàn)內(nèi)網(wǎng)穿透

    Flask是一個(gè)使用python編寫的輕量級(jí)Web框架,這篇文章我們將使用這個(gè)框架編寫一個(gè)屬于自己的博客網(wǎng)站!并教你如何通過使用內(nèi)網(wǎng)穿透工具處理項(xiàng)目,讓本地的項(xiàng)目可以在公網(wǎng)訪問,感興趣的可以了解一下
    2021-11-11

最新評(píng)論