Lab07 PDF
Lab07 PDF
Python programming
exercises, I
Riccardo Murri
Grid Computing Competence Center,
Organisch-Chemisches Institut,
University of Zurich
Reference: http://docs.python.org/library/unittest.html
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()
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()
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()
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()
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()
def test_sp_2(self):
(x,y) = sp(8, [2,1,9,4,4,56,90,3])
self.assertTrue((x,y) == (4,4))
-------------------------------------------
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.
Reference:
http://docs.python.org/library/stdtypes.html#file-objects
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.
Reference: http://docs.python.org/library/stringio.html
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
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
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