Skip to content

Instantly share code, notes, and snippets.

@danielroseman
Last active December 23, 2015 09:39
Show Gist options
  • Save danielroseman/6616262 to your computer and use it in GitHub Desktop.
Save danielroseman/6616262 to your computer and use it in GitHub Desktop.
import re
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.core.debugger import Pdb
ipdb = Pdb()
@magics_class
class StepMagic(Magics):
@line_magic
def step(self, params):
# params might either be
# foo, bar, baz
# or
# foo(bar, baz)
# we need to determine if there is a comma before the opening paren.
comma_pos = params.find(',')
paren_pos = params.find(')')
if comma_pos > -1 and (comma_pos < paren_pos or paren_pos == -1):
param_list = params.split(',')
else:
# Match everything up to first open paren, and everything inside parens.
# We use lazy repetition on the first group to ensure that it copes when
# the expressions within the parens are themselves calls.
match = re.match(r'(.*?)\((.*)\)', params)
if match:
func, args = match.groups()
# TODO: this doesn't work when args consists of a tuple.
param_list = [func] + args.split(',')
else:
# Assume it's a single expression
param_list = [params]
evaluated_params = [self.shell.ev(p) for p in param_list]
ipdb.runcall(*evaluated_params)
_loaded = False
def load_ipython_extension(ip):
"""Load the extension in IPython."""
global _loaded
if not _loaded:
plugin = StepMagic(shell=ip)
ip.register_magics(plugin)
_loaded = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment