.. include:: ==================================================== A Demonstration of the 'IPython' Interactive Shell ==================================================== :Author: Jeff Rush :Copyright: 2007 Tau Productions Inc. :License: GNU Free Documentation License :Date: June 27, 2007 :Version: 1 A usage walkthru showing the use of the enhanced interactive shell named IPython. Superior to the default Python shell, it provides power object introspection features, easy access to operating system commands, integrates with your text editor and can play nice with GUI frameworks. .. |CLICK| image:: /home/caster/themes/screencast-800x600/NextSlide.png .. container:: handout Hello, my name is Jeff Rush and this talk is a demonstration walkthru of the IPython interactive shell. If you missed it, there is a prior presentation, "5-Minutes with Python and the Interactive Shell 'IPython'" that provides a features and benefits overview in a slideshow format. Demonstration ============= .. class: center .. class:: huge Enough slides, let's see some code! .. container:: handout + (type in msgflag=1, pi=3, msg='hello', warning=None) + (introduce tab completion) + Namespace Management + %who {TYPES}... simple names + %whos {TYPES}... values and types too + %psearch {RE} {TYPE} + %store {VAR} or %store {VAR} >/var/tmp/a.txt + %store (list stored vars) + %reset + %store -r (refresh from storage) + %store -z (zero out storage) + Logging + %logstate + %logstart and %logon and %logoff + (do not show log -- will show it further on) + Intro to Magic + %lsmagic + can omit percent if no variable exists by that name + %magic (notice output is paged) + SOMETHING? and SOMETHING?? + A Few Cosmetic Commands + (import sys) + '%p sys.path' commmand for 'print' + '%page sys.path' command for pretty-print an object + auto-parenthese and /NAME (need a callable) + auto-strings (start with , or ;) + Slight Detour into Help Subsystem (not IPython) + help(Module) or help('keyword') or help('TOPIC') + help() then "topics", "keywords", "modules" (beware!) + quit to exit + Introspection + (import re) + %pdef re.match + %pdoc re.match + %pinfo string.upper (all info together - same as string.upper?) + %psource + re.match?? (same as all of above) + %pfile (show all of file in which definition appears) + %edit -x string.upper + Directory Navigation (magic commands) + %pwd + %cd (tab-completion) + %pushd /usr/lib/python2.4/site-packages/ + %pushd /usr/share/zope-3.3.1-py2.4/lib/python/BTrees + %dirs + %bookmark btree + %bookmark site /usr/lib/python2.4/site-packages/ + %bookmark -l + %popd + %dhist + System Shell Commands + cd site + !ls and !ls -l + patt = "\*.py" + !ls -l $patt + !ls -l ${patt+'c'} + %alias + ls and ls -l + ldir + %alias pr echo You Said: %s + %unalias + :: | def go(patt=patt): | !ls -l $patt + go() + go("\*.so") + x = !ls and x x.s x.l x.n + x = !ls \*.py | sort + Running Python Programs + cd ~/notes/presentations/demofiles + %run calc + (c is an instance of Calculator) runs with access to namespace afterward (reloads each time) starts with empty namespace, but can inherit interactive one if desired + runs entire programs __main__ + %run -p + %run -d + Input History Caching + Input Caching + up/down arrows or Ctl-P/N (partial search for 'def') + _i, _ii, _iii + _iN + In[N] (a list) + View History of Inputs + %hist and %hist -r (raw, expansion) + %hist LASTN or %hist FIRSTN LASTN + Input Replay + exec _i7 + exec In[3:7] + exec In[3:7]+In[9] + Use'em often? Give'em a Name! + a = 3; b = 4; c = 5 + %macro abc 3-7 9 + abc + %who (macro is a variable) + print abc + store abc + Viewing and Editing Source + cd /tmp + edit abc (edit object) + edit 3:7 9 (edit input lines) + save FILENAME 3:7 9 (saves input lines into a file) + less /tmp/FILENAME.py + cd site + less site.py + pycat site.py (syntax coloring) + cd /tmp + %edit dummy.py + (visit http://aspn.activestate.com/ASPN/Python/Cookbook/) + (grab an algorithm) + %cpaste + Output History Caching + Out[N] (a dict) + Out.keys() + a = Out[12] + 34 + trailing semicolon means don't print return value:: def xyz(): print "Hello" return "World" + Logging (continued from beginning) - another way to cache output + less ipython_log.py + %runlog FILES... (logfiles are executable Python) + can log your input, or your input and your output + Debugging Your Programs + cd ~/notes/presentations/demofiles + Choice of Tracebacks + %xmode Plain and then %run calc2 + %xmode Context and then %run calc2 + %xmode Verbose and then %run calc2 + Access to Python Debugger + %pdb and then %run calc2 (toggles go-into-debugger-upon-exception) + %run -d + %debug (goes into debugger NOW) + (uncomment the following inside calc2.py):: >>> from IPython.Shell import IPShellEmbed >>> ipshell = IPShellEmbed() >>> ipshell() (drop into shell; exit to continue) + NOT a breakpoint, uni-directional -- changes you make do NOT flow back cannot single step + Profiling + %time sum(range(10,000,000)) (coarse) + %timeit 2**124 (fine) + %time zopedocs(); (note semicolon) + %timeit zopedocs() (no semicolon needed) + %prun zopedocs() (runs expressions, NOT files) + %run -p (runs files, NOT expressions) + Running in Background + cd ~/notes/presentations/demofiles + ed gofetch.py + %bg gofetch() + jobs[0].status + jobs[0].result + Flexible Input Filtering + ipython -p physics + m = 200 lb + v = 30 mi/h + e = 1.0/2.0 * m * v**2 + e.convertToUnit('J') + e + Data Visualization + ipython -pylab + Visit http://matplotlib.sourceforge.net/screenshots.html + snip source and %cpaste it in Wrapup for Now ============== For More Information -------------------- http://ipython.scipy.org/moin/ Contact ------- Jeff Rush .. Local Variables: mode: rst mode: outline-minor End: