python programming exercises, i - gc3: grid … · grid computing competence center python...

25
Grid Computing Competence Center Python programming exercises, I Riccardo Murri Grid Computing Competence Center, Organisch-Chemisches Institut, University of Zurich Nov. 10, 2011

Upload: buidat

Post on 08-Sep-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

Grid Computing Competence Center

Python programmingexercises, IRiccardo MurriGrid Computing Competence Center,Organisch-Chemisches Institut,University of Zurich

Nov. 10, 2011

Today’s class

Getting your feet wet with Python programming.(Review of yesterday’s 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

Yesterday’s homework

Write a function sp(C,L) that returns a pair (tuple)of elements from a list L whose sum is integer C.

(Unspecified: what should it do when no pairs of valuesfrom L sums to C?)

Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

Testing solutions

Rather than inspecting each solutions’ code, we shallwrite a test class, using Python standard library unittesting facility.

Your solution is correct if it passes the test.

Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

Python’s unittest module

Python’s unittest requires one to define a subclassof unittest.TestCase.

All methods whose name starts with test areexecuted; if none errors out, the test is passed.

Test methods should use methods assertEqual,assertTrue, etc. defined by class TestCase to checkif test conditions are satisfied.

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):

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()

This syntax allows you toimport a module but

assign it a different name.

Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

import unittest as ut

class SpTest(ut.TestCase):

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()

Declare a new class,ihneriting from theut.TestCase class.

Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

import unittest as ut

class SpTest(ut.TestCase):

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()

A method declaration looksexactly like a function

definition. Every methodmust have at least oneargument, named self.

More on self

Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

import unittest as ut

class SpTest(ut.TestCase):

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()

self is a reference to theobject instance (like, e.g.,

this in Java). It is used toaccess attributes andinvoke methods of the

object itself.More on self

Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

import unittest as ut

class SpTest(ut.TestCase):

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()

This invokes methodassertEqual of the

current object.(Where is it defined?)

Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

import unittest as ut

class SpTest(ut.TestCase):

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 idiom: executeut.main() iff this file isthe main script, i.e., it is

not being read as amodule.

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) ... oktest_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 thefile located at path. Mode is one of ’r’, ’w’ or ’a’ forreading, writing (truncates on open), appending. Youcan add a ‘+’ character to enable read+write (othereffects being the same).

close()Close an open file.

for line in file objLoop over lines in the file one by one.

Reference:http://docs.python.org/library/stdtypes.html#file-objects

Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

Exercise 1

1. Implement a function count_lines(stream), thatreturns the number of lines in stream (a file-likeobject).

2. Implement a function keys_for_value(D, val),which returns the list of keys that are associated tovalue 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 acollection.

x in SEvaluates to True if x is equal to a value contained inthe S sequence (list, tuple, set).

x in DEvaluates to True if x is equal to a key in the D

dictionary.

x in TEvaluates 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 file-like object from astring:

>>> from StringIO import StringIO>>> # create a file-like object>>> stream = StringIO("python")

The read(n) method can be used to read at most nbytes from a file-like object:

>>> s = stream.read(2)>>> s == ’py’True

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

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 returnsa dictionary, mapping each character into the count of itsoccurrences in stream. Characters that do not occur in theinput should not be present in the returned dictionary. Youcan assume stream is formed of ASCII characters.

1. Write test cases for the function, before writing it. Atthe very least, check:

– That count_chars() on an empty stream returnsan empty dictionary;

– That count_chars() on a stream whose content isthe string ’aaaaaa’ returns the dictionary{’a’:6}

– That count_chars() on a stream whose content isthe 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 alllowercase / turned all uppercase.

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

s.startswith(t), s.endswith(t)Return True if t is the initial/final 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 thenumber of distinct words occurring in stream. Wordsare 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 / Changethe 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 tocontain the leaf.

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

Filesystem operations, II

These functions are available from the os.pathmodule.

os.path.exists(path), os.path.isdir(path)Return True if path exists / is a directory / is aregular file.

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 thelast / 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 followingcommand-line:

python rename.py EXT1 EXT2 DIR [DIR ...]

where:

ext1,ext2 Are file name extensions (without theleading dot), e.g., jpg and jpeg.

dir Is directory path; possibly, manydirectories names can be given on thecommand-line.

The rename.py command should rename all files indirectory DIR, that end with extension ext1 to endwith 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 Google’s Pythonclass: 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 selfas first argument.

However, you do not specify it when calling a method:it’s 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 methodsor 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 tothe LEGB rule:

L Local scope: any names defined in the currentfunction;

E Enclosing function scope: names defined inenclosing functions (outermost last);

G global scope: names defined in the toplevel ofthe current module;

B Built-in names (i.e., Python’s builtinsmodule).

Any name that is not in one of the above scopes mustbe qualified.

So you have to write self.assertEqual to call a methodon this object, ut.TestCase to mean a class defined inmodule ut, etc.Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011