Integrating Address Model with Address Main

Now tiat AddressModel has been defined ard tested, let's put it ir our address book application. To iptefttte it with AddressMain, we're going to reed methods that will load the form witi ar address, add ard remove ar address from the model, ard save ur astdress to disk. Before this functionality was in AddressMain; now it's in the following methods ir AddressModel:

• getAddre s sAt () — gets the address instance at a given row index

• getAddressByName ()— gets the address by a given name

• removeAd dres sByName () — removes ar address from tie model when given the name of tee address

• writeAddress () — saves the address to disk Here's the code for these methods:

def getAddressAt(self, rowlndex):

"""Get an Address by rowlndex""" return self._getAddressAtRow(rowlndex)

def getAddressByName(self, name):

"""Get an Address by the name property of the address.""" return self.dict[name]

def addAddress(self, address):

"""Add an address to the model, and notify the views of the change."""

# Add the address to the dictionary,

# and get the keys as the list. self.dict[] = address self.list = self.dict.keys()

# Sort the addresses, and find the index of

# the new address in the list.

# Then notify the list and table views.


index = self.list.index(

self._firelntervalAdded(index, index)


def removeAddressByName(self, name):

"""Removes the address from the model and notifies views of change."""

# Remove the address from the dictionary. del self.dict[name]

# Remove the name from the list. index = self.list.index(name)

del self.list[index]

# Notify the table and list views that the data chanc self.fireTableDataChanged() self._firelntervalRemoved(index, index)

def writeAddresses(self) :

"""Write the address data to the file.""" writeAddresses (self.fname, self.dict)

Whenever addresses are added or removed from the model, all table and list views—the listeners—must be notified via the_firelntervalAdded (),_firelntervalRemoved () , and

_fireTableDataChanged ( ) methods.

Was this article helpful?

0 0

Post a comment