Regular expression: First review: . match any character [abc] match any character between the brackets [^abc] match any character except those between brackets [0-5] match any character in the range 0 ... 5 inclusive * match 0 or more of preceding + match 1 or more of preceding ? match 0 or 1 of preceding {n} match exactly n {n,} match at least n {n,m} match between n and m of preceeding ^ anchor to beginning of line $ anchor to end of line () grouping | alterntation \d [0-9] \w [a-zA-Z0-9_] \s [ \t\v\n\f\r] Puzzles -------- 1. binary strings that are even numbers [01]*0 2. matching an html anchor tag except [exception [, target]]: [else: ] where things in [] are optional. A try/except block can have more one except clause. An except without an exception is a "catch-all" clause. The exception is the exception class to catch; target is bound to the instance of the exception that was thrown. For example, suppose we are trying to open a file to read some information. we'd like to handle errors gracefully and we know that open will generate an IOError if something strange happens. f = open(filename) lines = f.readlines() f.close() We can enclose this in a try/except block: try: f = open(filename) lines = f.readlines() f.close() except IOError, ex: print >> sys.stderr, "Error reading file:", ex else: print lines The else clause is executed if nothing goes wrong. In this case, if the file is read successfully then lines will be printed. If the file is not opened, then we don't bother printing lines. Without the else clause, lines won't be defined unless we used it prior to the try/except block, so we'd get another error or we'd print old data. There is another form of exception handling: try: finally: In this form, no except clauses are allowed. The code in the finally clause is always excecuted, regardless of what happens in the try clause. The finally clause is called a clean up handler. f = open(filename) try: foo(f) finally: f.close() If foo generates an exception, we just clean-up. An exception that is generated in the try clause is re-raised after the except. This block might actually look like this: try: f = open(filename) try: foo(f) finally: f.close() except IOError, ex: print 'Error with file:', ex ( So if foo raised an exception that wasn't an IOError, it wouldn't be caught unless we put in another handler or a catch-all (or had something higher up that dealt with exceptions) ) [ Why would we want something like this? For releasing resources! ] [ what happens if the finally code raises an exception? ] How do we signal that some sort of wierdness has occurred? We can use the raise statement: raise MyException("some argument") Exceptions in Python are instance of classes that are derived from the Exception class. This leads nicely to how to define custom exceptions. Suppose we were designing some piece of code and decided that we needed an exception to signal some special error. If our error was a more specific version of some existing exception, we could just do something like: class MyException(ExistingException) "Docs for MyException" pass Now, suppose this error wasn't a more specific version of an existing version or we wanted to include information in the exception, such as an error code class MyException(Exception): def __init__(self, code, *args): Exception.__init__(self, *args) self.code = code def __str__(self): return "%s: %s" % (repr(self.code), self.args) try: raise MyException(123,"bar") except MyException, e: print e ---> 123: ('bar',) Exception objects have a member called args, which stores all extra arguments. Reading from standard input --------------------------- Remind the students about what it means to read from standard input in java. Talk about how setting up a BufferedReader to read from System.in means that they can read from the keyboard or from redirected input. Point out that you can also pipe the standard output from one command to be the standard input to another. For example, instead of saying, java LargestAnagramSet < sometestfile they could say cat sometestfile | java LargestAnagramSet Now talk about doing this in python. Here is an example where we read all the lines from the redirected input file. import sys # need to import sys for line in sys.stdin: # reads to None print line Commandline arguments --------------------- The basics of commandline arguments are similar to c or java. Again in sys module. argv is a list of the arguments. argv[0] is the program name len(argv) is the number of arguments including the initial command import sys for i in range(len(sys.argv)): print "argument ",i,"is",argv[i]