Lecture 7

advertisement
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
• Dictionaries
Error handling
• Raise
def defensiveWithdraw (amount, balance):
if balance < amount:
raise ValueError, "insufficient funds"
balance -= amount
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
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
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
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()
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
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))
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
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()
The internal dictionaries
• Python maintains its own dictionaries for your variables
• Locals() gives you the local variables
• Correspondingly global() gives you the global variables
Download