12
CIT 590 Intro to Programming Lecture 7

CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

Embed Size (px)

Citation preview

Page 1: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

CIT 590Intro to Programming

Lecture 7

Page 2: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

Agenda• Configuring IDLE (now that your code is getting huge)• Exceptions• Testing for exceptions – the weird self.assertRaises• Dictionaries

Page 3: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

Error handling• Raise

def defensiveWithdraw (amount, balance): if balance < amount: raise ValueError, "insufficient funds" balance -= amount

Page 4: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

Testing our error checking. How?• Unit Test!!!

• self.assertRaises

• The syntax is a little weird • Argument 1 – type of error you are asserting gets raised• Argument 2 – the function name (name only!)• Argument 3 – the arguments to the function

Page 5: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

Dictionary• Dictionaries consist of key, value pairs• Also know as Hashmaps, associative arrays in other

languages

Initialized with dictionary = {}

And then we can add key, value pairs as follows

dictionary[‘name’] = ‘Arvind’

dictionary[‘age’] = 92• Very efficient way of storing sparse data

A lot of matrices and vectors that come up in probability are sparse, so you could use an integer key and store values in that manner

Page 6: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

Live coding – word count example• See wordCounter.py in the repository

• Modifications will be made in class

• We will also write unit tests with dictionaries

Page 7: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

Dictionary update operation. Copying dictionaries

• dict_one = {‘abc’: 3, ‘def’:7, ‘xyz’: 9}• dict_two = {‘def’: 5, ‘pdq’ : 4}• dict_one.update(dict_two)

As with lists or for that matter any kind of assignment, you need to be careful when you do assignment, since that is done by reference

So dict1 = dict2 will make both dictionaries refer to the same value

If you want to make what is generally called a ‘shallow copy’, you need to use the copy method

dict 2 = dict1.copy()

Page 8: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

Altering dictionaries by passing them into functions• Remember that arguments are passed into functions by

reference, so you could get some unintended consequences if you are not careful

• Similar to lists, dictionaries are passed ‘by reference’ to functions

Page 9: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

Initializing a dictionary version 2• The dict() function can convert a list of 2 element tuples

into dictionaries• Very useful when you want to initialize a dictionary by

using 2 lists, one of which has

keys = [‘Arvind’, ‘Aashish’]

values = [33, 28]

dict(zip(keys, values))

Page 10: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

Looping over a dictionary• For e in dict:

• This will loop over the keys

• For e in dict.values():• As is somewhat obvious, this will loop over the values

• Since there is no guarantee about what order the keys are looped over, you might sometimes want to call the sorted function

Page 11: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

Persistent variables• The shelve module allows data to persist without using

explicit file commands

import shelve

Data = shelve.open(“database”)

this will make an implicit file called database

And then data from that point can basically be treated just as any other dictionary

Remember to close the shelf before quitting by using

Data.close()

Page 12: CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises

The internal dictionaries• Python maintains its own dictionaries for your variables• Locals() gives you the local variables• Correspondingly global() gives you the global variables