Sie sind auf Seite 1von 25

Grid Computing Competence Center

Python programming exercises, I


Riccardo Murri Grid Computing Competence Center, Organisch-Chemisches Institut, University of Zurich

Nov. 10, 2011

Todays class

Getting your feet wet with Python programming. (Review of yesterdays stuff.)
These slides are available for download from: http://www.gc3.uzh.ch/teaching/lsci2011/lab07.pdf

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

Yesterdays homework

Write a function sp(C,L ) that returns a pair (tuple) of elements from a list L whose sum is integer C. (Unspecied: what should it do when no pairs of values from L sums to C?)

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

Testing solutions

Rather than inspecting each solutions code, we shall write a test class, using Python standard library unit testing facility. Your solution is correct if it passes the test.

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

Pythons unittest module

Pythons unittest requires one to dene a subclass of unittest.TestCase. All methods whose name starts with test are executed; if none errors out, the test is passed. Test methods should use methods assertEqual, assertTrue, etc. dened by class TestCase to check if test conditions are satised.
Reference: http://docs.python.org/library/unittest.html

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

import unittest as ut class SpTest(ut.TestCase):

This syntax allows you to import a module but assign it a different name.

def test_sp_1(self): (x, y) = sp(100, [5, 75, 25]) self.assertEqual((x,y), (75, 25)) def test_sp_2(self): (x,y) = sp(8, [2,1,9,4,4,56,90,3]) self.assertTrue((x,y) == (4,4)) if __name__ == "__main__": ut.main()

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

import unittest as ut class SpTest(ut.TestCase):

Declare a new class, ihneriting from the ut.TestCase class.

def test_sp_1(self): (x, y) = sp(100, [5, 75, 25]) self.assertEqual((x,y), (75, 25)) def test_sp_2(self): (x,y) = sp(8, [2,1,9,4,4,56,90,3]) self.assertTrue((x,y) == (4,4)) if __name__ == "__main__": ut.main()

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

import unittest as ut class SpTest(ut.TestCase):

A method declaration looks exactly like a function denition. Every method must have at least one argument, named self.
More on self

def test_sp_1( self ): (x, y) = sp(100, [5, 75, 25]) self.assertEqual((x,y), (75, 25)) def test_sp_2(self): (x,y) = sp(8, [2,1,9,4,4,56,90,3]) self.assertTrue((x,y) == (4,4)) if __name__ == "__main__": ut.main()

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

self is a reference to the object instance (like, e.g., import unittest as ut this in Java). It is used to access attributes and class SpTest(ut.TestCase): invoke methods of the def test_sp_1(self): object itself.
(x, y) = sp(100, [5, 75, 25]) self .assertEqual((x,y), (75, 25)) def test_sp_2(self): (x,y) = sp(8, [2,1,9,4,4,56,90,3]) self.assertTrue((x,y) == (4,4)) if __name__ == "__main__": ut.main()
More on self

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

import unittest as ut class SpTest(ut.TestCase):

This invokes method assertEqual of the current object. (Where is it dened?)

def test_sp_1(self): (x, y) = sp(100, [5, 75, 25]) self.assertEqual((x,y), (75, 25)) def test_sp_2(self): (x,y) = sp(8, [2,1,9,4,4,56,90,3]) self.assertTrue((x,y) == (4,4)) if __name__ == "__main__": ut.main()

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

import unittest as ut class SpTest(ut.TestCase):

Python idiom: execute ut.main() iff this le is the main script, i.e., it is not being read as a module.

def test_sp_1(self): (x, y) = sp(100, [5, 75, 25]) self.assertEqual((x,y), (75, 25)) def test_sp_2(self): (x,y) = sp(8, [2,1,9,4,4,56,90,3]) self.assertTrue((x,y) == (4,4)) if name == " main ": ut.main()

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

Time to check!
0. Download the test code from http: //www.gc3.uzh.ch/teaching/lsci2011/lab06/sptest.py 1. Add your sp function to it. 2. Run the script: python sptest.py --verbose 3. If you get the following output: congratulations! your sp implementation works as expected. test_sp_1 (__main__.SpTest) ... ok test_sp_2 (__main__.SpTest) ... ok ------------------------------------------Ran 2 tests in 0.000s OK
Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

File I/O

open(path,mode) Return a Python file object for reading or writing the le located at path. Mode is one of r, w or a for reading, writing (truncates on open), appending. You can add a + character to enable read+write (other effects being the same). close() Close an open le. for line in file obj Loop over lines in the le one by one.
Reference: http://docs.python.org/library/stdtypes.html#le-objects
Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

Exercise 1

1. Implement a function count_lines(stream), that returns the number of lines in stream (a le-like object). 2. Implement a function keys_for_value(D, val), which returns the list of keys that are associated to value val in dictionary D.

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

The in operator

Use the in operator to test for presence of an item in a collection.


x in S

Evaluates to True if x is equal to a value contained in the S sequence (list, tuple, set).
x in D

Evaluates to True if x is equal to a key in the D dictionary.


x in T

Evaluates to True if x is a substring of string T.

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

Strings to streams and back


The StringIO module creates a le-like object from a string:
>>> from StringIO import StringIO >>> # create a file-like object >>> stream = StringIO("python")

The read(n) method can be used to read at most n bytes from a le-like object:
>>> s = stream.read(2) >>> s == py True

If n is omitted, read() reads until end-of-le.


Reference: http://docs.python.org/library/stringio.html
Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

Exercise 2
Implement a function count_chars(stream), that returns a dictionary, mapping each character into the count of its occurrences in stream. Characters that do not occur in the input should not be present in the returned dictionary. You can assume stream is formed of ASCII characters. 1. Write test cases for the function, before writing it. At the very least, check: That count_chars() on an empty stream returns an empty dictionary; That count_chars() on a stream whose content is the string aaaaaa returns the dictionary {a:6} That count_chars() on a stream whose content is the string ababab returns the dictionary {a:3, b:3} 2. Verify that the test cases fail. 3. Write the function, and check that all tests pass.
Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

Operations on strings
Assume s is a Python str object.
s.capitalize(), s.lower(), s.upper()

Return a copy of the string capitalized / turned all lowercase / turned all uppercase.
s.split(t)

Split s at every occurrence of t and return list of parts. If t is omitted, split on whitespace.
s.startswith(t), s.endswith(t)

Return True if t is the initial/nal substring of s.


Reference: http://docs.python.org/library/stdtypes.html#string-methods

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

Exercise 3

Implement a function count_words, that counts the number of distinct words occurring in stream. Words are delimited by whitespace; case does not matter.

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

Filesystem operations
These functions are available from the os module. os.getcwd(), os.chdir(path) Return the path to the current working directory / Change the current working directory to path. os.listdir(dir) Return list of entries in directory dir (omitting . and ..) os.mkdir(path) Create a directory; fails if the directory already exists. Assumes that all parent directories exist already. os.makedirs(path) Create a directory; no-op if the directory already exists. Creates all the intermediate-level directories needed to contain the leaf. Reference: http://docs.python.org/library/os.html
Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

Filesystem operations, II
These functions are available from the os.path module.
os.path.exists(path), os.path.isdir(path)

Return True if path exists / is a directory / is a regular le.


os.path.basename(path), os.path.dirname(path)

Return the base name (the part after the last / character) or the directory name (the part before the last / character).
os.path.abspath(path)

Make path absolute (i.e., start with a /).


Reference: http://docs.python.org/library/os.path.html
Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

Exercise 4
Write a Python program rename.py with the following command-line:
python rename.py EXT1 EXT2 DIR [DIR ...]

where: ext1,ext2 Are le name extensions (without the leading dot), e.g., jpg and jpeg. dir Is directory path; possibly, many directories names can be given on the command-line. The rename.py command should rename all les in directory DIR, that end with extension ext1 to end with extension ext2 instead.

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

More exercises...

(In case you want to practice.) Basic Python Exercises from Googles Python class: http://code.google.com/edu/languages/ google-python-class/exercises/basic.html The Python Challenge: http://www.pythonchallenge.com/ Python Koans: https://github.com/gregmalcolm/python koans

Python II

R. Murri,

Large Scale Computing Infrastructures,

Nov. 10, 2011

The self argument


Every method of a Python object always has self as rst argument. However, you do not specify it when calling a method: its automatically inserted by Python:
>>> class ShowSelf(object): ... def show(self): ... print(self) ... >>> x = ShowSelf() # construct instance >>> x.show() # self automatically inserted! <__main__.ShowSelf object at 0x299e150>

The self variable is a reference to the object itself. You need to use self when accessing other methods or attributes of the object.
Back to Unit Testing
Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

Name resolution rules


Within a function body, names are resolved according to the LEGB rule: L Local scope: any names dened in the current function; E Enclosing function scope: names dened in enclosing functions (outermost last); G global scope: names dened in the toplevel of the current module; B Built-in names (i.e., Pythons module). builtins

Any name that is not in one of the above scopes must be qualied. So you have to write self.assertEqual to call a method on this object, ut.TestCase to mean a class dened in module ut, etc.
Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

Das könnte Ihnen auch gefallen