{ "metadata": { "name": "", "signature": "sha256:bcc633f3b2edcc1e9101832323b40711254d2b2740643bc82ad57d9e1c63ea63" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "IPython: beyond plain Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When executing code in IPython, all valid Python syntax works as-is, but IPython provides a number of features designed to make the interactive experience more fluid and efficient." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "First things first: running code, getting help" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the notebook, to run a cell of code, hit `Shift-Enter`. This executes the cell and puts the cursor in the next cell below, or makes a new one if you are at the end. Alternately, you can use:\n", " \n", "- `Alt-Enter` to force the creation of a new cell unconditionally (useful when inserting new content in the middle of an existing notebook).\n", "- `Control-Enter` executes the cell and keeps the cursor in the same cell, useful for quick experimentation of snippets that you don't need to keep permanently." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Hi\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hi\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Getting help:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Typing `object_name?` will print all sorts of details about any object, including docstrings, function definition lines (for call arguments) and constructor details for classes." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import collections\n", "collections.namedtuple?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "collections.Counter??" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "*int*?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "An IPython quick reference card:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%quickref" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Tab completion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tab completion, especially for attributes, is a convenient way to explore the structure of any object you\u2019re dealing with. Simply type `object_name.` to view the object\u2019s attributes. Besides Python objects and keywords, tab completion also works on file and directory names." ] }, { "cell_type": "code", "collapsed": false, "input": [ "collections." ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The interactive workflow: input, output, history" ] }, { "cell_type": "code", "collapsed": false, "input": [ "2+10" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "12" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "_+10" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "22" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "You can suppress the storage and rendering of output if you append `;` to the last cell (this comes in handy when plotting with matplotlib, for example):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "10+20;" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "_" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "22" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The output is stored in `_N` and `Out[N]` variables:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "_10 == Out[10]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "True" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "And the last three have shorthands for convenience:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'last output:', _\n", "print 'next one :', __\n", "print 'and next :', ___" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "last output: True\n", "next one : 22\n", "and next : 22\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "In[11]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "u'_10 == Out[10]'" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "_i" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "u'In[11]'" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "_ii" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "u'In[11]'" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "print 'last input:', _i\n", "print 'next one :', _ii\n", "print 'and next :', _iii" ], "language": "python", "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "last input: _ii\n", "next one : _i\n", "and next : In[11]\n" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "%history -n 1-5" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 1: print \"Hi\"\n", " 2: ?\n", " 3:\n", "import collections\n", "collections.namedtuple?\n", " 4: collections.Counter??\n", " 5: *int*?\n" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Exercise**\n", "\n", "Write the last 10 lines of history to a file named `log.py`." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Accessing the underlying operating system" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!pwd" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/home/fperez/teach/astro-hack-week/day1/ipython\r\n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "files = !ls\n", "print \"My current directory's files:\"\n", "print files" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "My current directory's files:\n", "['Beyond Plain Python.ipynb', 'Converting Notebooks With nbconvert.ipynb', 'Custom Display Logic.ipynb', 'images', 'Notebook Basics.ipynb', 'Rich Output.ipynb', 'Using Interact.ipynb', 'Working With Markdown Cells.html', 'Working With Markdown Cells.ipynb', 'Working With Markdown Cells.pdf', 'Working With Markdown Cells.tex']\n" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "!echo $files" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[Beyond Plain Python.ipynb, Converting Notebooks With nbconvert.ipynb, Custom Display Logic.ipynb, images, Notebook Basics.ipynb, Rich Output.ipynb, Using Interact.ipynb, Working With Markdown Cells.html, Working With Markdown Cells.ipynb, Working With Markdown Cells.pdf, Working With Markdown Cells.tex]\r\n" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "!echo {files[0].upper()}" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "BEYOND PLAIN PYTHON.IPYNB\r\n" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that all this is available even in multiline blocks:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "for i,f in enumerate(files):\n", " if f.endswith('ipynb'):\n", " !echo {\"%02d\" % i} - \"{os.path.splitext(f)[0]}\"\n", " else:\n", " print '--'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "00 - Beyond Plain Python\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "01 - Converting Notebooks With nbconvert\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "02 - Custom Display Logic\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "--\n", "04 - Notebook Basics\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "05 - Rich Output\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "06 - Using Interact\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "--\n", "08 - Working With Markdown Cells\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "--\n", "--\n" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Beyond Python: magic functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The IPyhton 'magic' functions are a set of commands, invoked by prepending one or two `%` signs to their name, that live in a namespace separate from your normal Python variables and provide a more command-like interface. They take flags with `--` and arguments without quotes, parentheses or commas. The motivation behind this system is two-fold:\n", " \n", "- To provide an orthogonal namespace for controlling IPython itself and exposing other system-oriented functionality.\n", "\n", "- To expose a calling mode that requires minimal verbosity and typing while working interactively. Thus the inspiration taken from the classic Unix shell style for commands." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%magic" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Line vs cell magics:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%timeit range(10)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1000000 loops, best of 3: 195 ns per loop\n" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "%%timeit\n", "range(10)\n", "range(100)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1000000 loops, best of 3: 832 ns per loop\n" ] } ], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Line magics can be used even inside code blocks:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(5):\n", " size = i*100\n", " print 'size:',size, \n", " %timeit range(size)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "size: 010000000 loops, best of 3: 122 ns per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", " size: 1001000000 loops, best of 3: 651 ns per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", " size: 2001000000 loops, best of 3: 1.1 \u00b5s per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", " size: 3001000000 loops, best of 3: 1.74 \u00b5s per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", " size: 400100000 loops, best of 3: 2.66 \u00b5s per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n" ] } ], "prompt_number": 26 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Magics can do anything they want with their input, so it doesn't have to be valid Python:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%bash\n", "echo \"My shell is:\" $SHELL\n", "echo \"My memory status is:\"\n", "free" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "My shell is: /bin/bash\n", "My memory status is:\n", " total used free shared buffers cached\n", "Mem: 7870496 7077772 792724 265364 442596 2153668\n", "-/+ buffers/cache: 4481508 3388988\n", "Swap: 3905532 20552 3884980\n" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another interesting cell magic: create any file you want locally from the notebook:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile test.txt\n", "This is a test file!\n", "It can contain anything I want...\n", "\n", "And more..." ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing test.txt\n" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "!cat test.txt" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is a test file!\r\n", "It can contain anything I want...\r\n", "\r\n", "And more..." ] } ], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see what other magics are currently defined in the system:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%lsmagic" ], "language": "python", "metadata": {}, "outputs": [ { "json": [ "{\"cell\": {\"prun\": \"ExecutionMagics\", \"file\": \"Other\", \"!\": \"OSMagics\", \"capture\": \"ExecutionMagics\", \"timeit\": \"ExecutionMagics\", \"script\": \"ScriptMagics\", \"pypy\": \"Other\", \"system\": \"OSMagics\", \"perl\": \"Other\", \"HTML\": \"Other\", \"bash\": \"Other\", \"python\": \"Other\", \"SVG\": \"Other\", \"javascript\": \"DisplayMagics\", \"writefile\": \"OSMagics\", \"ruby\": \"Other\", \"python3\": \"Other\", \"python2\": \"Other\", \"latex\": \"DisplayMagics\", \"sx\": \"OSMagics\", \"svg\": \"DisplayMagics\", \"html\": \"DisplayMagics\", \"sh\": \"Other\", \"time\": \"ExecutionMagics\", \"debug\": \"ExecutionMagics\"}, \"line\": {\"psource\": \"NamespaceMagics\", \"logstart\": \"LoggingMagics\", \"popd\": \"OSMagics\", \"loadpy\": \"CodeMagics\", \"install_ext\": \"ExtensionMagics\", \"colors\": \"BasicMagics\", \"who_ls\": \"NamespaceMagics\", \"lf\": \"Other\", \"install_profiles\": \"DeprecatedMagics\", \"clk\": \"Other\", \"ll\": \"Other\", \"pprint\": \"BasicMagics\", \"lk\": \"Other\", \"ls\": \"Other\", \"save\": \"CodeMagics\", \"tb\": \"ExecutionMagics\", \"lx\": \"Other\", \"dl\": \"Other\", \"pylab\": \"PylabMagics\", \"dd\": \"Other\", \"quickref\": \"BasicMagics\", \"dx\": \"Other\", \"d\": \"Other\", \"magic\": \"BasicMagics\", \"dhist\": \"OSMagics\", \"edit\": \"KernelMagics\", \"logstop\": \"LoggingMagics\", \"gui\": \"BasicMagics\", \"alias_magic\": \"BasicMagics\", \"debug\": \"ExecutionMagics\", \"page\": \"BasicMagics\", \"logstate\": \"LoggingMagics\", \"ed\": \"Other\", \"pushd\": \"OSMagics\", \"timeit\": \"ExecutionMagics\", \"rehashx\": \"OSMagics\", \"hist\": \"Other\", \"qtconsole\": \"KernelMagics\", \"rm\": \"Other\", \"dirs\": \"OSMagics\", \"run\": \"ExecutionMagics\", \"reset_selective\": \"NamespaceMagics\", \"rep\": \"Other\", \"pinfo2\": \"NamespaceMagics\", \"matplotlib\": \"PylabMagics\", \"automagic\": \"AutoMagics\", \"doctest_mode\": \"KernelMagics\", \"logoff\": \"LoggingMagics\", \"reload_ext\": \"ExtensionMagics\", \"pdb\": \"ExecutionMagics\", \"load\": \"CodeMagics\", \"lsmagic\": \"BasicMagics\", \"cl\": \"Other\", \"autosave\": \"KernelMagics\", \"cd\": \"OSMagics\", \"pastebin\": \"CodeMagics\", \"prun\": \"ExecutionMagics\", \"cp\": \"Other\", \"autocall\": \"AutoMagics\", \"bookmark\": \"OSMagics\", \"connect_info\": \"KernelMagics\", \"mkdir\": \"Other\", \"system\": \"OSMagics\", \"whos\": \"NamespaceMagics\", \"rmdir\": \"Other\", \"unload_ext\": \"ExtensionMagics\", \"store\": \"StoreMagics\", \"more\": \"KernelMagics\", \"pdef\": \"NamespaceMagics\", \"precision\": \"BasicMagics\", \"pinfo\": \"NamespaceMagics\", \"pwd\": \"OSMagics\", \"psearch\": \"NamespaceMagics\", \"reset\": \"NamespaceMagics\", \"recall\": \"HistoryMagics\", \"xdel\": \"NamespaceMagics\", \"xmode\": \"BasicMagics\", \"cat\": \"Other\", \"mv\": \"Other\", \"rerun\": \"HistoryMagics\", \"logon\": \"LoggingMagics\", \"history\": \"HistoryMagics\", \"pycat\": \"OSMagics\", \"unalias\": \"OSMagics\", \"install_default_config\": \"DeprecatedMagics\", \"env\": \"OSMagics\", \"load_ext\": \"ExtensionMagics\", \"config\": \"ConfigMagics\", \"killbgscripts\": \"ScriptMagics\", \"profile\": \"BasicMagics\", \"pfile\": \"NamespaceMagics\", \"less\": \"KernelMagics\", \"who\": \"NamespaceMagics\", \"notebook\": \"BasicMagics\", \"man\": \"KernelMagics\", \"sx\": \"OSMagics\", \"macro\": \"ExecutionMagics\", \"clear\": \"KernelMagics\", \"alias\": \"OSMagics\", \"time\": \"ExecutionMagics\", \"sc\": \"OSMagics\", \"ldir\": \"Other\", \"pdoc\": \"NamespaceMagics\"}}" ], "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "Available line magics:\n", "%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %cl %clear %clk %colors %config %connect_info %cp %d %dd %debug %dhist %dirs %dl %doctest_mode %dx %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode\n", "\n", "Available cell magics:\n", "%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile\n", "\n", "Automagic is ON, % prefix IS NOT needed for line magics." ] } ], "prompt_number": 30 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Running normal Python code: execution and errors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Not only can you input normal Python code, you can even paste straight from a Python or IPython shell session:" ] }, { "cell_type": "code", "collapsed": false, "input": [ ">>> # Fibonacci series:\n", "... # the sum of two elements defines the next\n", "... a, b = 0, 1\n", ">>> while b < 10:\n", "... print b\n", "... a, b = b, a+b" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "1\n", "2\n", "3\n", "5\n", "8\n" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "In [1]: for i in range(10):\n", " ...: print i,\n", " ...: " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1 2 3 4 5 6 7 8 9\n" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And when your code produces errors, you can control how they are displayed with the `%xmode` magic:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile mod.py\n", "\n", "def f(x):\n", " return 1.0/(x-1)\n", "\n", "def g(y):\n", " return f(y+1)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing mod.py\n" ] } ], "prompt_number": 33 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's call the function `g` with an argument that would produce an error:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import mod\n", "mod.g(0)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "float division by zero", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmod\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mmod\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m/home/fperez/teach/astro-hack-week/day1/ipython/mod.py\u001b[0m in \u001b[0;36mg\u001b[1;34m(y)\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m/home/fperez/teach/astro-hack-week/day1/ipython/mod.py\u001b[0m in \u001b[0;36mf\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[1;36m1.0\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mZeroDivisionError\u001b[0m: float division by zero" ] } ], "prompt_number": 34 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Raw Input in the notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since 1.0 the IPython notebook web application support `raw_input` which for example allow us to invoke the `%debug` magic in the notebook:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mod.g(0)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "float division by zero", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmod\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m/home/fperez/teach/astro-hack-week/day1/ipython/mod.py\u001b[0m in \u001b[0;36mg\u001b[1;34m(y)\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m/home/fperez/teach/astro-hack-week/day1/ipython/mod.py\u001b[0m in \u001b[0;36mf\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[1;36m1.0\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mZeroDivisionError\u001b[0m: float division by zero" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "%debug" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "> \u001b[1;32m/home/fperez/teach/astro-hack-week/day1/ipython/mod.py\u001b[0m(3)\u001b[0;36mf\u001b[1;34m()\u001b[0m\n", "\u001b[1;32m 2 \u001b[1;33m\u001b[1;32mdef\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[1;32m----> 3 \u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[1;36m1.0\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[1;32m 4 \u001b[1;33m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "ipdb> u\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "> \u001b[1;32m/home/fperez/teach/astro-hack-week/day1/ipython/mod.py\u001b[0m(6)\u001b[0;36mg\u001b[1;34m()\u001b[0m\n", "\u001b[1;32m 4 \u001b[1;33m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[1;32m 5 \u001b[1;33m\u001b[1;32mdef\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[1;32m----> 6 \u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "ipdb> q\n" ] } ], "prompt_number": 41 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Don't foget to exit your debugging session. Raw input can of course be use to ask for user input:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "enjoy = raw_input('Are you enjoying this tutorial ?')\n", "print 'enjoy is :', enjoy" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Are you enjoying this tutorial ?Yes !\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "enjoy is : Yes !\n" ] } ], "prompt_number": 39 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Running code in other languages with special `%%` magics" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%perl\n", "@months = (\"July\", \"August\", \"September\");\n", "print $months[0];" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "July" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "%%ruby\n", "name = \"world\"\n", "puts \"Hello #{name.capitalize}!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello World!\n" ] } ], "prompt_number": 39 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "The IPython kernel/client model" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%connect_info" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{\n", " \"stdin_port\": 50023, \n", " \"ip\": \"127.0.0.1\", \n", " \"control_port\": 50024, \n", " \"hb_port\": 50025, \n", " \"signature_scheme\": \"hmac-sha256\", \n", " \"key\": \"b54b8859-d64d-48bb-814a-909f9beb3316\", \n", " \"shell_port\": 50021, \n", " \"transport\": \"tcp\", \n", " \"iopub_port\": 50022\n", "}\n", "\n", "Paste the above JSON into a file, and connect with:\n", " $> ipython --existing \n", "or, if you are local, you can connect with just:\n", " $> ipython --existing kernel-30f00f4a-230c-4e64-bea5-0e5f6a52cb40.json \n", "or even just:\n", " $> ipython --existing \n", "if this is the most recent IPython session you have started.\n" ] } ], "prompt_number": 43 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can connect automatically a Qt Console to the currently running kernel with the `%qtconsole` magic, or by typing `ipython console --existing ` in any terminal:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%qtconsole" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 83 } ], "metadata": {} } ] }