Created
May 20, 2017 06:35
-
-
Save JackDanger/aaee4c1dd82b7ca02a74836095f19813 to your computer and use it in GitHub Desktop.
Pry's `whereami` implemented for IPython
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
def whereami(): | |
import traceback | |
for stack_frame in traceback.extract_stack(): | |
if stack_frame[3] == 'from IPython.terminal.embed import embed; embed()': | |
line = stack_frame[1] | |
file = stack_frame[0] | |
break | |
if not line: | |
raise Exception("couldn't figure out the line number") | |
import linecache | |
lines = linecache.updatecache(file, None) | |
print(file) | |
for n in range(line - 10, line + 10): | |
if n == line: | |
prefix = '{}: =>'.format(n) | |
else: | |
prefix = '{}: '.format(n) | |
print(prefix + linecache.getline(__file__, n).replace('\n', '')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome