Skip to content

Instantly share code, notes, and snippets.

@JackDanger
Created May 20, 2017 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackDanger/aaee4c1dd82b7ca02a74836095f19813 to your computer and use it in GitHub Desktop.
Save JackDanger/aaee4c1dd82b7ca02a74836095f19813 to your computer and use it in GitHub Desktop.
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
Copy link

mager commented Sep 19, 2018

Awesome

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