新聞中心
PySnooper是一款適合菜鳥用的代碼調(diào)試工具。 如果Python代碼哪兒輸出不對勁了,之前一般都是利用print函數(shù)結(jié)合pdb.setTrace()來找問題,如果使用pysnooper的話,那事情就變得簡單了!

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、網(wǎng)站制作、尼開遠網(wǎng)絡(luò)推廣、小程序開發(fā)、尼開遠網(wǎng)絡(luò)營銷、尼開遠企業(yè)策劃、尼開遠品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供尼開遠建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
1. 快速安裝
執(zhí)行下面這些命令進行安裝 PySnooper
$ python3 -m pip install pysnooper
# 或者
$ conda install -c conda-forge pysnooper
# 或者
$ yay -S python-pysnooper
2. 簡單案例
下面這段代碼,定義了一個 demo_func 的函數(shù),在里面生成一個 profile 的字典變量,然后去更新它,最后返回。
代碼本身沒有什么實際意義,但是用來演示 PySnooper 已經(jīng)足夠。
import pysnooper
@pysnooper.snoop()
def demo_func():
profile = {}
profile["name"] = "寫代碼的明哥"
profile["age"] = 27
profile["gender"] = "male"
return profile
def main():
profile = demo_func()
main()
現(xiàn)在我使用終端命令行的方式來運行它
[root@iswbm ~]# python3 demo.py
Source path:... demo.py
17:52:49.624943 call 4 def demo_func():
17:52:49.625124 line 5 profile = {}
New var:....... profile = {}
17:52:49.625156 line 6 profile["name"] = "寫代碼的明哥"
Modified var:.. profile = {'name': '寫代碼的明哥'}
17:52:49.625207 line 7 profile["age"] = 27
Modified var:.. profile = {'name': '寫代碼的明哥', 'age': 27}
17:52:49.625254 line 8 profile["gender"] = "male"
Modified var:.. profile = {'name': '寫代碼的明哥', 'age': 27, 'gender': 'male'}
17:52:49.625306 line 10 return profile
17:52:49.625344 return 10 return profile
Return value:.. {'name': '寫代碼的明哥', 'age': 27, 'gender': 'male'}
Elapsed time: 00:00:00.000486
可以看到 PySnooper 把函數(shù)運行的過程全部記錄了下來,包括:
代碼的片段、行號等信息,以及每一行代碼是何時調(diào)用的?
函數(shù)內(nèi)局部變量的值如何變化的?何時新增了變量,何時修改了變量。
函數(shù)的返回值是什么?
運行函數(shù)消耗了多少時間?
而作為開發(fā)者,要得到這些如此詳細的調(diào)試信息,你需要做的非常簡單,只要給你想要調(diào)試的函數(shù)上帶上一頂帽子(裝飾器) — @pysnooper.snoop() 即可。
3. 詳細使用
3.1 重定向到日志文件
@pysnooper.snoop() 不加任何參數(shù)時,會默認將調(diào)試的信息輸出到標準輸出。
對于單次調(diào)試就能解決的 BUG ,這樣沒有什么問題,但是有一些 BUG 只有在特定的場景下才會出現(xiàn),需要你把程序放在后面跑個一段時間才能復(fù)現(xiàn)。
這種情況下,你可以將調(diào)試信息重定向輸出到某一日志文件中,方便追溯排查。
@pysnooper.snoop(output='/var/log/debug.log')
def demo_func():
...
3.2 跟蹤非局部變量值
PySnooper 是以函數(shù)為單位進行調(diào)試的,它默認只會跟蹤函數(shù)體內(nèi)的局部變量,若想跟蹤全局變量,可以給 pysnooper.snoop() 加上 watch 參數(shù)
out = {"foo": "bar"}
@pysnooper.snoop(watch=('out["foo"]'))
def demo_func():
...
如此一來,PySnooper 會在 out[“foo”] 值有變化時,也將其打印出來 watch 參數(shù),接收一個可迭代對象(可以是list 或者 tuple),里面的元素為字符串表達式,什么意思呢?看下面例子就知道了
@pysnooper.snoop(watch=('out["foo"]', 'foo.bar', 'self.foo["bar"]'))
def demo_func():
...
和 watch 相對的,pysnooper.snoop() 還可以接收一個函數(shù) watch_explode,表示除了這幾個參數(shù)外的其他所有全局變量都監(jiān)控。
@pysnooper.snoop(watch_explode=('foo', 'bar'))
def demo_func():
...
3.3 設(shè)置跟蹤函數(shù)的深度
當(dāng)你使用 PySnooper 調(diào)試某個函數(shù)時,若該函數(shù)中還調(diào)用了其他函數(shù),PySnooper 是不會傻傻的跟蹤進去的。
如果你想繼續(xù)跟蹤該函數(shù)中調(diào)用的其他函數(shù),可以通過指定 depth 參數(shù)來設(shè)置跟蹤深度(不指定的話默認為 1)。
@pysnooper.snoop(depth=2)
def demo_func():
...
3.4 設(shè)置調(diào)試日志的前綴
當(dāng)你在使用 PySnooper 跟蹤多個函數(shù)時,調(diào)試的日志會顯得雜亂無章,不方便查看。
在這種情況下,PySnooper 提供了一個參數(shù),方便你為不同的函數(shù)設(shè)置不同的標志,方便你在查看日志時進行區(qū)分。
@pysnooper.snoop(output="/var/log/debug.log", prefix="demo_func: ")
def demo_func():
...
效果如下
3.5 設(shè)置最大的輸出長度
默認情況下,PySnooper 輸出的變量和異常信息,如果超過 100 個字符,被會截斷為 100 個字符。
當(dāng)然你也可以通過指定參數(shù) 進行修改
@pysnooper.snoop(max_variable_length=200)
def demo_func():
...
您也可以使用max_variable_length=None它從不截斷它們。
@pysnooper.snoop(max_variable_length=None)
def demo_func():
...
3.6 支持多線程調(diào)試模式
PySnooper 同樣支持多線程的調(diào)試,通過設(shè)置參數(shù) thread_info=True,它就會在日志中打印出是在哪個線程對變量進行的修改。
@pysnooper.snoop(thread_info=True)
def demo_func():
...
效果如下
3.7 自定義對象的格式輸出
pysnooper.snoop() 函數(shù)有一個參數(shù)是 custom_repr,它接收一個元組對象。
在這個元組里,你可以指定特定類型的對象以特定格式進行輸出。
這邊我舉個例子。
假如我要跟蹤 person 這個 Person 類型的對象,由于它不是常規(guī)的 Python 基礎(chǔ)類型,PySnooper 是無法正常輸出它的信息的。
因此我在 pysnooper.snoop() 函數(shù)中設(shè)置了 custom_repr 參數(shù),該參數(shù)的第一個元素為 Person,第二個元素為 print_persion_obj 函數(shù)。
PySnooper 在打印對象的調(diào)試信息時,會逐個判斷它是否是 Person 類型的對象,若是,就將該對象傳入 print_persion_obj 函數(shù)中,由該函數(shù)來決定如何顯示這個對象的信息。
class Person:pass
def print_person_obj(obj):
return f""
@pysnooper.snoop(custom_repr=(Person, print_person_obj))
def demo_func():
...
完整的代碼如下
import pysnooper
class Person:pass
def print_person_obj(obj):
return f""
@pysnooper.snoop(custom_repr=(Person, print_person_obj))
def demo_func():
person = Person()
person.name = "寫代碼的明哥"
person.age = 27
person.gender = "male"
return person
def main():
profile = demo_func()
main()
運行一下,觀察一下效果。 如果你要自定義格式輸出的有很多個類型,那么 custom_repr 參數(shù)的值可以這么寫
@pysnooper.snoop(custom_repr=((Person, print_person_obj), (numpy.ndarray, print_ndarray)))
def demo_func():
...
還有一點我提醒一下,元組的第一個元素可以是類型(如類名Person 或者其他基礎(chǔ)類型 list等),也可以是一個判斷對象類型的函數(shù)。
也就是說,下面三種寫法是等價的。
# 【第一種寫法】
@pysnooper.snoop(custom_repr=(Person, print_persion_obj))
def demo_func():
...
# 【第二種寫法】
def is_persion_obj(obj):
return isinstance(obj, Person)
@pysnooper.snoop(custom_repr=(is_persion_obj, print_persion_obj))
def demo_func():
...
# 【第三種寫法】
@pysnooper.snoop(custom_repr=(lambda obj: isinstance(obj, Person), print_persion_obj))
def demo_func():
...
分享名稱:通過PySnooper調(diào)試python代碼
URL網(wǎng)址:http://www.5511xx.com/article/dphccij.html


咨詢
建站咨詢
