diff --git a/03 - More Python goodness (1).ipynb b/03 - More Python goodness (1).ipynb new file mode 100644 index 0000000000000000000000000000000000000000..0635cf8fc1f6f5376f375d5e60067c31823439aa --- /dev/null +++ b/03 - More Python goodness (1).ipynb @@ -0,0 +1,912 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:bdcfde574b39f174fe25a293cb241f4aa1518865a8dd971976133f07d75a58e5" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# More Python Goodness (1)\n", + "***\n", + "\n", + "[Jeroen Laros](mailto:j.f.j.laros@lumc.nl), [Michiel van Galen](mailto:m.van_galen@lumc.nl), [Wai Yi Leung](w.y.leung@lumc.nl), [Martijn Vermaat](mailto:m.vermaat.hg@lumc.nl)\n", + "\n", + "[Department of Human Genetics, Leiden University Medical Center](http://humgen.nl)\n", + "\n", + "[Sequencing Analysis Support Core, Leiden University Medical Center](http://sasc.lumc.nl)\n", + "\n", + "License: [Creative Commons Attribution 3.0 License (CC-by)](http://creativecommons.org/licenses/by/3.0)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Table of contents\n", + "\n", + "1. [Working with scripts](#scripts)\n", + "2. [The standard library](#stdlib)\n", + "3. [String methods](#stringmethods)\n", + "4. [Comments and docstrings](#docstrings)\n", + "5. [Detour: PEP8 and other PEPs](#peps)\n", + "6. [Errors and exceptions](#exceptions)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<a id=\"scripts\"></a>\n", + "## Working with scripts\n", + "\n", + "Interpreters are great for *prototyping*, but not really suitable if you want to **share** or **release** code. To do so, we write our Python commands in scripts (and later, modules).\n", + "\n", + "A **script** is a simple text file containing Python instructions to execute." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Executing scripts\n", + "\n", + "There are two common ways to execute a script:\n", + "\n", + "1. As an argument of the Python interpreter command.\n", + "2. As a standalone executable (with the appropriate shebang line & file mode).\n", + "\n", + "IPython gives you a third option:\n", + "\n", + "<ol start=\"3\">\n", + " <li>As an argument of the `%run` magic.\n", + "</ol>" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Writing your script\n", + "\n", + "Let's start with a simple GC calculator. Open your text editor, and write the following Python statements (remember your indentations):\n", + "\n", + " def calc_gc_percent(seq):\n", + " at_count, gc_count = 0, 0\n", + " for char in seq:\n", + " if char in ('A', 'T'):\n", + " at_count += 1\n", + " elif char in ('G', 'C'):\n", + " gc_count += 1\n", + " \n", + " return gc_count * 100.0 / (gc_count + at_count) \n", + "\n", + " print \"The sequence 'CAGG' has a %GC of {:.2f}\".format(\n", + " calc_gc_percent(\"CAGG\"))\n", + "\n", + "Save the file as `seq_toolbox.py` (you can use any other name if you like) and go to your shell." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Running the script\n", + "\n", + "Let's try the first method: using your script as an argument:\n", + "\n", + " $ python seq_toolbox.py\n", + "\n", + "Is the output as you expect?\n", + "\n", + "For the second method, we need to do two more things:\n", + "\n", + "1. Open the script in your editor and add the following line to the very top:\n", + "\n", + " #!/usr/bin/env python\n", + "\n", + "2. Save the file, go back to the shell, and allow the file to be executed:\n", + "\n", + " $ chmod +x seq_toolbox.py\n", + "\n", + "You can now execute the file directly:\n", + "\n", + " $ ./seq_toolbox.py\n", + "\n", + "Is the output the same as the previous method?\n", + "\n", + "Finally, try out the third method. Open an IPython interpreter session and do:\n", + "\n", + " %run seq_toolbox.py" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<a id=\"stdlib\"></a>\n", + "## The standard library\n", + "\n", + "Our script is nice and dandy, but we don't want to edit the source file everytime we calculate a sequence's GC.\n", + "\n", + "The **standard library** is a collection of Python modules (or functions, for now) that comes packaged with a default Python installation. They're not part of the language per se, more like a *batteries included* thing." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Our first standard library module: `sys`\n", + "\n", + "We'll start by using the simple `sys` module to make our script more flexible.\n", + "\n", + "Standard library (and other modules, as we'll see later) can be used via the `import` statement, for example:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Like other objects so far, we can peek into the documentation of these modules using `help`, or the IPython `?` shortcut. For example:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sys?" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The `sys.argv` list\n", + "\n", + "The `sys` module provides a way to capture command line arguments with its `argv` object. This is a list of arguments supplied when invoking the current Python session. Not really useful for an interpreter session, but very handy for scripts." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sys.argv" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 3, + "text": [ + "['-c',\n", + " '-f',\n", + " '/home/martijn/.ipython/profile_default/security/kernel-9535cbe5-e69a-4c48-91f2-80a022c362b9.json',\n", + " \"--IPKernelApp.parent_appname='ipython-notebook'\",\n", + " '--profile-dir',\n", + " '/home/martijn/.ipython/profile_default',\n", + " '--parent=1']" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sys.argv[:3]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 4, + "text": [ + "['-c',\n", + " '-f',\n", + " '/home/martijn/.ipython/profile_default/security/kernel-9535cbe5-e69a-4c48-91f2-80a022c362b9.json']" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Improving our script with `sys.argv`\n", + "\n", + "To use `sys.argv` in our script, open a text editor and edit the script by adding an import statement, capturing the `sys.argv` value, and editing our last `print` line:\n", + "\n", + " #!/usr/bin/env python\n", + " import sys\n", + "\n", + " def calc_gc_percent(seq):\n", + " at_count, gc_count = 0, 0\n", + " for char in seq:\n", + " if char in ('A', 'T'):\n", + " at_count += 1\n", + " elif char in ('G', 'C'):\n", + " gc_count += 1\n", + "\n", + " return gc_count * 100.0 / (gc_count + at_count) \n", + "\n", + " input_seq = sys.argv[1]\n", + " print \"The sequence '{}' has a %GC of {:.2f}\".format(\n", + " input_seq, calc_gc_percent(input_seq))\n", + "\n", + "To test it, you can run the following command in your shell:\n", + "\n", + " $ python seq_toolbox.py CAGG\n", + "\n", + "Try it with `./seq_toolbox.py` instead. What happens?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<a id=\"stringmethods\"></a>\n", + "## String methods\n", + "\n", + "Try running the script with `'cagg'` as the input sequence. What happens?\n", + "\n", + "As we saw earlier, many objects, like those of type `list`, `dict`, or `str`, have useful methods defined on them. One way to squash this potential bug is by using Python's string method `upper`. Let's first check out some commonly used string functions.\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_str = 'Hello again, ipython!'" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_str.upper()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 6, + "text": [ + "'HELLO AGAIN, IPYTHON!'" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_str.lower()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 7, + "text": [ + "'hello again, ipython!'" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_str.title()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 8, + "text": [ + "'Hello Again, Ipython!'" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_str.startswith('H')" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 9, + "text": [ + "True" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_str.startswith('h')" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 10, + "text": [ + "False" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_str.split(',')" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 11, + "text": [ + "['Hello again', ' ipython!']" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_str.replace('ipython', 'lumc')" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 12, + "text": [ + "'Hello again, lumc!'" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_str.count('n')" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 13, + "text": [ + "2" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Improving our script with `upper()`\n", + "\n", + "Let's use `upper()` to fortify our function. It should now look something like this:\n", + "\n", + " def calc_gc_percent(seq):\n", + " at_count, gc_count = 0, 0\n", + " for char in seq.upper():\n", + " if char in ('A', 'T'):\n", + " at_count += 1\n", + " elif char in ('G', 'C'):\n", + " gc_count += 1\n", + "\n", + " return gc_count * 100.0 / (gc_count + at_count) \n", + "\n", + "And run it (in whichever way you prefer). Do you get the expected output?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<a id=\"docstrings\"></a>\n", + "## Comments and docstrings\n", + "\n", + "There's a golden rule in programming: write code for humans (this includes you in 6 months). Python provides two ways to accomplish this: comments and docstrings.\n", + "\n", + "### Comments\n", + "\n", + "Any lines prepended with `#` are **comments**, making them ignored by the interpreter. Comments can be freeform text; anything that helps in understanding the code\n", + "\n", + "### Docstrings\n", + "\n", + "**Docstrings** are Python's way of attaching proper documentation to objects. Officially, the first string literal that occurs in a module, function, class, or method definition is used as that object's docstring.\n", + "\n", + "In practice, *triple-quoted strings* are used, to handle newlines easier.\n", + "\n", + "Remember how we used the `help` function (or IPython's `?` shortcut) to get information about an object, function, or module? This actually prints that object's docstring." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Improving our script with comments and docstrings\n", + "\n", + "Open your script again in a text editor, and add the following comments and docstrings: \n", + "\n", + " #!/usr/bin/env python\n", + " import sys\n", + "\n", + " def calc_gc_percent(seq):\n", + " \"\"\"\n", + " Calculates the GC percentage of the given sequence.\n", + "\n", + " Arguments:\n", + " - seq - the input sequence (string).\n", + "\n", + " Returns:\n", + " - GC percentage (float).\n", + "\n", + " The returned value is always <= 100.0\n", + " \"\"\"\n", + " at_count, gc_count = 0, 0\n", + " # Change input to all caps to allow for non-capital\n", + " # input sequence.\n", + " for char in seq.upper():\n", + " if char in ('A', 'T'):\n", + " at_count += 1\n", + " elif char in ('G', 'C'):\n", + " gc_count += 1\n", + "\n", + " return gc_count * 100.0 / (gc_count + at_count) \n", + "\n", + " input_seq = sys.argv[1]\n", + " print \"The sequence '{}' has a %GC of {:.2f}\".format(\n", + " input_seq, calc_gc_percent(input_seq))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<a id=\"peps\"></a>\n", + "## Detour: PEP8 and other PEPs\n", + "\n", + "Since comments and docstrings are basically free-form text, whether it's useful or not depends heavily on the developer. To mitigate this, the Python community has come up with practical conventions. They are documented in a document called **PEP8**.\n", + "\n", + "Complementary to PEP8, there is **PEP257** which is for docstrings specifically. It's not a must to follow these conventions, but *very* encouraged to do so.\n", + "\n", + "Python Enhancement Proposals, or **PEP**s, are how Python grows. There are hundreds of them now, all have to be approved by our BDFL.\n", + "\n", + "> [PEP8: Style Guide for Python Code](http://www.python.org/dev/peps/pep-0008/)\n", + "\n", + "> [PEP257: Docstring Conventions](http://www.python.org/dev/peps/pep-0257/)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<a id=\"exceptions\"></a>\n", + "## Errors and exceptions\n", + "\n", + "Try running the script with `ACTG123` as the argument. What happens? Is this acceptable behavior?\n", + "\n", + "Sometimes we want to put safeguards to handle invalid inputs. In this case we only accept `ACTG`, all other characters are invalid.\n", + "\n", + "Python provides a way to break out of the normal execution flow, by raising what's called as an **exception**. We can raise exceptions ourselves as well, by using the `raise` statement." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The `ValueError` built-in exception\n", + "\n", + "One of the most often used exceptions is the builtin exception `ValueError`. It is used on occasions where inappropriate argument values are used, for example when trying to convert the string `A` to an integer:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "int('A')" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'A'", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-14-0da6d315d7ad>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'A'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'A'" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`ValueError` is the appropriate exception to raise when your function is called with argument values it cannot handle." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Improving our script by handling invalid inputs\n", + "\n", + "Open your script, and edit the `if` clause to add our exception:\n", + "\n", + " def calc_gc_percent(seq):\n", + " \"\"\"\n", + " Calculates the GC percentage of the given sequence.\n", + "\n", + " Arguments:\n", + " - seq - the input sequence (string).\n", + "\n", + " Returns:\n", + " - GC percentage (float).\n", + "\n", + " The returned value is always <= 100.0\n", + " \"\"\"\n", + " at_count, gc_count = 0, 0\n", + " # Change input to all caps to allow for non-capital\n", + " # input sequence.\n", + " for char in seq.upper():\n", + " if char in ('A', 'T'):\n", + " at_count += 1\n", + " elif char in ('G', 'C'):\n", + " gc_count += 1\n", + " else:\n", + " raise ValueError(\n", + " \"Unexpeced character found: {}. Only \"\n", + " \"ACTGs are allowed.\".format(char))\n", + " \n", + " return gc_count * 100.0 / (gc_count + at_count)\n", + "\n", + "Try running the script again with `ACTG123` as the argument. What happens now?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Handling corner cases\n", + "\n", + "Try running the script with `''` (two quote signs) as the argument. What happens? Why? Is this a valid input?\n", + "\n", + "We don't always want to let exceptions stop program flow, sometimes we want to provide alternative flow. The `try ... except` block allows you to do this.\n", + "\n", + "The syntax is:\n", + "\n", + " try:\n", + " # Statements that may raise exceptions.\n", + " # [...]\n", + " except {exception type}:\n", + " # What to do when the exceptionis raised.\n", + " # [...]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Improving our script by handling corner cases\n", + "\n", + "Let's change our script by adding a `try ... except` block:\n", + "\n", + " def calc_gc_percent(seq):\n", + " \"\"\"\n", + " Calculates the GC percentage of the given sequence.\n", + "\n", + " Arguments:\n", + " - seq - the input sequence (string).\n", + "\n", + " Returns:\n", + " - GC percentage (float).\n", + "\n", + " The returned value is always <= 100.0\n", + " \"\"\"\n", + " at_count, gc_count = 0, 0\n", + " # Change input to all caps to allow for non-capital\n", + " # input sequence.\n", + " for char in seq.upper():\n", + " if char in ('A', 'T'):\n", + " at_count += 1\n", + " elif char in ('G', 'C'):\n", + " gc_count += 1\n", + " else:\n", + " raise ValueError(\n", + " \"Unexpeced character found: {}. Only \"\n", + " \"ACTGs are allowed.\".format(char))\n", + "\n", + " # Corner case handling: empty input sequence.\n", + " try:\n", + " return gc_count * 100.0 / (gc_count + at_count)\n", + " except ZeroDivisionError:\n", + " return 0.0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Detour: Exception handling best practices\n", + "\n", + "#### Aim for a minimal `try` block\n", + "\n", + "We want to be able to pinpoint the statements that may raise the exceptions so we can tailor our handling.\n", + "\n", + "Example of code that violates this principle:\n", + "\n", + " try:\n", + " my_function()\n", + " my_other_function()\n", + " except ValueError:\n", + " my_fallback_function()\n", + "\n", + "A better way would be:\n", + "\n", + " try:\n", + " my_function()\n", + " except ValueError:\n", + " my_fallback_function()\n", + " my_other_function()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Be specific when handling exceptions\n", + "\n", + "The following code is syntactically valid, but *never* use it in your real scripts / programs:\n", + "\n", + " try:\n", + " my_function()\n", + " except:\n", + " my_fallback_function()\n", + "\n", + "*Always* use the full exception name when handling exceptions, to make for a much cleaner code:\n", + "\n", + " try:\n", + " my_function()\n", + " except ValueError:\n", + " my_fallback_function()\n", + " except TypeError:\n", + " my_other_fallback_function()\n", + " except IndexError:\n", + " my_final_function()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Look Before You Leap (LBYL) vs Easier to Ask for Apology (EAFP)\n", + " \n", + "We could have written our last exception block like so:\n", + "\n", + " if gc_count + at_count == 0:\n", + " return 0.0\n", + " return gc_count * 100.0 / (gc_count + at_count)\n", + "\n", + "Both approaches are correct and have their own plus and minuses in general. However in this case, I would argue that EAFP is better since it makes the code more readable." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Improving our script by handling more corner cases\n", + "\n", + "Now try running your script without any arguments at all. What happens?\n", + "\n", + "Armed with what you now know, how would you handle this situation?" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from IPython.core.display import HTML\n", + "def custom_style():\n", + " style = open('styles/notebook.css', 'r').read()\n", + " return HTML('<style>' + style + '</style>')\n", + "def custom_script():\n", + " script = open('styles/notebook.js', 'r').read()\n", + " return HTML('<script>' + script + '</script>')" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "custom_style()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "html": [ + "<style>/*\n", + " https://github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers\n", + "*/\n", + "@font-face {\n", + " font-family: \"Computer Modern\";\n", + " src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');\n", + "}\n", + "div.cell{\n", + " width:800px;\n", + " margin-left:16% !important;\n", + " margin-right:auto;\n", + "}\n", + "h1 {\n", + " font-family: Helvetica, serif;\n", + "}\n", + "h4{\n", + " margin-top:12px;\n", + " margin-bottom: 3px;\n", + " }\n", + "div.text_cell_render{\n", + " font-family: Computer Modern, \"Helvetica Neue\", Arial, Helvetica, Geneva, sans-serif;\n", + " line-height: 145%;\n", + " font-size: 130%;\n", + " width:800px;\n", + " margin-left:auto;\n", + " margin-right:auto;\n", + "}\n", + ".CodeMirror{\n", + " font-family: \"Source Code Pro\", source-code-pro,Consolas, monospace;\n", + "}\n", + ".prompt{\n", + " display: None;\n", + "}\n", + ".text_cell_render .exercise {\n", + " font-weight: 300;\n", + " /*font-size: 22pt;*/\n", + " color: #4057A1;\n", + " font-style: italic;\n", + " /*margin-bottom: .5em;\n", + " margin-top: 0.5em;\n", + " display: block;*/\n", + "}\n", + ".text_cell_render .example {\n", + " font-weight: 300;\n", + " color: #40A157;\n", + " font-style: italic;\n", + "}\n", + "\n", + ".warning{\n", + " color: rgb( 240, 20, 20 )\n", + "}\n", + "</style>" + ], + "metadata": {}, + "output_type": "pyout", + "prompt_number": 16, + "text": [ + "<IPython.core.display.HTML at 0x2ceccd0>" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "custom_script()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "html": [ + "<script>// https://github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers\n", + "MathJax.Hub.Config({\n", + " TeX: {\n", + " extensions: [\"AMSmath.js\"]\n", + " },\n", + " tex2jax: {\n", + " inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n", + " displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ]\n", + " },\n", + " displayAlign: 'center', // Change this to 'center' to center equations.\n", + " \"HTML-CSS\": {\n", + " styles: {'.MathJax_Display': {\"margin\": 4}}\n", + " }\n", + " });\n", + "</script>" + ], + "metadata": {}, + "output_type": "pyout", + "prompt_number": 17, + "text": [ + "<IPython.core.display.HTML at 0x2cecdd0>" + ] + } + ], + "prompt_number": 17 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/more-python.ipynb b/03 - More Python goodness (2).ipynb similarity index 66% rename from more-python.ipynb rename to 03 - More Python goodness (2).ipynb index d804db7cc645bc0b40376cda100cdf5423b72819..14143f9d498635cad8d2f9c2c28298c61dcc010d 100644 --- a/more-python.ipynb +++ b/03 - More Python goodness (2).ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "" + "name": "", + "signature": "sha256:d5c2adf4404b1f629233eeb862afe0be50c85c5be8ac6b3427e61925dfa358ca" }, "nbformat": 3, "nbformat_minor": 0, @@ -11,7 +12,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# More Python Goodness\n", + "# More Python Goodness (2)\n", "***\n", "\n", "[Wibowo Arindrarto](mailto:w.arindrarto@lumc.nl), [Jeroen Laros](mailto:j.f.j.laros@lumc.nl), [Zuotian Tatum](mailto:z.tatum@lumc.nl), [Martijn Vermaat](mailto:m.vermaat.hg@lumc.nl)\n", @@ -29,12 +30,6 @@ "source": [ "## Table of contents\n", "\n", - "1. [Working with scripts](#scripts)\n", - "2. [The standard library](#stdlib)\n", - "3. [String methods](#stringmethods)\n", - "4. [Comments and docstrings](#docstrings)\n", - "5. [Detour: PEP8 and other PEPs](#peps)\n", - "6. [Errors and exceptions](#exceptions)\n", "7. [Working with modules](#modules)\n", "8. [Examples from the standard library](#stdlib-examples)\n", "9. [Reading and writing files](#io)\n", @@ -42,743 +37,6 @@ "11. [Further reading](#further)" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<a id=\"scripts\"></a>\n", - "## Working with scripts\n", - "\n", - "Interpreters are great for *prototyping*, but not really suitable if you want to **share** or **release** code. To do so, we write our Python commands in scripts (and later, modules).\n", - "\n", - "A **script** is a simple text file containing Python instructions to execute." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Executing scripts\n", - "\n", - "There are two common ways to execute a script:\n", - "\n", - "1. As an argument of the Python interpreter command.\n", - "2. As a standalone executable (with the appropriate shebang line & file mode).\n", - "\n", - "IPython gives you a third option:\n", - "\n", - "<ol start=\"3\">\n", - " <li>As an argument of the `%run` magic.\n", - "</ol>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Writing your script\n", - "\n", - "Let's start with a simple GC calculator. Open your text editor, and write the following Python statements (remember your indentations):\n", - "\n", - " def calc_gc_percent(seq):\n", - " at_count, gc_count = 0, 0\n", - " for char in seq:\n", - " if char in ('A', 'T'):\n", - " at_count += 1\n", - " elif char in ('G', 'C'):\n", - " gc_count += 1\n", - " \n", - " return gc_count * 100.0 / (gc_count + at_count) \n", - "\n", - " print \"The sequence 'CAGG' has a %GC of {:.2f}\".format(\n", - " calc_gc_percent(\"CAGG\"))\n", - "\n", - "Save the file as `seq_toolbox.py` (you can use any other name if you like) and go to your shell." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Running the script\n", - "\n", - "Let's try the first method: using your script as an argument:\n", - "\n", - " $ python seq_toolbox.py\n", - "\n", - "Is the output as you expect?\n", - "\n", - "For the second method, we need to do two more things:\n", - "\n", - "1. Open the script in your editor and add the following line to the very top:\n", - "\n", - " #!/usr/bin/env python\n", - "\n", - "2. Save the file, go back to the shell, and allow the file to be executed:\n", - "\n", - " $ chmod +x seq_toolbox.py\n", - "\n", - "You can now execute the file directly:\n", - "\n", - " $ ./seq_toolbox.py\n", - "\n", - "Is the output the same as the previous method?\n", - "\n", - "Finally, try out the third method. Open an IPython interpreter session and do:\n", - "\n", - " %run seq_toolbox.py" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<a id=\"stdlib\"></a>\n", - "## The standard library\n", - "\n", - "Our script is nice and dandy, but we don't want to edit the source file everytime we calculate a sequence's GC.\n", - "\n", - "The **standard library** is a collection of Python modules (or functions, for now) that comes packaged with a default Python installation. They're not part of the language per se, more like a *batteries included* thing." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Our first standard library module: `sys`\n", - "\n", - "We'll start by using the simple `sys` module to make our script more flexible.\n", - "\n", - "Standard library (and other modules, as we'll see later) can be used via the `import` statement, for example:" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "import sys" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 2 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Like other objects so far, we can peek into the documentation of these modules using `help`, or the IPython `?` shortcut. For example:" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "sys?" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 3 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### The `sys.argv` list\n", - "\n", - "The `sys` module provides a way to capture command line arguments with its `argv` object. This is a list of arguments supplied when invoking the current Python session. Not really useful for an interpreter session, but very handy for scripts." - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "sys.argv" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 9, - "text": [ - "['-c',\n", - " '-f',\n", - " '/home/martijn/.config/ipython/profile_default/security/kernel-b281163d-aa97-4ffa-aa16-c5232a9d8a89.json',\n", - " \"--IPKernelApp.parent_appname='ipython-notebook'\",\n", - " '--parent=1']" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "sys.argv[:3]" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 10, - "text": [ - "['-c',\n", - " '-f',\n", - " '/home/martijn/.config/ipython/profile_default/security/kernel-b281163d-aa97-4ffa-aa16-c5232a9d8a89.json']" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Improving our script with `sys.argv`\n", - "\n", - "To use `sys.argv` in our script, open a text editor and edit the script by adding an import statement, capturing the `sys.argv` value, and editing our last `print` line:\n", - "\n", - " #!/usr/bin/env python\n", - " import sys\n", - "\n", - " def calc_gc_percent(seq):\n", - " at_count, gc_count = 0, 0\n", - " for char in seq:\n", - " if char in ('A', 'T'):\n", - " at_count += 1\n", - " elif char in ('G', 'C'):\n", - " gc_count += 1\n", - "\n", - " return gc_count * 100.0 / (gc_count + at_count) \n", - "\n", - " input_seq = sys.argv[1]\n", - " print \"The sequence '{}' has a %GC of {:.2f}\".format(\n", - " input_seq, calc_gc_percent(input_seq))\n", - "\n", - "To test it, you can run the following command in your shell:\n", - "\n", - " $ python seq_toolbox.py CAGG\n", - "\n", - "Try it with `./seq_toolbox.py` instead. What happens?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<a id=\"stringmethods\"></a>\n", - "## String methods\n", - "\n", - "Try running the script with `'cagg'` as the input sequence. What happens?\n", - "\n", - "As we saw earlier, many objects, like those of type `list`, `dict`, or `str`, have useful methods defined on them. One way to squash this potential bug is by using Python's string method `upper`. Let's first check out some commonly used string functions.\n" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "my_str = 'Hello again, ipython!'" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 12 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "my_str.upper()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 13, - "text": [ - "'HELLO AGAIN, IPYTHON!'" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "my_str.lower()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 14, - "text": [ - "'hello again, ipython!'" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "my_str.title()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 15, - "text": [ - "'Hello Again, Ipython!'" - ] - } - ], - "prompt_number": 15 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "my_str.startswith('H')" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 16, - "text": [ - "True" - ] - } - ], - "prompt_number": 16 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "my_str.startswith('h')" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 17, - "text": [ - "False" - ] - } - ], - "prompt_number": 17 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "my_str.split(',')" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 18, - "text": [ - "['Hello again', ' ipython!']" - ] - } - ], - "prompt_number": 18 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "my_str.replace('ipython', 'lumc')" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 19, - "text": [ - "'Hello again, lumc!'" - ] - } - ], - "prompt_number": 19 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "my_str.count('n')" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 20, - "text": [ - "2" - ] - } - ], - "prompt_number": 20 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Improving our script with `upper()`\n", - "\n", - "Let's use `upper()` to fortify our function. It should now look something like this:\n", - "\n", - " def calc_gc_percent(seq):\n", - " at_count, gc_count = 0, 0\n", - " for char in seq.upper():\n", - " if char in ('A', 'T'):\n", - " at_count += 1\n", - " elif char in ('G', 'C'):\n", - " gc_count += 1\n", - "\n", - " return gc_count * 100.0 / (gc_count + at_count) \n", - "\n", - "And run it (in whichever way you prefer). Do you get the expected output?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<a id=\"docstrings\"></a>\n", - "## Comments and docstrings\n", - "\n", - "There's a golden rule in programming: write code for humans (this includes you in 6 months). Python provides two ways to accomplish this: comments and docstrings.\n", - "\n", - "### Comments\n", - "\n", - "Any lines prepended with `#` are **comments**, making them ignored by the interpreter. Comments can be freeform text; anything that helps in understanding the code\n", - "\n", - "### Docstrings\n", - "\n", - "**Docstrings** are Python's way of attaching proper documentation to objects. Officially, the first string literal that occurs in a module, function, class, or method definition is used as that object's docstring.\n", - "\n", - "In practice, *triple-quoted strings* are used, to handle newlines easier.\n", - "\n", - "Remember how we used the `help` function (or IPython's `?` shortcut) to get information about an object, function, or module? This actually prints that object's docstring." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Improving our script with comments and docstrings\n", - "\n", - "Open your script again in a text editor, and add the following comments and docstrings: \n", - "\n", - " #!/usr/bin/env python\n", - " import sys\n", - "\n", - " def calc_gc_percent(seq):\n", - " \"\"\"\n", - " Calculates the GC percentage of the given sequence.\n", - "\n", - " Arguments:\n", - " - seq - the input sequence (string).\n", - "\n", - " Returns:\n", - " - GC percentage (float).\n", - "\n", - " The returned value is always <= 100.0\n", - " \"\"\"\n", - " at_count, gc_count = 0, 0\n", - " # Change input to all caps to allow for non-capital\n", - " # input sequence.\n", - " for char in seq.upper():\n", - " if char in ('A', 'T'):\n", - " at_count += 1\n", - " elif char in ('G', 'C'):\n", - " gc_count += 1\n", - "\n", - " return gc_count * 100.0 / (gc_count + at_count) \n", - "\n", - " input_seq = sys.argv[1]\n", - " print \"The sequence '{}' has a %GC of {:.2f}\".format(\n", - " input_seq, calc_gc_percent(input_seq))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<a id=\"peps\"></a>\n", - "## Detour: PEP8 and other PEPs\n", - "\n", - "Since comments and docstrings are basically free-form text, whether it's useful or not depends heavily on the developer. To mitigate this, the Python community has come up with practical conventions. They are documented in a document called **PEP8**.\n", - "\n", - "Complementary to PEP8, there is **PEP257** which is for docstrings specifically. It's not a must to follow these conventions, but *very* encouraged to do so.\n", - "\n", - "Python Enhancement Proposals, or **PEP**s, are how Python grows. There are hundreds of them now, all have to be approved by our BDFL.\n", - "\n", - "> [PEP8: Style Guide for Python Code](http://www.python.org/dev/peps/pep-0008/)\n", - "\n", - "> [PEP257: Docstring Conventions](http://www.python.org/dev/peps/pep-0257/)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<a id=\"exceptions\"></a>\n", - "## Errors and exceptions\n", - "\n", - "Try running the script with `ACTG123` as the argument. What happens? Is this acceptable behavior?\n", - "\n", - "Sometimes we want to put safeguards to handle invalid inputs. In this case we only accept `ACTG`, all other characters are invalid.\n", - "\n", - "Python provides a way to break out of the normal execution flow, by raising what's called as an **exception**. We can raise exceptions ourselves as well, by using the `raise` statement." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### The `ValueError` built-in exception\n", - "\n", - "One of the most often used exceptions is the builtin exception `ValueError`. It is used on occasions where inappropriate argument values are used, for example when trying to convert the string `A` to an integer:" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "int('A')" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "ename": "ValueError", - "evalue": "invalid literal for int() with base 10: 'A'", - "output_type": "pyerr", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m<ipython-input-21-0da6d315d7ad>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'A'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'A'" - ] - } - ], - "prompt_number": 21 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`ValueError` is the appropriate exception to raise when your function is called with argument values it cannot handle." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Improving our script by handling invalid inputs\n", - "\n", - "Open your script, and edit the `if` clause to add our exception:\n", - "\n", - " def calc_gc_percent(seq):\n", - " \"\"\"\n", - " Calculates the GC percentage of the given sequence.\n", - "\n", - " Arguments:\n", - " - seq - the input sequence (string).\n", - "\n", - " Returns:\n", - " - GC percentage (float).\n", - "\n", - " The returned value is always <= 100.0\n", - " \"\"\"\n", - " at_count, gc_count = 0, 0\n", - " # Change input to all caps to allow for non-capital\n", - " # input sequence.\n", - " for char in seq.upper():\n", - " if char in ('A', 'T'):\n", - " at_count += 1\n", - " elif char in ('G', 'C'):\n", - " gc_count += 1\n", - " else:\n", - " raise ValueError(\n", - " \"Unexpeced character found: {}. Only \"\n", - " \"ACTGs are allowed.\".format(char))\n", - " \n", - " return gc_count * 100.0 / (gc_count + at_count)\n", - "\n", - "Try running the script again with `ACTG123` as the argument. What happens now?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Handling corner cases\n", - "\n", - "Try running the script with `''` (two quote signs) as the argument. What happens? Why? Is this a valid input?\n", - "\n", - "We don't always want to let exceptions stop program flow, sometimes we want to provide alternative flow. The `try ... except` block allows you to do this.\n", - "\n", - "The syntax is:\n", - "\n", - " try:\n", - " # Statements that may raise exceptions.\n", - " # [...]\n", - " except {exception type}:\n", - " # What to do when the exceptionis raised.\n", - " # [...]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Improving our script by handling corner cases\n", - "\n", - "Let's change our script by adding a `try ... except` block:\n", - "\n", - " def calc_gc_percent(seq):\n", - " \"\"\"\n", - " Calculates the GC percentage of the given sequence.\n", - "\n", - " Arguments:\n", - " - seq - the input sequence (string).\n", - "\n", - " Returns:\n", - " - GC percentage (float).\n", - "\n", - " The returned value is always <= 100.0\n", - " \"\"\"\n", - " at_count, gc_count = 0, 0\n", - " # Change input to all caps to allow for non-capital\n", - " # input sequence.\n", - " for char in seq.upper():\n", - " if char in ('A', 'T'):\n", - " at_count += 1\n", - " elif char in ('G', 'C'):\n", - " gc_count += 1\n", - " else:\n", - " raise ValueError(\n", - " \"Unexpeced character found: {}. Only \"\n", - " \"ACTGs are allowed.\".format(char))\n", - "\n", - " # Corner case handling: empty input sequence.\n", - " try:\n", - " return gc_count * 100.0 / (gc_count + at_count)\n", - " except ZeroDivisionError:\n", - " return 0.0" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Detour: Exception handling best practices\n", - "\n", - "#### Aim for a minimal `try` block\n", - "\n", - "We want to be able to pinpoint the statements that may raise the exceptions so we can tailor our handling.\n", - "\n", - "Example of code that violates this principle:\n", - "\n", - " try:\n", - " my_function()\n", - " my_other_function()\n", - " except ValueError:\n", - " my_fallback_function()\n", - "\n", - "A better way would be:\n", - "\n", - " try:\n", - " my_function()\n", - " except ValueError:\n", - " my_fallback_function()\n", - " my_other_function()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Be specific when handling exceptions\n", - "\n", - "The following code is syntactically valid, but *never* use it in your real scripts / programs:\n", - "\n", - " try:\n", - " my_function()\n", - " except:\n", - " my_fallback_function()\n", - "\n", - "*Always* use the full exception name when handling exceptions, to make for a much cleaner code:\n", - "\n", - " try:\n", - " my_function()\n", - " except ValueError:\n", - " my_fallback_function()\n", - " except TypeError:\n", - " my_other_fallback_function()\n", - " except IndexError:\n", - " my_final_function()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Look Before You Leap (LBYL) vs Easier to Ask for Apology (EAFP)\n", - " \n", - "We could have written our last exception block like so:\n", - "\n", - " if gc_count + at_count == 0:\n", - " return 0.0\n", - " return gc_count * 100.0 / (gc_count + at_count)\n", - "\n", - "Both approaches are correct and have their own plus and minuses in general. However in this case, I would argue that EAFP is better since it makes the code more readable." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Improving our script by handling more corner cases\n", - "\n", - "Now try running your script without any arguments at all. What happens?\n", - "\n", - "Armed with what you now know, how would you handle this situation?" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -887,7 +145,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 5 + "prompt_number": 1 }, { "cell_type": "code", @@ -901,13 +159,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 6, + "prompt_number": 2, "text": [ "<function seq_toolbox.calc_gc_percent>" ] } ], - "prompt_number": 6 + "prompt_number": 2 }, { "cell_type": "markdown", @@ -925,7 +183,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 8 + "prompt_number": 3 }, { "cell_type": "code", @@ -939,13 +197,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 23, + "prompt_number": 4, "text": [ "50.0" ] } ], - "prompt_number": 23 + "prompt_number": 4 }, { "cell_type": "markdown", @@ -963,7 +221,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 27 + "prompt_number": 5 }, { "cell_type": "code", @@ -977,13 +235,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 28, + "prompt_number": 6, "text": [ "25.0" ] } ], - "prompt_number": 28 + "prompt_number": 6 }, { "cell_type": "markdown", @@ -1001,7 +259,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 29 + "prompt_number": 7 }, { "cell_type": "code", @@ -1015,13 +273,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 30, + "prompt_number": 8, "text": [ "25.0" ] } ], - "prompt_number": 30 + "prompt_number": 8 }, { "cell_type": "markdown", @@ -1070,7 +328,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 32 + "prompt_number": 9 }, { "cell_type": "code", @@ -1084,13 +342,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 33, + "prompt_number": 10, "text": [ "'/home/martijn/projects/programming-course'" ] } ], - "prompt_number": 33 + "prompt_number": 10 }, { "cell_type": "code", @@ -1104,13 +362,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 34, + "prompt_number": 11, "text": [ - "'/home/martijn/.virtualenvs/programming-course/bin:/home/martijn/projects/muscle/muscle3.8.31/src:/home/martijn/projects/bedtools/bin:/home/martijn/projects/bamtools/bamtools/bin:/home/martijn/projects/gvnl/concordance/tabix:/home/martijn/projects/gvnl/concordance/vcftools/bin:/home/martijn/projects/gvnl/concordance/vcftools/cpp:/home/martijn/projects/samtools-trunk:/home/martijn/projects/samtools-trunk/bcftools:/home/martijn/coq-8.3-rc1/bin:/home/martijn/projects/kiek/trunk:/home/martijn/bin:/home/martijn/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games'" + "'/home/martijn/.virtualenvs/programming-course/bin:/home/martijn/projects/vcftools_0.1.11/bin:/home/martijn/projects/vcftools_0.1.11/cpp:/home/martijn/projects/muscle/muscle3.8.31/src:/home/martijn/projects/bedtools/bin:/home/martijn/projects/bamtools/bamtools/bin:/home/martijn/projects/gvnl/concordance/tabix:/home/martijn/projects/samtools-trunk:/home/martijn/projects/samtools-trunk/bcftools:/home/martijn/.venvburrito/bin:/home/martijn/coq-8.3-rc1/bin:/home/martijn/projects/kiek/trunk:/home/martijn/bin:/home/martijn/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games'" ] } ], - "prompt_number": 34 + "prompt_number": 11 }, { "cell_type": "code", @@ -1121,7 +379,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 35 + "prompt_number": 12 }, { "cell_type": "code", @@ -1135,13 +393,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 36, + "prompt_number": 13, "text": [ "('input', '.fastq')" ] } ], - "prompt_number": 36 + "prompt_number": 13 }, { "cell_type": "code", @@ -1156,13 +414,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 37, + "prompt_number": 14, "text": [ "'/home/martijn/projects/programming-course/input.fastq'" ] } ], - "prompt_number": 37 + "prompt_number": 14 }, { "cell_type": "code", @@ -1176,13 +434,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 19, + "prompt_number": 15, "text": [ "False" ] } ], - "prompt_number": 19 + "prompt_number": 15 }, { "cell_type": "code", @@ -1196,13 +454,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 20, + "prompt_number": 16, "text": [ "True" ] } ], - "prompt_number": 20 + "prompt_number": 16 }, { "cell_type": "code", @@ -1216,13 +474,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 21, + "prompt_number": 17, "text": [ "False" ] } ], - "prompt_number": 21 + "prompt_number": 17 }, { "cell_type": "markdown", @@ -1244,7 +502,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 38 + "prompt_number": 18 }, { "cell_type": "code", @@ -1258,29 +516,33 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 23, + "prompt_number": 19, "text": [ "['',\n", - " '/home/bow/devel/repos/watch/ipython',\n", - " '/home/bow/devel/modules/python',\n", - " '/home/bow/devel/repos/work/mnm',\n", - " '/home/bow/devel/sandbox/.virtualenvs/ipython/lib/python27.zip',\n", - " '/home/bow/devel/sandbox/.virtualenvs/ipython/lib/python2.7',\n", - " '/home/bow/devel/sandbox/.virtualenvs/ipython/lib/python2.7/plat-linux2',\n", - " '/home/bow/devel/sandbox/.virtualenvs/ipython/lib/python2.7/lib-tk',\n", - " '/home/bow/devel/sandbox/.virtualenvs/ipython/lib/python2.7/lib-old',\n", - " '/home/bow/devel/sandbox/.virtualenvs/ipython/lib/python2.7/lib-dynload',\n", - " '/usr/lib64/python2.7',\n", + " '/home/martijn/.venvburrito/lib/python/distribute-0.6.49-py2.7.egg',\n", + " '/home/martijn/.venvburrito/lib/python/pip-1.4.1-py2.7.egg',\n", + " '/home/martijn/.venvburrito/lib/python2.7/site-packages',\n", + " '/home/martijn/.venvburrito/lib/python',\n", + " '/usr/local/samba/lib/python2.6/site-packages',\n", + " '/home/martijn/.virtualenvs/programming-course/lib/python2.7',\n", + " '/home/martijn/.virtualenvs/programming-course/lib/python2.7/plat-linux2',\n", + " '/home/martijn/.virtualenvs/programming-course/lib/python2.7/lib-tk',\n", + " '/home/martijn/.virtualenvs/programming-course/lib/python2.7/lib-old',\n", + " '/home/martijn/.virtualenvs/programming-course/lib/python2.7/lib-dynload',\n", " '/usr/lib/python2.7',\n", " '/usr/lib/python2.7/plat-linux2',\n", - " '/usr/lib64/python2.7/lib-tk',\n", " '/usr/lib/python2.7/lib-tk',\n", - " '/home/bow/devel/sandbox/.virtualenvs/ipython/lib/python2.7/site-packages',\n", - " '/home/bow/devel/repos/watch/ipython/IPython/extensions']" + " '/home/martijn/.virtualenvs/programming-course/local/lib/python2.7/site-packages',\n", + " '/home/martijn/.virtualenvs/programming-course/local/lib/python2.7/site-packages/gtk-2.0',\n", + " '/home/martijn/.virtualenvs/programming-course/lib/python2.7/site-packages',\n", + " '/home/martijn/.virtualenvs/programming-course/lib/python2.7/site-packages/gtk-2.0',\n", + " '/usr/local/samba/lib/python2.6/site-packages',\n", + " '/usr/local/samba/lib/python2.6/site-packages',\n", + " '/home/martijn/.virtualenvs/programming-course/local/lib/python2.7/site-packages/IPython/extensions']" ] } ], - "prompt_number": 23 + "prompt_number": 19 }, { "cell_type": "code", @@ -1294,13 +556,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 39, + "prompt_number": 20, "text": [ "'/home/martijn/.virtualenvs/programming-course/bin/python'" ] } ], - "prompt_number": 39 + "prompt_number": 20 }, { "cell_type": "code", @@ -1314,13 +576,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 40, + "prompt_number": 21, "text": [ "sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)" ] } ], - "prompt_number": 40 + "prompt_number": 21 }, { "cell_type": "code", @@ -1334,13 +596,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 41, + "prompt_number": 22, "text": [ "2" ] } ], - "prompt_number": 41 + "prompt_number": 22 }, { "cell_type": "markdown", @@ -1362,7 +624,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 42 + "prompt_number": 23 }, { "cell_type": "code", @@ -1376,13 +638,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 28, + "prompt_number": 24, "text": [ "2.302585092994046" ] } ], - "prompt_number": 28 + "prompt_number": 24 }, { "cell_type": "code", @@ -1396,13 +658,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 29, + "prompt_number": 25, "text": [ "2.0" ] } ], - "prompt_number": 29 + "prompt_number": 25 }, { "cell_type": "code", @@ -1416,13 +678,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 30, + "prompt_number": 26, "text": [ "81.0" ] } ], - "prompt_number": 30 + "prompt_number": 26 }, { "cell_type": "code", @@ -1436,13 +698,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 31, + "prompt_number": 27, "text": [ "1.4142135623730951" ] } ], - "prompt_number": 31 + "prompt_number": 27 }, { "cell_type": "code", @@ -1456,13 +718,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 32, + "prompt_number": 28, "text": [ "3.141592653589793" ] } ], - "prompt_number": 32 + "prompt_number": 28 }, { "cell_type": "markdown", @@ -1484,7 +746,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 43 + "prompt_number": 29 }, { "cell_type": "code", @@ -1498,13 +760,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 34, + "prompt_number": 30, "text": [ - "0.596574287305641" + "0.05941901356497081" ] } ], - "prompt_number": 34 + "prompt_number": 30 }, { "cell_type": "code", @@ -1518,13 +780,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 35, + "prompt_number": 31, "text": [ - "7" + "13" ] } ], - "prompt_number": 35 + "prompt_number": 31 }, { "cell_type": "code", @@ -1539,13 +801,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 36, + "prompt_number": 32, "text": [ "'grape'" ] } ], - "prompt_number": 36 + "prompt_number": 32 }, { "cell_type": "code", @@ -1560,13 +822,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 37, + "prompt_number": 33, "text": [ - "['grape', 'orange', 'kiwi']" + "['orange', 'apple', 'banana']" ] } ], - "prompt_number": 37 + "prompt_number": 33 }, { "cell_type": "markdown", @@ -1588,7 +850,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 44 + "prompt_number": 34 }, { "cell_type": "code", @@ -1599,7 +861,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 39 + "prompt_number": 35 }, { "cell_type": "code", @@ -1610,7 +872,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 40 + "prompt_number": 36 }, { "cell_type": "code", @@ -1624,13 +886,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 41, + "prompt_number": 37, "text": [ "'CAGTCA'" ] } ], - "prompt_number": 41 + "prompt_number": 37 }, { "cell_type": "code", @@ -1641,7 +903,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 42 + "prompt_number": 38 }, { "cell_type": "code", @@ -1660,7 +922,7 @@ ] } ], - "prompt_number": 43 + "prompt_number": 39 }, { "cell_type": "markdown", @@ -1735,7 +997,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 4 + "prompt_number": 40 }, { "cell_type": "markdown", @@ -1755,7 +1017,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 44 + "prompt_number": 41 }, { "cell_type": "markdown", @@ -1776,13 +1038,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 45, + "prompt_number": 42, "text": [ "'this short file has two lines\\nit is used in the example code\\n'" ] } ], - "prompt_number": 45 + "prompt_number": 42 }, { "cell_type": "markdown", @@ -1803,13 +1065,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 46, + "prompt_number": 43, "text": [ "''" ] } ], - "prompt_number": 46 + "prompt_number": 43 }, { "cell_type": "markdown", @@ -1827,7 +1089,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 47 + "prompt_number": 44 }, { "cell_type": "code", @@ -1841,13 +1103,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 48, + "prompt_number": 45, "text": [ "'this short file has two lines\\nit is used in the example code\\n'" ] } ], - "prompt_number": 48 + "prompt_number": 45 }, { "cell_type": "markdown", @@ -1865,7 +1127,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 52 + "prompt_number": 46 }, { "cell_type": "code", @@ -1879,13 +1141,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 53, + "prompt_number": 47, "text": [ "'this short file has two lines\\n'" ] } ], - "prompt_number": 53 + "prompt_number": 47 }, { "cell_type": "code", @@ -1899,13 +1161,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 54, + "prompt_number": 48, "text": [ "'it is used in the example code\\n'" ] } ], - "prompt_number": 54 + "prompt_number": 48 }, { "cell_type": "code", @@ -1919,13 +1181,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 55, + "prompt_number": 49, "text": [ "''" ] } ], - "prompt_number": 55 + "prompt_number": 49 }, { "cell_type": "markdown", @@ -1943,7 +1205,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 56 + "prompt_number": 50 }, { "cell_type": "code", @@ -1966,7 +1228,7 @@ ] } ], - "prompt_number": 57 + "prompt_number": 51 }, { "cell_type": "markdown", @@ -1987,13 +1249,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 58, + "prompt_number": 52, "text": [ "''" ] } ], - "prompt_number": 58 + "prompt_number": 52 }, { "cell_type": "markdown", @@ -2014,13 +1276,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 59, + "prompt_number": 53, "text": [ "61" ] } ], - "prompt_number": 59 + "prompt_number": 53 }, { "cell_type": "markdown", @@ -2038,7 +1300,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 60 + "prompt_number": 54 }, { "cell_type": "code", @@ -2055,12 +1317,12 @@ "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m<ipython-input-61-4e86183cf03e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfh\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadline\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<ipython-input-55-4e86183cf03e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfh\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadline\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: I/O operation on closed file" ] } ], - "prompt_number": 61 + "prompt_number": 55 }, { "cell_type": "markdown", @@ -2080,7 +1342,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 62 + "prompt_number": 58 }, { "cell_type": "markdown", @@ -2104,12 +1366,12 @@ "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIOError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m<ipython-input-63-73497a15302b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfw\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\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<ipython-input-59-73497a15302b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfw\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mIOError\u001b[0m: File not open for reading" ] } ], - "prompt_number": 63 + "prompt_number": 59 }, { "cell_type": "markdown", @@ -2127,7 +1389,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 64 + "prompt_number": 60 }, { "cell_type": "code", @@ -2138,7 +1400,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 65 + "prompt_number": 61 }, { "cell_type": "code", @@ -2149,7 +1411,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 66 + "prompt_number": 62 }, { "cell_type": "markdown", @@ -2167,7 +1429,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 67 + "prompt_number": 63 }, { "cell_type": "code", @@ -2191,7 +1453,7 @@ ] } ], - "prompt_number": 68 + "prompt_number": 64 }, { "cell_type": "markdown", @@ -2209,7 +1471,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 71 + "prompt_number": 65 }, { "cell_type": "markdown", @@ -2243,12 +1505,12 @@ "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m<ipython-input-16-f293b9e3578f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'data/short_file.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mline\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m'We closed our filehandle'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m<ipython-input-66-f293b9e3578f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'data/short_file.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mline\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m'We closed our filehandle'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'this short file has two lines\\n'" ] } ], - "prompt_number": 16 + "prompt_number": 66 }, { "cell_type": "markdown", @@ -2274,25 +1536,25 @@ "language": "python", "metadata": {}, "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "We closed our file handle\n" + ] + }, { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: 'this short file has two lines\\n'", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m<ipython-input-17-71128226da53>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'data/short_file.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mline\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m<ipython-input-67-71128226da53>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'data/short_file.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mline\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'this short file has two lines\\n'" ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "We closed our file handle\n" - ] } ], - "prompt_number": 17 + "prompt_number": 67 }, { "cell_type": "markdown", @@ -2329,7 +1591,7 @@ ] } ], - "prompt_number": 18 + "prompt_number": 68 }, { "cell_type": "markdown", @@ -2504,7 +1766,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 4 + "prompt_number": 71 }, { "cell_type": "code", @@ -2550,14 +1812,19 @@ ".prompt{\n", " display: None;\n", "}\n", - ".text_cell_render h5 {\n", + ".text_cell_render .exercise {\n", " font-weight: 300;\n", - " font-size: 22pt;\n", + " /*font-size: 22pt;*/\n", " color: #4057A1;\n", " font-style: italic;\n", - " margin-bottom: .5em;\n", + " /*margin-bottom: .5em;\n", " margin-top: 0.5em;\n", - " display: block;\n", + " display: block;*/\n", + "}\n", + ".text_cell_render .example {\n", + " font-weight: 300;\n", + " color: #40A157;\n", + " font-style: italic;\n", "}\n", "\n", ".warning{\n", @@ -2567,13 +1834,13 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 5, + "prompt_number": 72, "text": [ - "<IPython.core.display.HTML at 0x2cdcbd0>" + "<IPython.core.display.HTML at 0x1caac50>" ] } ], - "prompt_number": 5 + "prompt_number": 72 }, { "cell_type": "code", @@ -2604,13 +1871,13 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 6, + "prompt_number": 73, "text": [ - "<IPython.core.display.HTML at 0x2cd73d0>" + "<IPython.core.display.HTML at 0x1d3d950>" ] } ], - "prompt_number": 6 + "prompt_number": 73 } ], "metadata": {}