CSC326 Lecture 7 - CSC326 Persistent Programming i CSC326...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSC326 Persistent Programming i CSC326 Persistent Programming CSC326 Persistent Programming ii REVISION HISTORY NUMBER DATE DESCRIPTION NAME 1.0 2011-09 JZ CSC326 Persistent Programming iii Contents 1 Agenda 1 2 Persistent Programming 1 3 File 1 4 File names and Path 2 5 Catching Exception 2 6 Key-Value Store 3 7 Pickling 3 8 Client-Server Key-Value Store 4 9 Relational Database 8 10 Recap 9 CSC326 Persistent Programming 1 / 10 1 Agenda • File • Exception • Key-Value Store • Client Server Key-Value Store • Relational Database 2 Persistent Programming • So far we have seen – Imperative programming constructs – operating on data structures • Data structures are – Transient In memory – Lost if power is done – Not carried across different sessions of the same program • Persistent programs – Run for a long time (all the time) – Keep at least some data in perment storage (disk, flash etc) – Examples: OS, web servers 3 File • Opening file >>> fout = open( ’output.txt’ , ’w’ ) >>> print fout <open file ’output.txt’ , mode ’w’ at 0xb7eb2410> • Write file >>> line1 = "This her’s the wattle,\n" >>> fout.write( line1 ) • Close file >>> fout.close() • Formating – argment of write has to be a string CSC326 Persistent Programming 2 / 10 – format operator % – similar to printf in C, except in expression form >>> camels = 42 >>> ’%d’ % camels ’42’ >>> ’I have spotted %d camels.’ % camels ’I have spotted 42 camels.’ • If more than one, has to use tuple >>> ’In %d years I have spotted %d %s.’ % (3, 0.1, ’camels’ ) ’In 3 years I have spotted 0.1 camles.’ • Make sure to match types 4 File names and Path >>> import os >>> cwd = os.getcwd() >>> print cwd /home/jzhu >>> os.path.abspath( ’memo.txt’ ) ’/home/jzhu/memo.txt’ >>> os.path.exists( ’memo.txt’ ) True >>> os.path.isdir( ’memo.txt’ ) False >>> os.path.isfile( ’memo.txt’ ) False >>> os.path.listdir( cwd ) [ ’music’ , ’photos’ , ’memo.txt’ ] def walk(dir) : for name is os.listdir(dir) : path = os.path.join( dir, name ) if os.path.isfile(path) : print path else walk(path) 5 Catching Exception >>> fin = open( ’bad_file’ ) IOError: [Errno 2] No such file or directory: ’bad_file’ >>> fin = open( ’/etc/passwd’ , w ) IOError: [Errno 13] Permission deined: ’etc/passwd’ >>> fin = open( ’/home’ ) IOError: [Errno 21] Is a directory CSC326 Persistent Programming 3 / 10 try : fin = open( ’bad_file’ ) for line in fin : print line fin.clse() except : print ’Something went wrong’ 6 Key-Value Store • Persistent dictionary >>> import anydbm >>> db = anydbm.open( ’captions.db’ , ’c’ ) >>> db[ ’cleese.png’ ] = ’Photo of John Cleese’ >>> print db[ ’cleese.png’ ] = ’Photo of John Cleese’ Phote of John Cleese....
View Full Document

This note was uploaded on 02/20/2012 for the course CSC 326 taught by Professor Jzhu during the Fall '11 term at University of Toronto.

Page1 / 13

CSC326 Lecture 7 - CSC326 Persistent Programming i CSC326...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online