Download presentation
Presentation is loading. Please wait.
1
1 Exception-Handling Overview Exception: when something unforeseen happens and causes an error Exception handling – improves program clarity by removing error-handling code from the “main line” of the program – Improves a program’s fault tolerance Exception handler catches a raised/thrown exception try statement encloses code that potentially cause exceptions raise statement indicates an exception occurred
2
number1 = raw_input( "Enter numerator: " ) number2 = raw_input( "Enter denominator: " ) # attempt to convert and divide values try: number1 = float( number1 ) number2 = float( number2 ) result = number1 / number2 # float raises a ValueError exception except ValueError: print "You must enter two numbers" # division by zero raises a ZeroDivisionError exception except ZeroDivisionError: print "Attempted to divide by zero" # else clause's suite executes if try suite raises no exceptions else: print "%.3f / %.3f = %.3f" % ( number1, number2, result ) User might raise a ValueError by entering a string that cannot be converted to float Division by zero raises ZeroDivisionError exception
3
3 14.3 Files and Streams Python views files as sequential streams of bytes Each file ends with an end-of-file marker Opening a file creates an object associated with a stream 0123456789n-1... end-of-file marker Fig. 14.2 Python’s view of a file of n bytes.
4
4 Files and Streams Three file streams created when Python program executes – sys.stdin (standard input stream) – sys.stdout (standard output stream) – sys.stderr (standard error stream) Defaulted to keyboard, screen and screen, respectively ( raw_input uses stdin, print uses stdout ) Redirect: print >> file, “Yes sir we have no bananas”
5
5 Methods for reading a file
6
6 CD management cd.py
7
7 Implicitly calling the __str__ method of the cd class
8
8 Save cd collection to a file, line by line cd.py
9
9 File cdsave.dat cdtest.py Test Prince / Purple Rain (rating: 7.4) Oscar Peterson / Night Train (rating: 8.0) Beatles / Sgt. Pepper (rating: 9.6) Program output Prince Purple Rain 7.4 Oscar Peterson Night Train 8.0 Beatles Sgt. Pepper 9.6
10
10 Load cd collection from file, line by line Create new CD object and add to collection cd.py Remove the trailing newline from line
11
11 Testing the load method >>> from cd import CD, CDcollection >>> cds = CDcollection() >>> cds.load( 'cdsave.dat') >>> print cds Prince / Purple Rain (rating: 7.4) Oscar Peterson / Night Train (rating: 8.0) Beatles / Sgt. Pepper (rating: 9.6)
12
12 Extending class definition with cd comments Write each comment on a line of its own, with a suitable number of spaces cd2.py
13
13 Prince / Purple Rain (rating: 7.4) [Groundbreaking!] Oscar Peterson / Night Train (rating: 8.0) [Got it in a Starbucks in West L.A.] [Good album, especially the Gershwin songs.] Beatles / Sgt. Pepper (rating: 9.6) [Groundbreaking AND evergreen!!] cd2test.py
14
14 New save method Write each comment on a line of its own after artist, title and rating cd2.py
15
15 New load method..? Which attributes are which in the saved file?? No longer 3 lines per cd Change save method or Write super-intelligent load method Prince Purple Rain 7.4 Groundbreaking! Oscar Peterson Night Train 8.0 Got it in a Starbucks in West L.A. Good album, especially the Gershwin songs. Beatles Sgt. Pepper 9.6 Groundbreaking AND evergreen!!
16
16 New load/save methods using module cPickle cd_pickle.py Write entire object (list of CD objects) to file Easy to load object back into memory if you’re completely sure what kind of object the file contains
17
17 Testing the pickle save method cd_pickletest.py.. S'Purple Rain' p6 sS'comments' p7 (lp8 S'Groundbreaking!' p9 asS'artist' p10 S'Prince' p11 sba(icd_pickle CD.. Excerpt from file cd_picklesave.dat
18
18 Testing the load method cd_pickletest_load.py Prince / Purple Rain (rating: 7.4) [Groundbreaking!] Oscar Peterson / Night Train (rating: 8.0) [Got it in a Starbucks in West L.A.] [Good album, especially the Gershwin songs.] Beatles / Sgt. Pepper (rating: 9.6) [Groundbreaking AND evergreen!!] Beatles / A Hard Day's Night (rating: 6.8) [First album with all Lennon/McCartney songs] We need a CDcollection object for which we can call the load method to retrieve the pickled object
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.