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!!

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

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.

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.

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

Edited 6 Years Ago by Beat_Slayer: n/a

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

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

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

Edited 6 Years Ago by Beat_Slayer: n/a

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

This question has already been answered. Start a new discussion instead.