IPython: Alias which uses a magic function itself

tim

I couldn't find anything about this so Im asking here: I want to create an alias in IPython which itself uses an IPython-magic-function.

That means: I often need to to retype

%run process.py

thus I'd like to create an alias like this:

%alias rp %run process.py

the creation works, but when calling rp, it says, that the command %run could not be found. Any ideas about how to do this?

GuiltyDolphin

Try this:

When you enter the IPython console, type %run process.py

Then you can use the %macro magic to bind rp to %run process.py.

Do so with the In storage of inputs.

%run process.py
%macro rp In[-2]

Should work!


%macro can also be used to cover a range of inputs, using the syntax %macro name range - where range can be an integer or integers in the form start-end.

For instance, if you wanted to time two functions with a single command you can specify the line ranges.

Define functions:

In[20]: def foo(x): return x
In[21]: def bar(x): return x*x

Time functions:

In[22]: %timeit foo(100)
10000000 loops, best of 3: 137 ns per loop
In[23]: %timeit bar(100)
10000000 loops, best of 3: 194 ns per loop

Bind macro to name time_fb:

In[24]: %macro time_fb 22-23

Macro bound:

Macro `time_fb` created. To execute, type its name (without quotes).
=== Macro contents: ===
get_ipython().magic('timeit foo(100)')
get_ipython().magic('timeit bar(100)')

Check it works:

In[25]: time_fb
10000000 loops, best of 3: 135 ns per loop
10000000 loops, best of 3: 192 ns per loop

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Capture the result of an IPython magic function

From Dev

iPython autoreload magic function not found

From Dev

Git alias which uses aliases

From Dev

IPython run magic: How create an alias for "run -i"?

From Dev

IPython run magic: How create an alias for "run -i"?

From Dev

ipython notebook R cell magic as a python function

From Dev

Set parameters of a function which is itself a parameter of a function

From Dev

How to write an ipython alias which executes in python instead of shell?

From Dev

What is the pure Python equivalent to the IPython magic function call %matplotlib inline?

From Dev

IPython custom tab-completion for user magic function

From Dev

IPython custom tab-completion for user magic function

From Dev

How can I bind expression which uses itself?

From Dev

JavaScript function which acts on itself to extend itself with another object

From Dev

Ignore IPython magic in python

From Dev

IPython notebook: How to write cell magic which can access notebook variables?

From Dev

IPython notebook: How to write cell magic which can access notebook variables?

From Dev

Is it possible to declare a function type that uses itself as an argument in C++?

From Dev

calling a jquery function from a button which inside the function itself

From Dev

How to check which function uses a type?

From Dev

Testing a C function which uses file descriptors

From Dev

How to check which function uses a type?

From Dev

Check Constraint Which Uses A Custom Function

From Dev

IPython Alias with '%s' In It

From Dev

Memory address of ipython magic functions

From Dev

Pipe Ipython magic output to a variable?

From Dev

Bash cell magic in IPython script

From Dev

Cannot form new instance of a function which returns itself

From Dev

Integrating a function which itself involves an integral using SciPy

From Dev

How argument is being passed in the function(which itself is an argument) in JavaScript

Related Related

  1. 1

    Capture the result of an IPython magic function

  2. 2

    iPython autoreload magic function not found

  3. 3

    Git alias which uses aliases

  4. 4

    IPython run magic: How create an alias for "run -i"?

  5. 5

    IPython run magic: How create an alias for "run -i"?

  6. 6

    ipython notebook R cell magic as a python function

  7. 7

    Set parameters of a function which is itself a parameter of a function

  8. 8

    How to write an ipython alias which executes in python instead of shell?

  9. 9

    What is the pure Python equivalent to the IPython magic function call %matplotlib inline?

  10. 10

    IPython custom tab-completion for user magic function

  11. 11

    IPython custom tab-completion for user magic function

  12. 12

    How can I bind expression which uses itself?

  13. 13

    JavaScript function which acts on itself to extend itself with another object

  14. 14

    Ignore IPython magic in python

  15. 15

    IPython notebook: How to write cell magic which can access notebook variables?

  16. 16

    IPython notebook: How to write cell magic which can access notebook variables?

  17. 17

    Is it possible to declare a function type that uses itself as an argument in C++?

  18. 18

    calling a jquery function from a button which inside the function itself

  19. 19

    How to check which function uses a type?

  20. 20

    Testing a C function which uses file descriptors

  21. 21

    How to check which function uses a type?

  22. 22

    Check Constraint Which Uses A Custom Function

  23. 23

    IPython Alias with '%s' In It

  24. 24

    Memory address of ipython magic functions

  25. 25

    Pipe Ipython magic output to a variable?

  26. 26

    Bash cell magic in IPython script

  27. 27

    Cannot form new instance of a function which returns itself

  28. 28

    Integrating a function which itself involves an integral using SciPy

  29. 29

    How argument is being passed in the function(which itself is an argument) in JavaScript

HotTag

Archive