#!/usr/bin/env python # coding: utf-8 # # IPython Magic Commands # IPython has a system of commands we call 'magics' that provide effectively a mini command language that is orthogonal to the syntax of Python and is extensible by the user with new commands. Magics are meant to be typed interactively, so they use command-line conventions, such as using whitespace for separating arguments, dashes for options and other conventions typical of a command-line environment. # # Magics come in two kinds: # # Line magics: these are commands prepended by one % character and whose arguments only extend to the end of the current line. # # Cell magics: these use two percent characters as a marker (%%), and they receive as argument both the current line where they are declared and the whole body of the cell. # In[1]: # brings up a documentation pane with an overview of IPython's feature get_ipython().show_usage() # In[2]: # quick reference guide get_ipython().run_line_magic('quickref', '') # In[3]: # use the ! operator to run system shell commands get_ipython().system('ping www.google.com') # In[4]: # several ways to interactively access help documentation help # In[5]: # help module for magic commands get_ipython().run_line_magic('magic', '') # In[6]: # list of all available magic commands get_ipython().run_line_magic('lsmagic', '') # In[7]: # bring up the help module for a specific command get_ipython().run_line_magic('pinfo', '%timeit') # In[8]: # the ? syntax also works for any generic object x = 5 get_ipython().run_line_magic('pinfo', 'x') # In[9]: # run a python script from within the notebook get_ipython().run_line_magic('run', 'scripts/hello') # In[10]: # debug a statement either in-line or after the fact get_ipython().run_line_magic('debug', '') # In[11]: # time the execution of a statement import numpy as np get_ipython().run_line_magic('timeit', 'np.linalg.eigvals(np.random.rand(100,100))') # In[12]: # additional magic can be loaded using load_ext get_ipython().run_line_magic('load_ext', 'Cython') # In[13]: # list all environment variables or specific variables get_ipython().run_line_magic('env', 'SYSTEMROOT') # In[14]: # print the cache of previously executed commands get_ipython().run_line_magic('history', '') # In[15]: # set matplotlib to inline or interactive (qt) mode get_ipython().run_line_magic('matplotlib', 'inline') # In[16]: # print the call signature for any object import urllib get_ipython().run_line_magic('pdef', 'urllib.urlopen') # In[17]: # print the docstring for a class/callable object get_ipython().run_line_magic('pdoc', 'urllib') # In[18]: # print all interactive variables get_ipython().run_line_magic('who', '') # In[19]: # same as who but more information get_ipython().run_line_magic('whos', '') # In[20]: # example using cProfile import matplotlib.pyplot as plt def f(x): return x**3 - 1 def fprime(x): return 3*x**2 def newton(z, f, fprime, max_iter=100, tol=1e-6): """The Newton-Raphson method.""" for i in range(max_iter): step = f(z)/fprime(z) if abs(step) < tol: return i, z z -= step return i, z def plot_newton_iters(p, pprime, n=200, extent=[-1,1,-1,1], cmap='hsv'): """Shows how long it takes to converge to a root using the Newton-Raphson method.""" m = np.zeros((n,n)) xmin, xmax, ymin, ymax = extent for r, x in enumerate(np.linspace(xmin, xmax, n)): for s, y in enumerate(np.linspace(ymin, ymax, n)): z = x + y*1j m[s, r] = newton(z, p, pprime)[0] plt.imshow(m, cmap=cmap, extent=extent) stats = get_ipython().run_line_magic('prun', '-r -q plot_newton_iters(f, fprime)') # In[21]: stats.sort_stats('time').print_stats(10) # In[22]: # reset the namespace get_ipython().run_line_magic('reset', '')