IPython %magic
Table of content
IPython is a more powerful interactive shell for python.
In this post I’ll explore some of my favorite IPython features.
History ¶
In IPython In
and Out
aren’t just visual
aid. Those are regular python variables:
In [1]: x = 1
In [2]: In
Out[2]: ['', 'x = 1', 'In']
In [3]: Out
Out[3]: {2: ['', 'x = 1', 'In', 'Out']}
In
is a regular python list
.
Out
is a dict
.
Magic functions ¶
IPython has a set of predefined magic functions. Magic functions are
invoked by typing %
or %%
followed by the
magic function name and its arguments. Opposed to python functions,
magic function arguments aren’t surrounded by parentheses and aren’t
separated by commas.
The difference between %
and %%
is how the
arguments are interpreted. With %
only the current line is
interpreted and the command is immediately executed. With
%%
the arguments are expected to be on multiple-lines.
%history magic to generate doctest-ready output ¶
One of my favorite is the %history
magic. It can be used
to generate doctest code. Below is an example:
In [1]: from cr8.misc import parse_table
In [2]: parse_table('users')
Out[2]: ('doc', 'users')
In [3]: parse_table('x.users')
Out[3]: ('x', 'users')
In [4]: %history -op 2-3
>>> parse_table('users')
('doc', 'users')
>>> parse_table('x.users')
('x', 'users')
An alternative to generate doctest-ready output would be to switch
into %doctest_mode
.
%save ¶
Every once in a while I start out in IPython to quickly test something only to realize that things are starting to get longer than I thought. It would have been better to create a file.
Instead of re-typing everything in a file, %save
can be
used to save the history to a file:
%save foo.py 1-10
Where 1-10
is a history-range, specifying which lines
from In
to save.
%edit ¶
Editing multi-line statements in IPython can get troublesome.
%edit
can be used to launch an editor to write expressions.
The expressions will be executed once the editor is closed.
A cool thing about %edit
is how it handles
arguments.
Not only can you pass a filename or a history-range, but also objects. If it is an object IPython will try to locate the file where it was defined and open the editor at that point.
This means it is possible to open functions or even entire modules.
For example: %edit requests
opens
[...]/requests/__init__.py
if the requests module had been
imported.
%timeit ¶
%timeit
uses the timeit module to time the execution of
a statement or expression:
Great for quick benchmarks:
%timeit datetime.now()
1000000 loops, best of 3: 1.2 µs per loop
%timeit datetime.utcnow()
1000000 loops, best of 3: 455 ns per loop
%prun ¶
Runs a statement through the profiler:
%prun 2 * 2
3 function calls in 0.000 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
? and ?? ¶
?
and ??
can be used to get help for an
object:
In [11]: x??
Type: int
String form: 1
Docstring:
int(x=0) -> integer
int(x, base=10) -> integer
Convert a number or string ...
Both work as prefix or suffix. So ??x
works too. They
also work on magic functions. So if you’re wondering what
%doctest_mode
does, you can use
%doctest_mode??
to get more information.