Last active
March 31, 2018 18:59
-
-
Save akuhn/97cbd706316421f2c35e6cf5837e87fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import this module into your script to enable debugging. | |
# Set a function breakpoint. | |
# | |
# class.method = debug.wrap(class.method) | |
# | |
def wrap(function): | |
def wrapper(*args, **quarks): | |
set_trace_with_lazy_import_of_libraries(up=2) | |
return function(*args, **quarks) | |
return wrapper | |
# Sample all arguments of a function. | |
# | |
# Class.method = debug.sample(Class.method) | |
# | |
# Install this at the begin of your script, and then set a breakpoint | |
# at the end of the script and inspect Class.method.samples | |
# | |
# Pro tipp, import omakase for ease of inspection. | |
# | |
def sample(function): | |
def wrapper(*args, **quarks): | |
return_value = function(*args, **quarks) | |
sample = dict(enumerate(args), **quarks) | |
sample['$'] = return_value | |
wrapper.samples.append(sample) | |
return return_value | |
wrapper.samples = [] | |
return wrapper | |
# Sample all arguments of all methods of a class | |
# | |
# debug.sampleall(Class) | |
# | |
# Install this at the begin of your script, and then set a breakpoint | |
# at the end of the script and inspect Class.allsamples | |
# | |
# For example, this will find dead code | |
# | |
# [name for name, samples in Class.allsamples.items() if not samples] | |
# | |
# Pro tipp, import omakase for ease of inspection. | |
# | |
def sampleall(klass): | |
import inspect | |
allsamples = {} | |
for name in dir(klass): | |
method = getattr(klass, name) | |
if not inspect.ismethod(method): continue | |
wrapped_method = sample(method) | |
setattr(klass, name, wrapped_method) | |
allsamples[name] = wrapped_method.samples | |
klass.allsamples = allsamples | |
def breakpoint(value=None, up=1): | |
if breakpoint.enabled: | |
import sys | |
import ipdb | |
frame = sys._getframe() | |
for __ in range(up): frame = frame.f_back | |
# Call sset_trace (starting with double s) rather than set_trace | |
# in order to bypass node's capturing of stdin and stdout! | |
ipdb.sset_trace(frame, context=7) | |
return value | |
def disable_all_breakpoints(): | |
breakpoint.enabled = False | |
# Mangle the function name to enable codesearch of active breakpoints! | |
BUILTIN_NAME = 'd e b u g g e r'[::2] | |
breakpoint.enabled = True | |
breakpoint.disable = disable_all_breakpoints | |
__builtins__[BUILTIN_NAME] = breakpoint |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment