Saving Macros in IPython

According to the docs, I would have to define a macro and save it. The macro will then be available the next time the IPython shell is started. But this does not work:

In [4]: print "Foobarbatbizbuzzbonk" Foobarbatbizbuzzbonk In [5]: %macro foo 4 Macro `foo` created. To execute, type its name (without quotes). === Macro contents: === print "Foobarbatbizbuzzbonk" In [6]: %store foo Stored 'foo' (Macro) In [7]: quit() 

When I run the IPython shell again, no macros:

 In [1]: foo --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-d3b07384d113> in <module>() ----> 1 foo NameError: name 'foo' is not defined In [2]: %macro Out[2]: [] 

Does anyone know why this is not working?

+7
ipython
source share
3 answers

I found the answer to this in several obscure places.

First, the README found in $ HOME / .ipython says: "For more information on configuring IPython, run: ipython config -h "

Doing this gives a ton of help, including the following advice:

 To initialize a profile with the default configuration file, do:: $> ipython profile create and start editing `IPYTHONDIR/profile_default/ipython_config.py` 

Good Excellent. Did this. Now, what to add to this file is described in Configuring the application for the ipython command line . But this document does not mention% store.

Finally, I found my answer in the “Configuration and Setup” section under Extensions bundled with IPython, where you will find a storemagic entry that says:

% stores magic to facilitate persistence. Stores variables, aliases, and macros in the IPythons database. Automatically restore saved variables at startup, add this to your ipython_config.py file:

 c.StoreMagic.autorestore = True 

Add this, restart IPython and bang! there are my macros. Cool!

+7
source share

As an alternative to defining a macro in the shell, then saving it, you can simply define the macro in the startup file. For example, you can put the following line in IPYTHONDIR / profile_default / ipython_config.py:

 get_ipython().define_macro('foo','print "Foobarbatbizbuzzbonk"') 

If you want to define a macro for a magic command, you can use something like this:

 get_ipython().define_macro('clr',"""get_ipython().magic(u'%reset -sf')"""); 

I did not understand how to define a macro for a magic command with parameters that will take arguments (for example, type 'rn tmp' instead of '% run -i tmp' to run the tmp.py file in ipython Namespace).

+7
source share

I have to admit that sometimes finding good documentation for ipython is a nightmare (for the basics). It looks like this interactive console was created for a truly programming guru. At least this is what I feel when trying to solve a simple problem, for example, saving a variable for later use ... something more than just in Matlab.

But to answer your question ... try opening ipython, usually just typing ipython and then write% store -r

It should restore your data with the original names. I still don't know how to store variables in files or restore only the variables that I want. BTw, to find the names of your stored variables, enter% store.

Hope this works for you.

+4
source share

All Articles