再也不用Print函數調試啦--神級Python代碼調試工具PySnooper的介紹和使用

PySnooper

PySnooper是一款適合菜鳥用的代碼調試工具。
如果Python代碼哪兒輸出不對勁了,之前一般都是利用print函數結合pdb.setTrace()來找問題,如果使用pysnooper的話,那事情就變得簡單了!
你可以通過使用這個工具,清楚的看見每一行代碼的執行結果以及所有的局部變量的值。只要在函數上添加一個裝飾行(類似於裝飾器的使用),就可以打印出該函數每一行執行的結果,支持將結果輸出到日誌或者命令行。
pysnooper的強大之處在於只需添加decorator就可以,不用加到處加print函數,調試完之後還得一個個刪除。那麼我們開始吧!
首先安裝:

  • pip:
pip install pysnooper
  • conda with conda-forge channel
conda install -c conda-forge pysnooper

其大致使用方法如下面代碼示例

import pysnooper

將函數運行過程寫到日誌

@pysnooper.snoop("test.log")
def main():
    a = 1
    b = 5
    while a < b:
        a += 1

if __name__ == '__main__':
    main()

將函數運行過程輸出到控制檯

@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)
Source path:... <ipython-input-3-bc51369b551c>
Starting var:.. number = 6
22:17:12.013064 call         2 def number_to_bits(number):
22:17:12.013569 line         3     if number:
22:17:12.013658 line         4         bits = []
New var:....... bits = []
22:17:12.013749 line         5         while number:
22:17:12.013862 line         6             number, remainder = divmod(number, 2)
Modified var:.. number = 3
New var:....... remainder = 0
22:17:12.013948 line         7             bits.insert(0, remainder)
Modified var:.. bits = [0]
22:17:12.014084 line         5         while number:
22:17:12.014198 line         6             number, remainder = divmod(number, 2)
Modified var:.. number = 1
Modified var:.. remainder = 1
22:17:12.014282 line         7             bits.insert(0, remainder)
Modified var:.. bits = [1, 0]
22:17:12.014412 line         5         while number:
22:17:12.014519 line         6             number, remainder = divmod(number, 2)
Modified var:.. number = 0
22:17:12.014669 line         7             bits.insert(0, remainder)
Modified var:.. bits = [1, 1, 0]
22:17:12.014791 line         5         while number:
22:17:12.014900 line         8         return bits
22:17:12.014982 return       8         return bits
Return value:.. [1, 1, 0]





[1, 1, 0]

用with來控制trace的範圍,只對with代碼塊內的代碼進行debug信息打印

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()
1 422.0 843



Source path:... <ipython-input-5-c7244b59db45>
New var:....... lst = [787, 799, 1, 458, 375, 75, 496, 843, 726, 570]
New var:....... i = 9
22:18:51.357818 line        10         lower = min(lst)
New var:....... lower = 1
22:18:51.358333 line        11         upper = max(lst)
New var:....... upper = 843
22:18:51.358460 line        12         mid = (lower + upper) / 2
New var:....... mid = 422.0
22:18:51.358582 line        13         print(lower, mid, upper)
## 查看某些非局部變量
@pysnooper.snoop(watch=('foo.bar', 'self.x["whatever"]'))
#查看對象的所有屬性
@pysnooper.snoop(watch_explode=('foo', 'self'))
##展示我們函數中調用函數的 snoop 行:
@pysnooper.snoop(depth=2)
##將所有用snoop的代碼行加前綴,更好找
@pysnooper.snoop(prefix='ZZZ ')
##在多線程下查看實際的線程id
@pysnooper.snoop(thread_info=True)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章