Tuesday, April 1, 2014

[Level 1] How to auto restore when start iPython

iPython have a magic command call "alias". This command just command "alias" in unix shell could help you to create alias command in iPython environment. How could we save the alias that we created before, you could use "store" magic command. The alias would be saved in iPython internal db. ex.
In [1]: %alias ipython_alias echo 'hello world'
In [2]: %store ipython_alias
After you exit iPython and restart it again, you need to restore the aliases from internal db.
In [1]: %store -r
And how could we make iPython auto-restore when we launch it? You could modify ipython_config.py in profile. ex. If you use default profile
$> cat /home/stanley/.config/ipython/profile_default/ipython_config.py 
# StoreMagics configuration
c.StoreMagics.autorestore = True # uncomment this line and assign autorestore as 'True'
You could also create a script to add alias automatically.
## Add often use commands.
security_commands = 'chmod chown scp ssh sudo'
network_commands = 'ifcofig netstat ping route'
system_commands = 'iostat ps top vmstat'
common_commands = 'cat grep head nohup tail'
editor_commands = 'meld soffice vi'
sourcecode_commands = 'apt-cache apt-get easy_install git p4 pip'
## prepare command list
command_list = ' '.join([security_commands, network_commands, system_commands, common_commands, editor_commands, sourcecode_commands])
for cmd in command_list.split(' '):
    %alias $cmd $cmd
    %store $cmd

Wish this helps.

Stanley Huang