954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Address Book Python Project?!

Hi everyone! I'm a newbie here.

I have a problem:
I need to create the address book in python that include name, phone number, email, address, etc...The information in the address book can able to delete or change.
I will give more information later!

Can you help me? I need to solve this problem quickly!

Thanks a lot!!

GothLolita
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 0
 

Start by making a function that allows you to read and write the data from/into text files.

SgtMe
Nearly a Posting Virtuoso
1,205 posts since Oct 2009
Reputation Points: 68
Solved Threads: 85
 

If your data has be in text files, do what SgtMe suggested.
Alternatively, look into sqlite3 python module. It would allow you to create a real database and run sql statements against it to add, modify, and delete records.

sergb
Junior Poster
105 posts since May 2010
Reputation Points: 17
Solved Threads: 32
 

That would be better overal, sergb, but I feel that GothLolita should start off using text-files. You can use this to have test data for testing your program, and add full databse support later.

SgtMe
Nearly a Posting Virtuoso
1,205 posts since Oct 2009
Reputation Points: 68
Solved Threads: 85
 

help me in making address book!

bonethugs
Newbie Poster
2 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

Functions to add, remove and view entries to the address book, values stored in a pickled dictionary.

I'm eager to help, but I want to see some code effort from you.

Cheers and Happy coding

Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
 

Use either a pickled dictionary as said by Beat_Slayer or if you want portability, use json module of python. (JavaScript Object Notation)

techie1991
Junior Poster in Training
72 posts since Feb 2010
Reputation Points: 36
Solved Threads: 9
 

I'll get you a start point.

try:
    import cPickle as pickle
except:
    import pickle


class address_book():

    def __init__(self):
        try:
            self.__db = open('addressbook.db', 'rb')
            self.__entries = pickle.load(self.__db)
            self.__db.close()
        except:
            self.__db = open('addressbook.db', 'wb')
            self.__entries = {}
            self.__db.close()

    def __update(self):
        self.__db = open('addressbook.db', 'wb')
        pickle.dump(self.__entries, self.__db, -1)
        self.__db.close()

    def add_entry(self, (name, number, email)):
        self.__entries[name] = {}
        self.__entries[name]['Telephone'] = number
        self.__entries[name]['E-mail'] = email
        self.__update()

    def show_data(self):
        for k, v in self.__entries.iteritems():
            print 'Name:', k
            for subk, subv in v.iteritems():
                print '%s: %s' % (subk, subv)
            print
        

app = address_book()

userdata = ('Paul', '00316123456789', 'something@somewhere.com')

app.add_entry(userdata)

app.show_data()


Implement the rest,

Cheers and Happy coding

Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
 

Why is he adding portability adding json???

Json is more dedicated to data interchange, pickle it's here used to store data to the file, not to send serialized data over network or to databases.

As the address book increases in datasize, I believe cPickle will make you feel the difference.

Cheers and Happy coding

Edit:

One reason to use cPickle???

import cPickle as pickle
import json
import time

def time_it(func):
    def wrapper(*arg):
        t1 = time.time()
        res = func(*arg)
        t2 = time.time()
        print '%s took %0.3f ms' % (func.func_name, (t2 - t1) * 1000.0)
        return res
    return wrapper

list1 = [1, 2, 3, 'string1', 'string2', 'string3',
         [23, 35, 46], {'somewhere': 1, 'elsewhere': 4}]

print list1

@time_it
def test_pickle():
    for run in range(100000):
        pickle.dumps(list1)

@time_it
def test_json():
    for run in range(100000):
        json.dumps(list1)

test_pickle()
test_json()


It's really fast.

Cheers and Happy coding

Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
 

Maybe I used the wrong word "portability":$. Actually I wanted to say that if you want to create a project that is to be used by different coders following different languages, than you can always go for the json module. Just a piece of knowledge I had and I wanted to share. I didn't know about the speed problem. :-O
And if anyone wants to see the languages supported by json: json.org

techie1991
Junior Poster in Training
72 posts since Feb 2010
Reputation Points: 36
Solved Threads: 9
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: