က်ြန္ေတာ္တုိ့ဒီေန့ေတာ့ python programတစ္ပုဒ္မွာ error ေတြ ဘယ္လုိ debug လုပ္သင့္တယ္ဆုိတဲ့ နည္းေလးေတြကုိျပေပးသြားပါမယ္။က်ြန္ေတာ္တုိ့ အျမဲ codeေတြေရးၾကတယ္ ERROR ၾကည့္ခ်င္ရင္ print() နဲ့ထုတ္ၾကည့္ၾကတာမ်ားတယ္ စေလ့လာတဲ့အခါမွာ။တကယ္ေတာ့ မေကာင္းတဲ့ အက်င့္ထဲမွာ ပါတယ္လုိ့လဲ ေျပာလုိ့မရပါဘူး။ဒါေပမဲ့ ဒီလုိေလးေတြ လုပ္မယ္ဆုိ ပိုေကာင္းတယ္လုိ့ ေျပာခ်င္တာပါ။
အဲ့ေတာ့ က်ြန္ေတာ္တုိ့စလုိက္ရေအာင္
Logging
က်ြန္ေတာ္တုိ့ logging ကေတာ့ မ်ိုးစုံသုံးလုိ့ရပါတယ္။ဒီမွာေတာ့ Error ျပခ်င္တဲ့အခါ ဘယ္လုိသုံးမလဲဆုိတာကုိ ေလ့လာရေအာင္။
1 2 3 4 5 6 7 |
import logging logging.debug('This is a debug message') logging.info('This is an info message') logging.warning('This is a warning message') logging.error('This is an error message') logging.critical('This is a critical message') |
က်ြန္ေတာ္တုိ့ logging မွာေတာ့ message အမ်ိုဳးမ်ိဳးထုတ္လုိ့ရတယ္ဆုိတာ အေပါ္က example ကုိၾကည့္ရင္ေတြ့မွာပါ။အဲ့ေတာ့ က်ြန္ေတာ္တုိ့ error message ေတြကုိ ေတာ့ logging.error ကုိသုံးျပီးျပပါတယ္။
က်ြန္ေတာ္တုိ့ logging ရဲ့အားသာခ်က္က configuration လုပ္လုိ့ရျပီး error message ေတြကုိေပါ္ေစခ်င္သလုိျဖစ္ေစ file ထဲကုိ ေရးထည့္တာမ်ိဳးကုိ အလြယ္လုပ္လုိ့ရတာပါပဲ။logging ရဲံ config syntax ကိုၾကည့္မယ္ဆုိေအာက္ကတုိင္းပါပဲ။
1 2 3 4 |
import logging logging.basicConfig(level=logging.DEBUG,format='Error is-%(message)s') logging.debug("I am Error") |
logging.basicConfig() ထဲမွာ က်ြန္ေတာ္တုိ့ configuration ခ်ခ်င္တာေတြကုိထည့္ေပးက်ပါတယ္။ဒီေနရာမွာေတာ့ levelနဲ့format ကေတာ့ ေပါ္ခ်င္တဲ့ပုံစံပါ။အေပါ္က exampleကိုrunလုိက္မယ္ဆုိ
1 2 |
$ python debugging.py Error is-I am Error |
format ခ်ထားတာေၾကာင့္ အဲ့လုိပုံစံေလးေပါ္လာပါတယ္။ဒါဆုိက်ြန္ေတာ္ေျပာထားတဲ့ fileထဲကုိေရးထည့္မယ္ဆုိတဲ့ကိစလာပါျပီ။
1 2 3 4 5 |
import logging logging.basicConfig(filename='errorlog.txt',level=logging.DEBUG, format='%(asctime)s-%(levelname)s-Error is-%(message)s') logging.debug("I am Error") |
က်ြန္ေတ္ာတုိ့ filename ေလး သတ္မွတ္ေပးလိုက္ယံုပါပဲ။ဒါေလးပါပဲ။က်ြန္ေတ္ာတုိ့ output file ကိုၾကည့္လုိက္မယ္ဆုိရင္ေတာ့
1 2 3 |
$ python debugging.py $ cat errorlog.txt 2018-09-14 21:14:31,063-DEBUG-Error is-I am Error |
က်ြန္ေတာ္တုိ့ exception handler ေတြနဲ့ေရးမယ္ဆုိလဲ
1 2 3 4 5 6 7 8 9 10 |
import logging filename = "notexists.txt" try: with open(filename,'r') as f: print(f.read()) except Exception as e: logging.error("Error Occur",exc_info=True) |
က်ြန္ေတ္ာတုိ့မရွိတဲ့ filename ကိုေရးထည့္ျပီး ဖတ္ၾကည့္ပါတယ္။ေသခ်ာError တတ္မွာပါ။ဘယ္လုိသုံးလုိ့ရတယ္ဆုိတာေလးျပခ်င္လုိ့ပါ။
1 2 3 4 5 6 |
$ python debugging.py ERROR:root:Error Occur Traceback (most recent call last): File "debugging.py", line 6, in <module> with open(filename,'r') as f: FileNotFoundError: [Errno 2] No such file or directory: 'notexists.txt' |
က်ြန္ေတာ္တုိ့ run လုိက္ေတာ့ အေပါ္ကတုိင္းေတြ့ရမွာပါ။ဆက္ျပီးတျခားပုံစံေတြေလ့လာရေအာင္။
Raise KeyWord
raise keyword သုံးတာကေတာ့ ရုိးရွင္းပါတယ္။ေရးပုံကေတာ့
1 |
raise Exception('Error!') |
ဒါမ်ိဳးပုံစံေလးပါပဲ။Example အေနနဲ့ေတာ့
1 2 3 4 5 6 7 8 |
filename = "notexists.txt" try: with open(filename,'r') as f: print(f.read()) except Exception as e: raise |
က်ြန္ေတာ္တုိ့ ေအာက္ဆုံးက raise လုိ့ေရးေပးလုိက္တာနဲ့ အေပါ္က exception ကုိထုတ္ျပပါတယ္။outputကေတာ့
1 2 3 4 5 |
$ python debugging.py Traceback (most recent call last): File "debugging.py", line 4, in <module> with open(filename,'r') as f: FileNotFoundError: [Errno 2] No such file or directory: 'notexists.txt' |
handler မသုံးဘူးဆုိလဲ ရုိးရုိးေလးေရးယံုပါပဲ။
1 2 3 4 5 6 7 8 9 |
i = 4 if i > 3: raise Exception("Input Error!") $ python debugging.py Traceback (most recent call last): File "debugging.py", line 4, in <module> raise Exception("Input Error!") Exception: Input Error! |
Assert
က်ြန္ေတာ္တုိ့ assert ကိုေတာ့ true false ပုံစံမ်ိုးနဲ့ စစ္ျပီးသုံးၾကပါတယ္။ဥပမာ မိတ္ေဆြရဲ့ programတစ္ခုမွာ စားတဲ့ function ရွိတယ္ဆုိပါေတာ့စားတဲ့ေကာင္ ပုိင္းေျခက zeroျဖစ္လုိ့မရတာမ်ိဳး အဲ့အခါက် zero ျဖစ္ေနရင္ error ထုတ္ျပျပီး zeroမျဖစ္မွဆက္လုပ္ေစတဲ့ပုံစံမ်ိုးေတြမွာသုံးပါတယ္။
assert ကေတာ့ ေအာက္ပါတုိင္းေရးပါတယ္
1 |
assert <condition>,<error message> |
assert က conditionပဲေရးလုိ့ရေပမယ့္ error messageကိုထည့္ေစခ်င္လုိ့ ျပလုိက္တာပါ။က်ြန္ေတာ္တုိ့ ၆ဘာသာေပါင္းေပးတဲ့ function ေလးေရးၾကည့္ရေအာင္
1 2 3 4 5 6 |
def sum_marks(marks): assert len(marks) == 6,"length must be 6" return sum(marks) print(sum_marks([40,40,80,80])) |
အဲ့ေတာ့ len(marks) == 6 ဆုိတဲ့ condition သာမေျပလည္ဘူးဆုိ ေနာက္က Error message ကုိထုတ္ျပမွာပါ။ဒီမွာ က်ြန္ေတာ္တုိ့ list မွာ ၄ဘာသာ ပဲထည့္ထားတာေတြ့မွာပါ။အဲ့ေတာ့ Error ကတတ္မွာပါပဲ။
len(marks) == 6
1 2 3 4 5 6 7 |
$ python debugging.py Traceback (most recent call last): File "debugging.py", line 6, in <module> print(sum_marks([40,40,80,80])) File "debugging.py", line 2, in sum_marks assert len(marks) == 6,"length must be 6" AssertionError: length must be 6 |
အကယ္လုိ့ ၆ဘာသာလုံးထည္ေ့ပးလုိက္မယ္ဆုိရင္ေတာ့ ဆက္ျပီးအလုပ္လုပ္ေစပါတယ္။
1 2 3 4 5 6 |
def sum_marks(marks): assert len(marks) == 6,"length must be 6" return sum(marks) print(sum_marks([40,40,80,80,50,50])) |
Output
1 2 |
$ python debugging.py 340 |
က်ြန္ေတာ္ အားလုံးကုိနားလည္မယ္လို့ ယံုၾကည္ပါတယ္။ေက်းဇူးတင္ပါတယ္။