Tutorial 3: HTML
A Simple HTML Page
<html> <head> <title>Greg's Pets</title> </head> <body> <!-- 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> </body> </html> |
A List of Greg's PetsI grew up with a Manx cat. His name was Stumpy. He would chase anything. |
Tables
<html> <body> <table border="1"> <tr> <td>northwest</td> <td>northeast</td> </tr> <tr> <td>southwest</td> <td>southeast</td> </tr> </table> </body> </html> |
|
fish.csv
Date,Species,Count 2012-05-01,marlin,3 2012-05-01,shark,1 2012-05-02,turtle,1 2012-05-02,marlin,2
fish.py
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>'
makepage.py
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: process_line(line) reader.close() print '</table>' def main(filenames): print '<html>' print '<body>' for f in filenames: process_file(f) print '</body>' print '</html>' filenames = sys.argv[1:] main(filenames)
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
loop.py: 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()
earl.py: 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)
kitty.py: 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))
nicole.py: 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
humphrey.py: 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) reader.close() print name, count else: count = n.count_lines(sys.stdin) print 'stdin', count
Homework: write a Python program called total.py
that adds up the values in a specified column of a data file.
python total.py 3 somefile.txt
, it adds up the numbers in column 3 (the first column is column 1, not column 0), and prints the total.python total.py 3
, it reads from standard input.python total.py 3 a.txt b.txt c.txt
, it prints the total of the third column for each file, and then the grand total (shown below).a.txt 27.2 b.txt 91.5 c.txt –3.0 Total 115.7