Ultima attività 10 months ago

此程式使用 pretty_errors 改善錯誤輸出,並使用 icecream (ic) 進行簡潔的除錯訊息輸出,方便開發人員快速定位錯誤與分析變數內容,提高程式除錯效率。

timmy ha revisionato questo gist 10 months ago. Vai alla revisione

1 file changed, 18 insertions

error_printer_configuration.py(file creato)

@@ -0,0 +1,18 @@
1 + # 匯入本地的 error_printer 模組
2 + import error_printer
3 +
4 + # 配置 pretty_errors 和 icecream
5 + error_printer.configure_pretty_errors()
6 + error_printer.configure_icecream()
7 +
8 + # 測試代碼
9 + from icecream import ic
10 +
11 + def divide(a, b):
12 + return a / b
13 +
14 + try:
15 + result = divide(10, 0)
16 + except ZeroDivisionError as e:
17 + ic(e) # 使用 icecream 輸出錯誤資訊
18 + raise # 重新拋出異常以觸發 pretty_errors 的輸出

timmy ha revisionato questo gist 10 months ago. Vai alla revisione

1 file changed, 30 insertions

error_printer.py(file creato)

@@ -0,0 +1,30 @@
1 + import pretty_errors
2 + from icecream import ic
3 +
4 +
5 + def configure_pretty_errors() -> None:
6 + """
7 + 配置 pretty_errors 庫,用於更好的錯誤輸出。
8 +
9 + 不需要任何參數。
10 + """
11 + pretty_errors.configure(
12 + line_number_first=True, # 顯示行號在前面
13 + lines_before=5, # 顯示錯誤行之前的行數
14 + lines_after=2, # 顯示錯誤行之後的行數
15 + line_color=pretty_errors.RED
16 + + "> "
17 + + pretty_errors.default_config.line_color, # 自訂錯誤行的顏色
18 + display_locals=True, # 顯示局部變數
19 + )
20 +
21 +
22 + def configure_icecream() -> None:
23 + """
24 + 配置 icecream 庫,用於更好的 print 輸出。
25 +
26 + 不需要任何參數。
27 + """
28 + ic.configureOutput(includeContext=True) # 設定 icecream 函式庫的輸出格式
29 + ic.enable() # 開啟 icecream 的輸出
30 + # ic() # 印出除錯資訊
Più nuovi Più vecchi