-
Notifications
You must be signed in to change notification settings - Fork 4
π Customize your bug
Please check the colab documentation as for more intuitive play with this feature
Bugs are, terrifying things, to be exposed to your students, especially the ones not familiar with python.
But to fix bugs, first we have to conquer the fear. The fear over its complexity and verbosity
Here we try to offer a smooth experience over frequently met bugs, as this can be configured freely, even outside of this library
from unpackai.bug import ishell, BUGBOOK, itb
If you ran this, you'll have different experience encountering bugs:
- There will be download report button, so you can send your Mentor/TA a detailed report
- The error prompt will be more friendly structured, it will show message from unpackai
- Still shows the original traceback, if needed
# configure the bug mapping using error type name as the key (case sensitive)
BUGBOOK["KeyError"] = f"KeyError usually means there is no such key in a dictionary, but also can mean there is no such column the dataframe table"
Now try run the following to see an error
import pandas as pd
pd.DataFrame({"week":[1,2,3]})["weeks"]
Check the UnpackAI Tips
in return , congrats on your first reconfigured error report!
Certainly we can be much more smarter than that, we can try to customize the message text according to the error detail.
Instead of setting a string to
BUGBOOK
, we can set a function, that will takeetype
,evalue
,tb
as inputs, but return a string message
def customized_keyerror(etype, evalue, tb):
# create structured traceback
stb = itb.structured_traceback(etype, evalue, tb)
# string format of trace back
sstb = itb.stb2text(stb)
# if this is a pandas keyerror?
if 'pandas' in sstb:
key = str(evalue)
return f"""
There's no <strong style="font-weight: bold; color: var(--ansi-red);">column {key}</strong> in the dataframe
<br>
<p>Try to...</p>
"""
else:
return f"There's no key {key} in the dictionary"
BUGBOOK["KeyError"] = customized_keyerror
Hence we got our flexibility, check the UnpackAI Tips
again
pd.DataFrame({"week":[1,2,3]})["weeks"]
Notice the input arguments of the function, it will always be
- etype: error type,
etype.__name__=='KeyError'
- evalue: default error message, in the above case
str(evalue)=='weeks'
- tb: traceback, in the above case
itb.structured_traceback(etype, evalue, tb).stb2text(stb)
will be the entire traceback text
The return of the function will always be a string, it's actually a HTML string to render. So images, js, widgets, you name it...
With this flexibility, your own imagination will be the limit
If you're developing unpackai library, eg. if you are developing nlp module
If within the NLP module, certain error can only leads to certain solution under this subject
You can put the following under unpackai/nlp/__init__.py
:
from unpackai.bug import BUGBOOK, ishell
def customized_file_notfound(et, ev, tb):
if 'gdrive' in str(ev):
return f"""
Please check if the google drive is mounted...
<button>reload google drive</button>"""
elif '.txt' in str(ev):
return f"""
Please check the text data is ...
<button>upload text again</button>"""
else:
return str(ev)
BUGBOOK['FileNotFoundError'] = customized_file_notfound
In such way, the error handling for CV, tabular and NLP can be different and specific
- You can change the bug page by editing this html template
- YOu can change the error report page by editing this html template
Well, if the above things didn't buffle you, let's not stop here
Now you know this format to set new configuration
BUGBOOK['FileNotFoundError'] = customized_file_notfound
But for the key of configuration FileNotFoundError
, for now is a string to filter the error, means we do not apply this rule to other error, only the FileNotFoundError
This key is acting as a filter. The key can also be a function instead of a string
def pandas_filter(et, ev, tb) -> bool:
# create structured traceback
stb = itb.structured_traceback(et, ev, tb)
# string format of trace back
sstb = itb.stb2text(stb)
# return True if the traceback has keyword pandas
# no matter what error type it is
if 'pandas' in sstb:
return True
return False
Now, use the filter function pandas_filter
as the key
BUGBOOK[pandas_filter] = "This is a pandas error, please refer to the <a href='...'>documentation here</a>"
pd.DataFrame({"week":[1,2,3]}).not_a_method()