[IPython-dev] Changing functions in a running program?

Wes Turner wes.turner at gmail.com
Mon Jan 14 11:02:38 EST 2019

There are likely more convenient patterns for a functionally composed
signal processing pipeline; though ipdb may be good enough.
https://pypi.org/project/pdbpp/ supports {tab-completion,}

Decomposing to functions that accept state with a standard interface like
{data: [], kwargs: {}} may have advantages.
You can assign the output of one step of the pipeline to a global; or, more
ideally, a key of a dict (or an attribute of a class instance); so you
don't need to modify globals() from a different scope.

The Jupyter notebook way to do this would be to put the first processing
stage in one cell, and then work with it in the next. e g. Spyder and
VSCode support markers in Python sources so that you can execute a
top-level block of code at a time.

I'm not too familiar with signal processing. Someone has likely already
distilled the workflow into some standard interfaces like

/q="scikit signal"
- scipy.signal
- scikit-signal

/q="python signal processing"
- http://greenteapress.com/thinkdsp/html/
- https://github.com/unpingco/Python-for-Signal-Processing (ipynb)

# This logs IPython input and output to a file;
# but not ipdb i/o AFAIU
%logstart -o example.py
%logstart -h

To avoid logging code that modifies globals(),
it's probably better and more convenient to pass print_name as an argument
to name():

def name(print_name=print_name):

With a config dict/object:

def name(conf):
    print_name = conf['print_name']

On Monday, January 14, 2019, Andreas Yankopolus <andreas at yank.to> wrote:

> Thomas,
> If you define a function or variable at the breakpoint, it's
> probably making it a local variable inside main(), so it's not in scope for
> name().
> You may be able to get round this by defining the new function and
> then explicitly making it a global variable, something like this:
> globals()['print_name'] = print_name
> Not exactly elegant, but hopefully it works.
> Yes—makes sense and does the trick! I’ll have to figure out an Emacs hook
> to automatically update globals in that manner. My output:
> In main.
> > /Users/ayank/Documents/programming/python/bar.py(13)main()
>      12     import ipdb; ipdb.set_trace()
> ---> 13     name()
>      14
> ipdb> name()
> Alice
> ipdb> !def print_name(): print ("Bob")
> ipdb> name()
> Alice
> ipdb> globals()['print_name'] = print_name
> ipdb> name()
> Bob
> ipdb>
> To follow up on Wes’s comment regarding TDD—I’m writing signal processing
> code and using Python + SciPy like Matlab. There are some calculations when
> the code starts that take a few minutes. I have a breakpoint there and am
> experimenting with the next steps of the processing chain.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20190114/e61d86ef/attachment.html>

More information about the IPython-dev mailing list