University College London / April-May 2012
This course is now fully booked and has a long waiting list. If you would like to be informed of similar events in the future, please e-mail us on swc2012@ucl.ac.uk.
When: April 30-May 1, 2012. 9am to 5pm.
Where: University College London.
What: Our goal is to help scientists and engineers become more productive by teaching them basic computing skills like program design, version control, testing, and task automation. In this two-day boot camp, short tutorials will alternate with hands-on practical exercises. Participants will be encouraged both to help one another, and to apply what they have learned to their own research problems during and between sessions. There will be online follow up sessions for 6 to 8 weeks extending the material from the boot camp.
Who: The course is aimed at postgraduate students and other scientists who are familiar with basic programming concepts (like loops, conditionals, arrays, and functions) but need help to translate this knowledge into practical tools to help them work more productively.
Requirements: Participants must bring a laptop with a few specific software packages installed. (The list will be sent to participants a week before the boot camp.)
Content: The syllabus for this boot camp will include:
- using the shell to do more in less time
- using version control to manage and share information
- basic Python programming
- how (and how much) to test programs
- working with relational databases
counter.py
# Count data records in file. # Reads from standard input if no filename given. # Reads from a single file if filename given. # Title and comments don't count. import sys def count_records(reader): number = 0 expected = None for line in reader: if line.startswith('#-'): junk, expected = line.split() expected = int(expected) elif line.startswith('#'): pass elif line.startswith('D'): # FIXME pass else: number = number + 1 return number, expected if len(sys.argv) == 2: filename = sys.argv[1] source = file(filename, 'r') else: source = sys.stdin number, expected = count_records(source) source.close() if expected == None: print 'number of records:', number elif expected == number: print 'pass' else: print 'fail'
fun.py
def double(x): return 2 * x def add_three(x): return x + 3 def sub_one(x): return x - 1 def do_for_each(func, values): result = [] for v in values: temp = func(v) result.append(temp) return result start = [3, 7, 42, 96] doubled = do_for_each(double, start) added = do_for_each(add_three, start) subtracted = do_for_each(sub_one, start) print 'started with', start print 'doubled', doubled print 'incremented', added print 'subtracted', subtracted
Databases
Person
Login | LastName | FirstName |
skol | Kovalevskaya | Sofia |
mlom | Lomonosov | Mikhail |
dmitri | Mendeleev | Dmitri |
ivan | Pavlov | Ivan |
Project
ProjectId | ProjectName |
1214 | Antigravity |
1709 | Teleportation |
1737 | Time Travel |
Experiment
ProjectId | ExperimentId | NumInvolved | ExperimentDate | Hours |
1214 | 1 | 1 | 1.5 | |
1214 | 2 | 1 | 1889-11-01 | 14.3 |
1709 | 1 | 3 | 1891-01-22 | 7.0 |
1709 | 2 | 1 | 1891-02-23 | 7.2 |
1737 | 1 | 1 | 1900-07-05 | -1.0 |
1737 | 2 | 2 | 1900-07-05 | -1.5 |
Involved
ProjectId | ExperimentId | InvolvedId | Login |
1214 | 1 | 1 | mlom |
1214 | 2 | 1 | mlom |
1709 | 1 | 1 | dmitri |
1709 | 1 | 2 | skol |
1709 | 1 | 3 | ivan |
1709 | 2 | 1 | mlom |
1737 | 1 | 1 | skol |
1737 | 2 | 1 | skol |
1737 | 2 | 2 | ivan |
Sample Query
-- Get the people who worked on each project. select distinct Project.ProjectName, Involved.Login from Project join Involved where Project.ProjectId = Involved.ProjectId order by Involved.Login;
Homework
Homework
Get project name, person’s last name, experiment ID, and hours worked in one table.
select '# $Revision:$'; select '# db version' || VersionId from DatabaseVersion; select Project.ProjectName, Person.LastName, Experiment.ExperimentId, Experiment.Hours from Person join Project join Experiment join Involved where (Project.ProjectId = Experiment.ProjectId) and (Involved.ProjectId = Project.ProjectId) and (Involved.Login = Person.Login) and (Experiment.ExperimentId = Involved.ExperimentId);
Access from Python
import sqlite3 connection = sqlite3.connect("experiments.db") cursor = connection.cursor() cursor.execute("SELECT FirstName, LastName FROM Person;") results = cursor.fetchall(); for r in results: print r[0], r[1] cursor.close(); connection.close();
Nose tests for rectangle overlap
from overlap import overlap def test_overlap_on_one_corner(): assert overlap([0, 2, 0, 2], [1, 3, 1, 3]) == [1, 2, 1, 2] def test_no_overlap_at_all(): assert overlap([0, 1, 0, 1], [3, 4, 3, 4]) == None def test_one_inside_another(): assert overlap([0, 10, 0, 10], [2, 3, 2, 3]) == [2, 3, 2, 3] def test_overlap_along_one_edge(): assert overlap([0, 3, 0, 3], [3, 5, 0, 3]) == None def test_inside_on_one_axis(): assert overlap([0, 3, 0, 3], [1, 2, 1, 4]) == [1, 2, 1, 3]
Here’s another test for you. Where the rectangles make a cross
def test_cross():
assert overlap([0, 3, 1, 2],
[1, 2, 0, 3]) == [1, 2, 1, 2]
Hi Guys,
someone mentioned in class that one could use sqlite3 within python to access data from a database on a web site. However I never saw the syntax. Could someone
show this ?
many thanks
Farrukh
@Ed Ransley
Nice one. I was quite surprised to find that my code passes this! One test I added during the session was this:
def test_one_input_is_none():
assert overlap(None,
[0, 1, 0, 1]) == None
If we are using None as a return value for non-overlapping rectangles, it is quite likely to be used as an input at some stage, especially if we ever want to find the overlap of more than two rectangles.
@Farrukh Azfar
Greg did show an example, but there wasn’t time to follow it up. Rather than reproduce it here I’ll point you to the official documentation:
http://docs.python.org/library/sqlite3.html