Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pry's `whereami` implemented for IPython
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', ''))
@mager

This comment has been minimized.

Copy link

@mager mager commented Sep 19, 2018

Awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment