Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save a function definition in ipython

When using ipython I often want to save specific functions I have defined during my session, e.g.:

In [1]: def func1():
...:        pass
...: 

In [2]: %save func1.py func1
func1 is neither a string nor a macro.

Instead I have to pick out the function definition line number from enumerate(_ih), or manually copy and paste from vim if I have %edit'd the function.

Is there a way to achieve %save func1.py func1? I feel like it should be possible as ipython has access to the definition when using %edit.

Edit

Line based saving doesn't work if I have at some point edited the function with %ed. I'm looking for a way to save the new function definition in this case.

like image 860
cmh Avatar asked Apr 16 '12 15:04

cmh


People also ask

How do you save a function in Jupyter notebook?

There is a disk icon in the upper left of the Jupyter tool bar. Click the save icon and your notebook edits are saved. It's important to realize that you will only be saving edits you've made to the text sections and to the coding windows. You will NOT be saving the results of running the code.

What does %% Writefile mean?

%%writefile lets you output code developed in a Notebook to a Python module. The sys library connects a Python program to the system it is running on. The list sys. argv contains the command-line arguments that a program was run with.

What does %% capture do in Jupyter?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.


2 Answers

You can save the contents of line 1 with %save using:

In [2]: %save func1.py 1
The following commands were written to file `func1.py`:
def func1():
    pass

Help on %save is available with:

In [2]: %save?
Type:       Magic function
...
Docstring:
Save a set of lines or a macro to a given filename.

Usage:
  %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...

You can %edit a function body with:

In [3] %edit func1
 done. Executing edited code...

After %edit-ing your function func1, you can get the following output from IPython using _:

In [4]: _
Out[4]: 'def func1():\n    print "Hello World"\n\n'

Then, you can define or re-define a %macro with the update func1 contents like this:

In [5]: %macro new_func1_macro _
Macro `new_func1_macro` created. To execute, type its name (without quotes).
=== Macro contents: ===
def func1():
    print "Hello World"

Finally, you can save the new version of func1 with %save and the new %macro like this:

In [6]: %save func1.py new_func1_macro
The following commands were written to file `func1.py`:
def func1():
    print "Hello World"

Hope that clarifies.

like image 82
cfedermann Avatar answered Oct 04 '22 00:10

cfedermann


 In [1]: def a():
   ...:     print('hello')
   ...:     return

In [2]: from inspect import getsource

In [3]: %save a.py getsource(a)

The following commands were written to file `a.py`:
def a():
    print('hello')
    return
like image 45
nadapez Avatar answered Oct 04 '22 00:10

nadapez