Skip to content

🐞 Customize your bug

Raynard Jon (Zhang,Xiaochen) edited this page Oct 4, 2021 · 6 revisions

Open In Colab

🐞 Customize your bug

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

A friendlier experience

Here we try to offer a smooth experience over frequently met bugs, as this can be configured freely, even outside of this library

Import and deploy the bugbook

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

Simple unified message

# 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!

Simple conditional message output

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"]
Clone this wiki locally