Create a new Python module called addressbook.py consisting of a class called Record that defines an address book record. The module should also offer several functions to enable address book record creation and manipulation. Import the module into your m

others

Description

Assessment Instructions:

Task

 

Create a new Python module called addressbook.py consisting of a class called Record that defines an address book record. The module should also offer several functions to enable address book record creation and manipulation. Import the module into your main code file, called main.py (this is the file you should be executing).

 

The Record class should consist of 4 properties: name, email, phone and address of a person/customer. These properties can be public values so that they are easily obtained from the Record object.

 

Example code:

 

class Record:

    def __init__(self, name, email, tel, address):

        self.name = name

        self.email = email

        self.tel = tel

        self.address = address

 

Record objects will be saved (serialized) to a binary file using the Python pickle module and this will be our address book database.

 

Example code:

 

# write a record

pickle.dump(r, f, pickle.HIGHEST_PROTOCOL)

 

# read a record

r = pickle.load(f)

 

(where r is a Record object and f is the address book file stream)

 

Define and code the following:

 

A function to open the address book file in “append binary” mode and add a Record. (Hint: use pickle.dump)

 

#Add customer record

addrecord(Record):

 

A function to open the address book file in “read binary” mode and fetch a customer Record by name. (Hint: use pickle.load) Note: this function must make use of the index discussed below and raise a ValueError exception if the index hasn’t been created or is empty.

 

#Get customer Record by name.

getrecord(Name):

     return Record

 

The addressbook module should also have a global variable named index defined as a Python dictionary. The keys will be the name property and the value should be the file position of that Record in the address book. Define the function below to build the index.

 

#Build index of key value pairs from the address book.

buildindex():

 

The function should open the address book file in “read binary” mode and use a while loop as in the example code:

 

pos=0

while True:

     try:

        <your code here to read position, load record and populate the index with record name and position value…>

     except EOFError:

        break

 

When you need to read or set the file position, use the following two methods of the file stream:

 

#Set file index to desired position

f.seek(pos)

 

#Get current file index

pos=f.tell()

 

(where f is the address book file stream, pos is the index position inside the file)

 

For your assignment, use the address book file provided (addrbook.bin). It comes loaded with 6 Record entries but you can add more to it using your own function.

 

There are example addressbook.py and main.py on Blackboard you can use as a starting guide.

 

*** End ***

 


Related Questions in others category