0

I'm doing creating a address book for a computer science class, where we are supposed to have a list of objects "contact" written to a file when the program closes and read from the file when the program starts. However, after the data is written to the file, once it is read it does not seem to keep its object form.

import pickle

class Contact:
                        #Creating the class with the attributes I want
def __init__(self, firstname, lastname, number, areacode, city):
    self.firstname = firstname
    self.lastname = lastname
    self.number = number
    self.areacode = areacode
    self.city = city

def __str__(self):
    return  "({0} {1}, {2}, {3}, {4})".format(self.firstname, self.lastname, self.number, self.areacode, self.city)
                        #reading the object
def opendata():
    readable = []
    addresses = []
    A = open("Address_book", "rb+")
    addresses = A.read()
    A.close
    B = len(addresses)
    if B != 0:
        readable = pickle.loads(addresses)
        return readable
    else:
        return addresses

def savedata(file):
    addresses = open("Address_book", "wb+")
    temp = pickle.dumps(file)
    addresses.write(temp)
    addresses.close()

def Address_manager():
    Address = []
    fromfile = opendata()
    Address.append(fromfile)
    while True:
        print("add = add contact, modify = modify contact, search = search contacts, end = close program, print = print full list")
        A = input("What do you want to do?: ")
        if A == "end":
            savedata(Address)
            break
        else:
            if A == "print":
                for i in Address:
                    print(str(i))
                    print("")
                continue
            elif A == "add":
                Address.append(add_contact())
                print("")
                continue
            elif A == "search":
                lists2 = search_contact(Address)
                A = 0
                for i in lists2:
                    A += 1
                    print(A, str(i))
                    print("")
                print("")
                continue
            elif A == "modify":
                modified = modify_contact(Address)
                Address.append(modified)
                print("")
                continue

def add_contact():
    while True:
        try:
            A = Contact(input("First name: "), input("Last name: "), input("Phone Number: "), input("Area Code: "), input("City: "))
            print(str(A))
            B = input("Is this right?: (y/n)")
            if B == "y":
                print("Well done?")
                return A
            else:
                print("Try Again")
                continue



        except:
            print("bad data")

If I try to "print" the list after getting it from the file it prints them in the wrong form. What am I doing wrong and why?

Edit 1: I apologize for the less than efficient code.

Edit 2: Added function to add contact

Matthew I
  • 9
  • 2
  • 2
    What do you mean, the wrong form? And could you provide a short example of use so that we can reproduce the behavior? – Roberto May 04 '16 at 19:05
  • If you add a contact, then end the program and start it up again and try to print you get something like: <__main__.Contact object at 0x000000000382E9E8>] – Matthew I May 04 '16 at 19:14
  • Can you include the series of commands you are using so that I can reproduce this? I'm not sure what the `file` argument to you savedata() method is. – Tom Sitter May 04 '16 at 19:24
  • 2
    @MatthewI, the issue with it printing out that way is that you need to overwrite the `__repr__(self)` method. For more information, [this answer](http://stackoverflow.com/a/2626364/6061947) has some very good information regarding the differences between `__str__` and `__repr__`. Which in your case could just call your `__str__` method. – Cory Shay May 04 '16 at 19:26
  • @Tom Sitter If you run the Address_manager() you can add a contact. However I'm not sure how to check whether the is an existing "Address_book" file. In Address_manager() it generally refers to the Address list of Contacts – Matthew I May 04 '16 at 19:35
  • This program is difficult to debug, my general suggestion would be to scrap pickle and use an array of dicts as your address book which you can read/write to a JSON file -- this will be easier to work with and allow you to open and read the file in a text editor for debugging. To check is a file exists, you can use the `os` module's `os.path.isfile('Address_book')` – Tom Sitter May 04 '16 at 20:35

0 Answers0