首頁  >  文章  >  後端開發  >  Python 偵錯實用程式

Python 偵錯實用程式

WBOY
WBOY原創
2024-08-05 19:04:311084瀏覽

Python Debugging utility

pdbwhereami

目的

這是一個實用程式模組,可協助使用以下功能偵錯 python 來源

  • 誰阿米
  • 我在哪裡
  • 誰叫我
  • 叫樹
  • 誰爸爸

它用在哪裡?

該模組在幾個關鍵方面為開發人員提供協助,特別是在偵錯和維護程式碼方面。以下是主要好處:

1. 調試:

  • 辨識錯誤:發生錯誤時,了解確切的行號和檔案可以顯著減少搜尋原因所花費的時間。透過在錯誤訊息中呼叫這些函數,開發人員可以快速找出問題出現的位置。
  • 可追溯性:透過記錄行號和檔案名,開發人員可以追蹤程式的執行路徑,尤其是在處理複雜的程式碼庫或多檔案專案時。

2. 記錄:

  • 詳細日誌:在日誌訊息中包含行號和檔案名稱可提供詳細的上下文,使日誌資訊更豐富且更易於解釋。
  • 自動監控:在大型系統中,自動監控工具可以使用這些詳細資訊來標記程式碼中經常導致問題的特定部分,從而幫助主動維護。

3. 斷言與除錯建構:

  • 斷言:使用像assert()這樣的宏以及whoami、whereami、whocalledme、calledtree和whosdaddy有助於在開發過程中捕獲邏輯錯誤。當斷言失敗時,它會列印行號和文件名,從而更容易調試。
  • 條件編譯: 對於偵錯版本,可以使用這些巨集包含附加診斷信息,而不會影響發布版本。

4. 文件與維護:

  • 程式碼審查:在程式碼審查期間,擁有詳細的錯誤日誌可以幫助審查者了解流程並更有效地識別潛在的問題區域。
  • 維護:對於長期維護,包含行號和檔案名稱的詳細錯誤報告可以幫助新開發人員理解和修復問題,而無需深入熟悉整個程式碼庫。

透過利用 whoami、whereami、whocalledme、calledtree 和 whosdaddy,開發人員可以創建更健壯、可維護且更易於調試的程式碼。

如何使用?

from pdbwhereami import whoami, whereami, whocalledme, calledtree, whosdaddy

def debug_utility():
    whoami()
    whoami("I am in test_whoami function")

    print()

    whereami()
    whereami("I am in test_whereami")
    whereami(obsolete_path = True)
    whereami(path_depth=6)

    print()

    whocalledme()
    whocalledme(obsolete_path = True)
    whocalledme(path_depth=6)

    print()

    calledtree()
    tstr = calledtree(verbose=False)
    print(tstr)
    calledtree(tree_depth=1)
    calledtree(tree_depth=2)

    print()
    whosdaddy()

debug_utility()

輸出

[debug_utility] ->
[debug_utility] -> I am in test_whoami function

[whereami/main.py:71]:debug_utility ->
[whereami/main.py:72]:debug_utility -> I am in test_whereami
[/home/bhagavan/whereami/main.py:73]:debug_utility ->
[/home/bhagavan/whereami/main.py:74]:debug_utility ->

[whereami/main.py:93]:<module> ->
[/home/bhagavan/whereami/main.py:93]:<module> ->
[/home/bhagavan/whereami/main.py:93]:<module> ->

#1[/home/bhagavan/whereami/main.py:84]:debug_utility <--
#0[/home/bhagavan/whereami/main.py:93]:<module> <--

#1[/home/bhagavan/whereami/main.py:85]:debug_utility <--
#0[/home/bhagavan/whereami/main.py:93]:<module> <--

#0[/home/bhagavan/whereami/main.py:87]:debug_utility <--

#1[/home/bhagavan/whereami/main.py:88]:debug_utility <--
#0[/home/bhagavan/whereami/main.py:93]:<module> <--

[<module>] ->

安裝

使用點

pip install pdbwhereami

使用來源

git clone https://github.com/bhagavansprasad/pdbwhereami.git
cd pdbwhereami
pip insall ./

以上是Python 偵錯實用程式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:長生不老藥下一篇:長生不老藥