Handy IPython Tips
the magics
I/O
reload modules you just modified on the fly, autoreload:
%load_ext autoreload
%autoreload 2
save object for later use, storemagic:
# for example you got a counter
c = Counter("a b c".split())
# you can save it in IPython's database and quit
%store c
exit()
# when you want your counter back, simply type:
%store -r
save the ad hoc scripts you typed in IPython session, logstart:
%logstart
from awesome import me
exit()
# cat ipython_log.py
profile
the basic one, timeit:
%timeit reduce(lambda x, _: x + [x[-2] + x[-1]], [0] * (n-2), [0, 1])
#19.4 µs ± 47.1 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
memory usage, memory_profiler:
%load_ext memory_profiler
# usage
%mprun -f func_a -f func_b func_c()
Note that, only functions after -f
will do the memory profile.
The command above implies that func_c will call func_a and func_b.
Wrap it up
magic | description |
---|---|
%autoreload |
debug mode |
%storemagic |
stash object |
%logstart |
stash script |
%timeit |
simple benchmark |
%mprun |
memory profile |
references: