Skip to content

Instantly share code, notes, and snippets.

@ties
Created March 29, 2015 09:14
Show Gist options
  • Save ties/93751307e6777579ce10 to your computer and use it in GitHub Desktop.
Save ties/93751307e6777579ce10 to your computer and use it in GitHub Desktop.
import sys
"""
A debug tool for use with ipython/ipdb.
With this tool you can save local variables to the interactive session
which you are working from.
Source: <unknown>
"""
# Be safe and define a maximum of frames we're trying to walk up
MAX_FRAMES = 20
def save_to_interactive(dct):
n = 0
# Walk up the stack looking for '__name__'
# with a value of '__main__' in frame globals
for n in range(MAX_FRAMES):
cur_frame = sys._getframe(n)
name = cur_frame.f_globals.get('__name__')
if name == '__main__':
# Yay - we're in the stack frame of the interactive interpreter!
# So we update its frame globals with the dict containing our data
cur_frame.f_globals.update(dct)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment