Table of Contents
Macro library
Macros can be stored persistently by using %store. Here's how.
[~]|12> cd /tmp/test
[tmp\test]|13> ls
Volume in drive C has no label.
Volume Serial Number is 54BF-AFD0
Directory of c:\tmp\test
18.01.2006 16:38 <DIR> .
18.01.2006 16:38 <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 11 366 074 880 bytes free
[tmp\test]|14> %macro listing 12-13
Macro `listing` created. To execute, type its name (without quotes).
Macro contents:
ipmagic("cd /tmp/test")
ipalias("ls ")
[tmp\test]|15> %store listing
Stored 'listing' (93 bytes)
Now we have macre named "listing" residing in the user namespace every time IPython is started. However, if you have lots of macros there are possibly too many names in the default namespace, so you might want to reorganize a little bit and move all the macros under a "mac" structure, so you can call them by doing e.g. mac.listing.
[tmp\test]|16> import IPython.ipstruct
[tmp\test]|17> mac = IPython.ipstruct.Struct()
[tmp\test]|18> mac
<18> Struct({})
(Struct is like the already classic "Bunch", http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308 )
Now you can add the macros to the newly created Struct object:
[tmp\test]|19> mac.listing = listing [tmp\test]|20> %store mac Stored 'mac' (143 bytes)
And voila', the macro is stored under the "mac" struct persistently:
[opt\environmentswitch]|1> mac.listing
<1> Executing Macro...
Volume in drive C has no label.
Volume Serial Number is 54BF-AFD0
Directory of C:\tmp\test
18.01.2006 16:38 <DIR> .
18.01.2006 16:38 <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 10 997 104 128 bytes free
You might want to remove the "listing" name from the immediate user namespace now:
[opt\environmentswitch]|2> %store -d listing
Note that the same idea can be applied to non-macro variables as well - consider organizing project specific variables (release directory, changelog etc.) under different Struct objects if you are working with multiple projects:
> jed $prj1.changelog > cp release1.tar.gz $prj2.releasedir
Remember to %store the hierarchical structure (e.g. "%store mac") every time you make modifications!
(submitted by vivainio)
Using gvim in server mode
Noted by Ryan Krauss on the list: if you want to use gvim in server mode so that ipython communicates with it, you can define your own fix_editor_hook (saved in a file in $PYTHONPATH called ipythonhooks.py):
import os def fix_error_editor(self,filename,linenum,column,msg): cmd='gvim --servername ipython --remote +%d %s'% (linenum,filename,) os.system(cmd)
call this code to set the hook:
import ipythonhooks ip_set_hook('fix_error_editor',ipythonhooks.fix_error_editor)
Start gvim with the server name ipython:
gvim --servername ipython
and then ipython and vim play nice together, ipython locates the file and line number in the already open gvim.
You can also set this in your .bashrc file to make starting vim easier: alias ipvim="gvim --servername ipython" (or the equivalent .tcshrc syntax).
Defining prompts via hooks
The following example shows how to define input and output prompts via hooks:
import os from IPython import ipapi, Prompts def myinputprompt(self, cont): ip = self.api count = str(len(ip.user_ns["_ih"])) colors = Prompts.PromptColors[""].colors pwd = os.getcwd() if cont: return "%s%s%s: " % ( \ colors.in_prompt2, "."*(4+len(pwd)+1+len(count)+1), colors.normal) else: return "%sIn [%s|%s%s%s]: %s" % ( \ colors.in_prompt, pwd, colors.out_number, count, colors.in_prompt, colors.normal) def myoutputprompt(self): ip = self.api count = str(len(ip.user_ns["_ih"])) colors = Prompts.PromptColors[""].colors pwd = os.getcwd() prompt = "%sOut[%s|%s%s%s]: %s" % ( \ colors.out_prompt, pwd, colors.out_number, count, colors.out_prompt, colors.normal) return prompt ipapi.get().set_hook("generate_prompt", myinputprompt) ipapi.get().set_hook("generate_output_prompt", myoutputprompt)
Using ipipe
ipipe is a handy extension contributed by Walter Dörwald. It allows "piping" generators to produce shell pipeline like behaviour with python data.
- See Using ipipe for information about how to activate and use ipipe.
- See Support for ipipe for information about how to add support for ipipe to your own classes.
Persistent aliases
Like macros, aliases can be %store'd. Writing a simple alias can be a good alternative to writing a trivial script; here I introduce an alias called 'publish' to copy the specified folder to a "publishing area" where others can acces it, and make the alias available in subsequent sessions:
> alias publish cp -r %s n:/Public/Files/pub > %store publish
Now, after %Exit and restarting the ipython session, "publish" remains available. Also note the use of '%s' as an alias argument placeholder.
