[IPython-user] alias to magic function

Kuznetsov Valentin vkuznet@gmail....
Wed Oct 15 14:38:03 CDT 2008

I face out with a problem I can't find anywhere on a web. In short I  
want to have an alias to my magic function with one predefined  
parameters but also with ability to place other parameters. Let me  
explain on example.

I created a magic functions 'email' and 'blog'. Both of them can take  
as a parameter "list", e.g. 'email list' should list emails, and 'blog  
list' should list blogs. I also want to provide a users "a feeling"  
that once they type email, "they will be in email". It's easy to  
implement by changing prompt. For example

In [1]: email
email >

email > blog
blog >

Now, what if I want to have a function 'list' for both of them. I  
would rather defined it as a part of my magic function 'email' and  
'blob'. So if I'm in "email session" the list will list emails, while  
if I'm in "blog session" the list will list my blogs. It' rather easy  
to define structure of 'email' and 'blog' magic functions which will  
to such actions. So if I do

In [1]: email list # it will list emails
In [1]: blog  list # it will list blogs

But let me back to my example. If I'll trick users and get them  
felling of session then it would be nice to define "session  
functions", that users will do

email> list # which internally will invoke 'email list'
email> list 1 # which will internally invoke 'email list 1'

and similar to blog

blog> list # will internally invoke 'blog list'
blog> list 1 # will internally invoke 'blog list 1'

So this brings me to the question. Can I define an alias 'list' to  
magic function, but in a such a way that I can pass my parameters as  
well. A simple %alias list email list %s doesn't work since it will  
call 'email' system function, but %alias list %email list %s doesn't  
work either, I got plenty of exception.

If someone knows how to resolve this case I would appreciate any  
feedback, scenario, code suggestions.
Thank you,

More information about the IPython-user mailing list