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.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.