[IPython-user] Nifty %store...

Fernando Perez Fernando.Perez at colorado.edu
Thu Dec 29 14:04:12 CST 2005


Hi all,

thanks to Matt Wilkie and Ville's contribution, now we have the new neat 
%store mechanism for lightweight persistence.  I'm in the process of debugging 
a tricky problem with descriptors, and it just came in very handy.  I figured 
it would be worth illustrating this little use:

You can save macros for later reuse effortlessly.  Here's how.  In one 
session, I had run some code to test a problem, this was my history:

In [7]: hist
1: ipmagic("%runlog descbug.py")
2: m
3: ipalias("cat descbug.py")
4: import descriptors
5: m = descriptors.watched()
6: m.x

I needed to re-execute the last 3 lines over and over, but I'm restarting 
ipython (because I'm changing ipython itself).  Make a macro for it, call it 'db':

In [8]: macro db 4:7
Macro `db` created. To execute, type its name (without quotes).
Macro contents:
['import descriptors\nm = descriptors.watched()\nm.x\n']

and now simply store the macro:

In [9]: store db
Stored 'db' (167 bytes)


Now I quit, pound on ipython for a while, and restart it:

abdul[desc-bug]> ip
In [1]: db
Out[1]: Executing Macro...
Getting <descriptors.report_access object at 0x403b858c> <descriptors.watched 
object at 0x403b808c> <class 'descriptors.watched'>
5
Getting <descriptors.report_access object at 0x403b858c> <descriptors.watched 
object at 0x403b808c> <class 'descriptors.watched'>
6
Out[1]: [1, 2, 3]


My macro is nicely sitting there, and it's just as if I'd retyped those three 
lines myself.  Very, very handy.

Thanks again, Matt and Ville!

Best,

f




More information about the IPython-user mailing list