0% found this document useful (0 votes)
113 views25 pages

Lab07 PDF

The document discusses a Python programming class held by Riccardo Murri at the Grid Computing Competence Center at the University of Zurich. The class covers testing solutions with Python's unittest module, file input/output in Python, string operations, and filesystem operations. Examples are provided for writing tests, counting lines in a file, mapping character counts in a string, and counting distinct words in a stream.

Uploaded by

DjSunny Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views25 pages

Lab07 PDF

The document discusses a Python programming class held by Riccardo Murri at the Grid Computing Competence Center at the University of Zurich. The class covers testing solutions with Python's unittest module, file input/output in Python, string operations, and filesystem operations. Examples are provided for writing tests, counting lines in a file, mapping character counts in a string, and counting distinct words in a stream.

Uploaded by

DjSunny Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Grid Computing Competence Center

Python programming
exercises, I
Riccardo Murri
Grid 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 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


Python’s unittest module

Python’s unittest requires one to define 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. defined by class TestCase to check
if test conditions are satisfied.

Reference: http://docs.python.org/library/unittest.html

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


This syntax allows you to
import unittest as ut import a module but
assign it a different name.
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 II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011


Declare a new class,
import unittest as ut ihneriting from the
ut.TestCase class.
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 II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011


A method declaration looks
import unittest as ut exactly like a function
definition. Every method
class SpTest(ut.TestCase): must have at least one
argument, named self.
def test_sp_1( self ): More on 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
import unittest as ut object instance (like, e.g.,
this in Java). It is used to
class SpTest(ut.TestCase): access attributes and
invoke methods of the
def test_sp_1(self): object itself.
(x, y) = sp(100, [5, 75, 25]) More on self

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


This invokes method
import unittest as ut assertEqual of the
current object.
class SpTest(ut.TestCase): (Where is it defined?)

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


Python idiom: execute
import unittest as ut ut.main() iff this file is
the main script, i.e., it is
class SpTest(ut.TestCase): 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
file 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 file.

for line in file obj


Loop 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), that


returns the number of lines in stream (a file-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 file-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 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 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/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 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 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 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 file 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 files 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 Google’s 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 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 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 defined in the current
function;
E Enclosing function scope: names defined in
enclosing functions (outermost last);
G global scope: names defined in the toplevel of
the current module;
B Built-in names (i.e., Python’s builtins
module).

Any name that is not in one of the above scopes must


be qualified.

So you have to write self.assertEqual to call a method


on this object, ut.TestCase to mean a class defined in
module ut, etc.
Python II R. Murri, Large Scale Computing Infrastructures, Nov. 10, 2011

You might also like