Skip to content

Instantly share code, notes, and snippets.

@Keating950
Created March 7, 2020 01:57
Show Gist options
  • Save Keating950/1f6adf79cfa483233f82aef210e36051 to your computer and use it in GitHub Desktop.
Save Keating950/1f6adf79cfa483233f82aef210e36051 to your computer and use it in GitHub Desktop.
python function for debugging from https://andyljones.com/posts/post-mortem-plotting.html
def extract():
"""Copies the caller's environment up to your IPython session"""
import inspect
import ctypes
frames = inspect.stack()
caller = frames[1].frame
name, ls, gs = caller.f_code.co_name, caller.f_locals, caller.f_globals
ipython = [f for f in inspect.stack() if f.filename.startswith('<ipython-input')][-1].frame
ipython.f_locals.update({k: v for k, v in gs.items() if k[:2] != '__'})
ipython.f_locals.update({k: v for k, v in ls.items() if k[:2] != '__'})
ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(ipython), ctypes.c_int(0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment