Monterey Bay Aquarium Research Institute

March 26-27, 2012
9:00 am - 5:00 pm

Tutorial 3: HTML

A Simple HTML Page

    <title>Greg's Pets</title>
    <!-- this is a comment -->
    <h1>A List of Greg's Pets</h1>
    <p>I grew up with a Manx cat. His name was Stumpy.</p>
    <p>He would chase <em>anything</em>.</p>

A List of Greg's Pets

I grew up with a Manx cat. His name was Stumpy.

He would chase anything.


    <table border="1">
northwest northeast
southwest southeast



import sys

print '<html>'
print '<body>'
print '<table border="1">'

for line in sys.stdin:
    line = line.strip()
    all_fields = line.split(',')
    print '<tr>'
    for field in all_fields:
        print '<td>', field, '</td>'
    print '</tr>'

print '</table>'
print '</body>'
print '</html>'

import sys

def process_line(line):
    print '<tr>'
    fields = line.strip().split(',')
    for f in fields:
        print '<td>'
        print f
        print '</td>'
    print '</tr>'

def process_file(filename):
    print '<h1>'
    print filename
    print '</h1>'
    print '<table border="1">'
    reader = open(filename, 'r')
    for line in reader:
    print '</table>'

def main(filenames):
    print '<html>'
    print '<body>'
    for f in filenames:
    print '</body>'
    print '</html>'

filenames = sys.argv[1:]

Tutorial 2

Exercise 2

Tutorial 1

General description:

This two-day course will cover a range of computing skills important to scientists and data-processing. Specifically we will focus on the command-line (a little bit), version control, python (a lot), databases, and developing pipelines for streamlining data analysis. Follow-up training will be available through book and online course material.

You will find more general information about the bootcamps at the link above.

Bring your own laptop, and if you need to borrow one, contact {haddock} [at] mbari (dot) org.

Registration: The course runs all day for two days, and the later content depends on earlier lessons, so please plan to attend both days.

Unix commands covered

ls – list contents of a directory
cd – move to a named directory
cp – copy file or folder

history – show command history

up-arrow – move back up through history
tab – autocomplete file name
> – redirect output to a file name
* – wildcard: match any symbol

| – pipe: send output to another command

mv – move (not copy)
mkdir – make a directory
rm – remove a file

uniq – select unique adjacent lines

man – get help on a command
cut – cut columns from a file –d to specify delimiter, –f to tell which column

head – show first –x lines of a file
tail – show last –x lines of a file

less – view a file page by page. space to advance, q to quit
fgrep – search for a pattern in a file averaging the numbers in a file

# Count number of lines in data.txt.
reader = open('small.txt', 'r')
total = 0.0
num_lines = 0
for line in reader:
    line = line.strip()
    if len(line) > 0:
        num_lines = num_lines + 1
        first, second = line.split()
        total = total + float(second)
print 'average is', total / num_lines
reader.close() importing things

from math import sqrt, log, atan
from math import sin as something_else
print 'square root of 10', sqrt(10)
print 'and log is', log(10)
print 'and something else is', something_else(10) defining and using functions

def double(x):
    print 'in double, x is', x
    return 2 * x

def triple(x):
    print 'in triple, x is', x
    return 3 * x

print triple(double(5)) putting a utility function in a file

def count_lines(source):
    """Count the number of lines in a file.
       Takes an open handle and returns an integer
       Does not close the file/handle"""
    count = 0
    for line in source:
        count = count + 1
    return count a full–blown Unix utility

import sys
import nicole as n

if len(sys.argv) > 1:
    for name in sys.argv[1:]:
        reader = open(name, 'r')
        count = n.count_lines(reader)
        print name, count

    count = n.count_lines(sys.stdin)
    print 'stdin', count

Homework: write a Python program called that adds up the values in a specified column of a data file.

a.txt 27.2
b.txt 91.5
c.txt –3.0
Total 115.7