Created
February 23, 2015 16:51
-
-
Save seumasmorrison/fa12952a3ab8a68e0267 to your computer and use it in GitHub Desktop.
Neat function for exporting variables from python debugger session. Source - http://stackoverflow.com/questions/18124336/save-variable-to-interactive-namespace-from-python-debugger/18125590#18125590
This file contains 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 sys | |
# 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