Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การควบคุม Warning ในโปรแกรม Python


Warning (คำเตือน) แตกต่างจาก error (ข้อผิดพลาด) ในโปรแกรม หากพบ error โปรแกรม Python จะหยุดทำงานทันที Warning ด้านอื่น ไม่ร้ายแรง มันแสดงข้อความบางอย่างแต่โปรแกรมทำงานต่อไป Warnings ถูกแจ้งเตือนเพื่อแจ้งให้ผู้ใช้ทราบเงื่อนไขบางอย่างที่ไม่ใช่ exceptions แน่นอน ปกติ warning จะปรากฏหากพบการใช้งาน deprecated ของ programming element บางอย่าง เช่น keyword/function/class เป็นต้น

Warning messages ถูกแสดงโดยฟังก์ชัน warn() ที่กำหนดในโมดูล 'warning' ของ standard library ของ Python Warning จริงๆ แล้วเป็น subclass ของ Exception ใน built-in class hierarchy มี built-in Warning subclasses หลายตัว User defined subclass ก็สามารถกำหนดได้เช่นกัน

Warning
 นี่คือ base class ของ warning category classes ทั้งหมด
UserWarning
 Default category สำหรับ warn()
DeprecationWarning
 คำเตือนเกี่ยวกับฟีเจอร์ที่ deprecated เมื่อคำเตือนเหล่านี้มีเจตนาสำหรับ developers
SyntaxWarning
 คำเตือนเกี่ยวกับ syntactic features ที่น่าสงสัย
RuntimeWarning
คำเตือนเกี่ยวกับ runtime features ที่น่าสงสัย
FutureWarning
 คำเตือนเกี่ยวกับฟีเจอร์ที่ deprecated เมื่อคำเตือนเหล่านี้มีเจตนาสำหรับ end users
PendingDeprecationWarning
คำเตือนเกี่ยวกับฟีเจอร์ที่จะ deprecated ในอนาคต
ImportWarning
 คำเตือนที่ triggered ระหว่างกระบวนการ import โมดูล
UnicodeWarning
คำเตือนเกี่ยวกับ Unicode
BytesWarning
 คำเตือนเกี่ยวกับ bytes และ byte array
ResourceWarning
 คำเตือนเกี่ยวกับ resource usage

ตัวอย่าง Warning

โค้ดต่อไปนี้กำหนดคลาสที่มีเมธอด deprecated และเมธอดที่กำหนดจะ deprecated ในเวอร์ชันอนาคตของคลาสนั้น

# warningexample.py import warnings class WarnExample: def __init__(self): self.text = "Warning" def method1(self): warnings.warn( "method1 is deprecated, use new_method instead", DeprecationWarning ) print ('method1', len(self.text)) def method2(self): warnings.warn( "method2 will be deprecated in version 2, use new_method instead", PendingDeprecationWarning ) print ('method2', len(self.text)) def new_method(self): print ('new method', len(self.text)) if __name__=='__main__': e = WarnExample() e.method1() e.method2() e.new_method()

หากสคริปต์ข้างต้นถูกรันจาก command prompt เป็น

E:\python37>python warningexample.py

ไม่มี warning messages แสดงบน terminal สำหรับนั้นคุณต้องใช้ switch -Wd ดังนี้

E:\python37>python -Wd warningexample.py warningexample.py:10: DeprecationWarning: method1 is deprecated, use new_method instead DeprecationWarning method1 7 warningexample.py:19: PendingDeprecationWarning: method2 will be deprecated in version 2, use new_method instead PendingDeprecationWarning method2 7 new method 7

เช่นกัน interactive session ต่อไปนี้ก็ไม่แสดง warning messages ใดๆ

E:\python37>python >>> from warningexample import WarnExample >>> e = WarnExample() >>> e.method1() method1 7 >>> e.method2() method2 7 >>> e.new_method() new method 7

คุณต้องเริ่ม Python session ด้วย -Wd

E:\python37>python -Wd >>> from warningexample import WarnExample >>> e=WarnExample() >>> e.method1() E:\python37\warningexample.py:10: DeprecationWarning: method1 is deprecated, use new_method instead DeprecationWarning method1 7 >>> e.method2() E:\python37\warningexample.py:17: PendingDeprecationWarning: method2 will be deprecated in version 2, use new_method instead PendingDeprecationWarning method2 7 >>> e.new_method() new method 7

Warnings Filters (ตัวกรองคำเตือน)

Warnings filter ควบคุมว่า warnings ถูก ignore, displayed, หรือเปลี่ยนเป็น errors (ยก exception)

Action
Meaning
error
เปลี่ยน warning เป็น exception
ignore
ทิ้ง warning
always
ปล่อย warning เสมอ
default
พิมพ์ warning ครั้งแรกที่เกิดจากแต่ละ location
module
พิมพ์ warning ครั้งแรกที่เกิดจากแต่ละ module
once
พิมพ์ warning ครั้งแรกที่เกิด

Interactive session ต่อไปนี้ตั้ง filter เป็น default ด้วยฟังก์ชัน simplefilter()

E:\python37>python >>> import warnings >>> warnings.simplefilter('default') >>> from warningexample import WarnExample >>> e=WarnExample() >>> e.method1() E:\python37\warningexample.py:10: DeprecationWarning: method1 is deprecated, use new_method instead DeprecationWarning method1 7 >>> e.method2() E:\python37\warningexample.py:17: PendingDeprecationWarning: method2 will be deprecated in version 2, use new_method instead PendingDeprecationWarning method2 7 >>> e.new_method() new method 7

เพื่อปิด warning ชั่วคราว ตั้ง simplefilter เป็น 'ignore'

import warnings def function(): warnings.warn("deprecated", DeprecationWarning) with warnings.catch_warnings(): warnings.simplefilter("ignore") function()