Skip to content

Instantly share code, notes, and snippets.

@yarko
Last active October 6, 2017 18:43
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 yarko/bdaa9d3178a6db03e160fdbabb3a9885 to your computer and use it in GitHub Desktop.
Save yarko/bdaa9d3178a6db03e160fdbabb3a9885 to your computer and use it in GitHub Desktop.
conditional break for ipython terminal (**Use:** `if condition: breakpoint` instead -- **don't** use this.)
# This is for ipython interactive
# - you can, for example, place this in
# ~/.ipython/profile_default/startup
def breakpoint_(condition=True):
'''
in ipython:
- breakpoint_() <= break always
- breakpoint_(False) <= never break
- breakpoint_(condition) <= break if True
'''
# import here, to limit
# to this function's namespace
from sys import _getframe
from IPython.terminal import debugger
if condition:
debugger.set_trace(_getframe().f_back)
return debugger
@yarko
Copy link
Author

yarko commented Jun 19, 2017

Note: Don't use this - there's no need for it.

if condition:
    breakpoint_()

Is just as good. Also, note: breakpoint() coming with PEP 553 in Python 3.7.

This came via digesting @tillahoffmann post on:
ipython/ipython#9940 (comment)
combined with jupyter docs (and newer merge) here:
http://ipython.readthedocs.io/en/stable/api/generated/IPython.terminal.debugger.html

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