Skip to content

Instantly share code, notes, and snippets.

@rs2
Last active May 17, 2018 20:10
Show Gist options
  • Save rs2/303684b2e98e09b085b2f2c8ffc23f29 to your computer and use it in GitHub Desktop.
Save rs2/303684b2e98e09b085b2f2c8ffc23f29 to your computer and use it in GitHub Desktop.
PyDojoE9S9
import sitecustomize
foobar = 42
print(foo)
# Prints
# name 'foo' is not defined
# Did you mean foobar?
import sys
import re
RX = re.compile(r"name \'(?P<variable>\w+)\' is not defined")
original_hook = sys.excepthook
def custom_exception_handler(etype, value, tb):
if issubclass(etype, NameError):
print(value)
name = RX.match(str(value)).groupdict()['variable']
for sym in tb.tb_frame.f_globals:
if sym.startswith(name):
print("Did you mean {}?".format(sym))
return
original_hook(etype, value, tb)
def install_custom_hook():
sys.excepthook = custom_exception_handler
install_custom_hook()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment