Skip to content

Instantly share code, notes, and snippets.

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]
if not line:
raise Exception("couldn't figure out the line number")
import linecache
lines = linecache.updatecache(file, None)
for n in range(line - 10, line + 10):
if n == line:
prefix = '{}: =>'.format(n)
prefix = '{}: '.format(n)
print(prefix + linecache.getline(__file__, n).replace('\n', ''))

This comment has been minimized.

Copy link

@mager mager commented Sep 19, 2018


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