Skip to content

Instantly share code, notes, and snippets.

@gregcaporaso
Last active October 12, 2015 07:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gregcaporaso/3994000 to your computer and use it in GitHub Desktop.
Save gregcaporaso/3994000 to your computer and use it in GitHub Desktop.
IPython Notebook files used in Greg Caporaso's Fall 2012 BIO599 Computational Biology course. See the included README.md file for more details and licensing information.

IPython Notebook files used in Greg Caporaso's Fall 2012 BIO599 Computational Biology course.

These closely follow the Python Programming chapters of Practical Computing for Biologists. A lot of exercises can be found in Learn Python the Hard Way.

This work is licensed under the Creative Commons Attribution 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

Feel free to use or modify these notebooks, but please credit me by placing the following attribution information where you feel that it makes sense: Greg Caporaso, www.caporaso.us.

Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from sys import argv
usage = "Lecture25_example.py <name> <day>"
if len(argv) != 3:
print "ERROR: Incorrect number of arguments passed."
print "USAGE: " + usage
else:
script_name, name, day = argv
print "Hello " + name
print "Today is " + day
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "CaporasoLecture29"
},
"name": "CaporasoLecture29",
"nbformat": 2,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"source": "**Concepts of testing**\n\nToday we'll look at some very basic concepts of software testing. A great text on this subject is [*Test-driven development*](http://www.amazon.com/Test-Driven-Development-By-Example/dp/0321146530/ref=sr_1_1?ie=UTF8&qid=1354803092&sr=8-1&keywords=test+driven+development). We're going to work in the IPython Notebook to explore some concepts in testing. To write automated tests of your programs, you would use the ``unittest`` module in the python standard library, or the ``nose`` third-party module."
},
{
"cell_type": "code",
"collapsed": true,
"input": "gc = {'CTT': 'L', 'ACA': 'T', 'ACG': 'T', 'ATC': 'I', 'AAC': 'N',\n 'ATA': 'I', 'AGG': 'R', 'CCT': 'P', 'ACT': 'T', 'AGC': 'S', 'AAG': 'K',\n 'AGA': 'R', 'CAT': 'H', 'AAT': 'N', 'ATT': 'I', 'CTG': 'L', 'CTA': 'L',\n 'CTC': 'L', 'CAC': 'H', 'AAA': 'K', 'CCG': 'P', 'AGT': 'S', 'CCA': 'P',\n 'CAA': 'Q', 'CCC': 'P', 'TAT': 'Y', 'GGT': 'G', 'TGT': 'C', 'CGA': 'R',\n 'CAG': 'Q', 'TCT': 'S', 'GAT': 'D', 'CGG': 'R', 'TTT': 'F', 'TGC': 'C',\n 'GGG': 'G', 'GGA': 'G', 'TGG': 'W', 'GGC': 'G', 'TAC': 'Y',\n 'TTC': 'F', 'TCG': 'S', 'TTA': 'L', 'TTG': 'L', 'TCC': 'S', 'ACC': 'T',\n 'GCA': 'A', 'GTA': 'V', 'GCC': 'A', 'GTC': 'V', 'GCG': 'A','TAA': '*',\n 'GTG': 'V', 'GAG': 'E', 'GTT': 'V', 'GCT': 'A', 'GAC': 'D', 'CGT': 'R',\n 'GAA': 'E', 'TCA': 'S', 'ATG': 'M', 'CGC': 'R', 'TAG': '*', 'TGA': '*'}",
"language": "python",
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": true,
"input": "def translate(rna_seq,genetic_code=gc):\n \"\"\" \"\"\"\n protein_seq = \"\"\n for e in range(0,len(rna_seq),3):\n codon = rna_seq[e] + rna_seq[e+1] + rna_seq[e+2]\n aa = gc[codon]\n protein_seq = protein_seq + aa\n return protein_seq",
"language": "python",
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": "translate('ACCGTCGGATTACCGAAGGAA')",
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 5,
"text": "&apos;TVGLPKE&apos;"
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"source": "We'll use the ``assert`` command to define some tests. ``assert`` is like an ``if`` statement, but raises an error if the condition does not evaluate to ``True``."
},
{
"cell_type": "code",
"collapsed": false,
"input": "assert 42 < 100",
"language": "python",
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": "assert 42 > 100",
"language": "python",
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "pyerr",
"traceback": [
"<span class=\"ansired\">---------------------------------------------------------------------------</span>\n<span class=\"ansired\">AssertionError</span> Traceback (most recent call last)",
"<span class=\"ansigreen\">/home/ubuntu/&lt;ipython-input-15-7d014f98d471&gt;</span> in <span class=\"ansicyan\">&lt;module&gt;</span><span class=\"ansiblue\">()</span>\n<span class=\"ansigreen\">----&gt; 1</span><span class=\"ansiyellow\"> </span><span class=\"ansigreen\">assert</span> <span class=\"ansicyan\">42</span> <span class=\"ansiyellow\">&gt;</span> <span class=\"ansicyan\">100</span><span class=\"ansiyellow\"></span>\n",
"<span class=\"ansired\">AssertionError</span>: "
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"source": "You can have you error produce useful debugging information for the user as follows:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "assert 42 > 100, \"42 is obviously not greater than 100!\"",
"language": "python",
"outputs": [
{
"ename": "AssertionError",
"evalue": "42 is obviously not greater than 100!",
"output_type": "pyerr",
"traceback": [
"<span class=\"ansired\">---------------------------------------------------------------------------</span>\n<span class=\"ansired\">AssertionError</span> Traceback (most recent call last)",
"<span class=\"ansigreen\">/home/ubuntu/&lt;ipython-input-16-c0dfe7bee7d1&gt;</span> in <span class=\"ansicyan\">&lt;module&gt;</span><span class=\"ansiblue\">()</span>\n<span class=\"ansigreen\">----&gt; 1</span><span class=\"ansiyellow\"> </span><span class=\"ansigreen\">assert</span> <span class=\"ansicyan\">42</span> <span class=\"ansiyellow\">&gt;</span> <span class=\"ansicyan\">100</span><span class=\"ansiyellow\">,</span> <span class=\"ansiblue\">&quot;42 is obviously not greater than 100!&quot;</span><span class=\"ansiyellow\"></span>\n",
"<span class=\"ansired\">AssertionError</span>: 42 is obviously not greater than 100!"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"source": "As discussed in class, some the the things we want to test are the following:\n\n1. Basic functionality: does valid input give correct output.\n2. Error detection: is invalid input handled appropriately (remember the difference between failing quietly and failing loudly!).\n3. Boundary cases: What extreme values can be passed? Are they handled correctly?"
},
{
"cell_type": "code",
"collapsed": true,
"input": "",
"language": "python",
"outputs": [],
"prompt_number": "&nbsp;"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment