DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Adress book (http://www.daniweb.com/forums/thread200291.html)

Your_mum Jun 28th, 2009 10:50 am
Adress book
 
Hi attempting to make an address book program but hit a snag-
I have:
A class to give various attributes(phone numbers etc)to an instance
which then puts it all into a doc string which can be added to a dictionary-
but I want a function that will ask the user for the relevant information and then feed it to the class as the relevant arguments. however I Can't figure that out. Any Ideas-Section of code below:
class Data:
    def __init__(self, home, mobile, email):
        self.home = home
        self.mobile = mobile
        self.email = email
    def Coll_Data(self):
        self.info = """
Home Phone: %s
Mobile Phone: %s
Email: %s
""" % (self.home, self.mobile, self.email)

#the bit that doesn't work that needs changing is below:

def add_new():
    name = raw_input("Enter name to be added: ")
    h = raw_input("Enter home phone: ")
    m = raw_input("Enter Mobile phone: ")
    e = raw_input("Enter Email address: ")
    name = Data(h, m, e)
Please help!!!!!!

sneekula Jun 28th, 2009 11:24 am
Re: Adress book
 
Oh I see, you want the instance name be the name of the person. That will not be a good idea, because identifiers in Python don't allow spaces.

A dictionary, where the name is the key and the value is a ( home, mobile, email ) tuple or list will do much better. You could also make the class instance the value of your dictionary.

Your_mum Jun 28th, 2009 11:47 am
Re: Adress book
 
Thanks, but making the class instance the value of the dictionary does not work it just returns the location of the instance in hexadecimal notation.

I have rejigged the code:
class Data:
    def __init__(self):
        self.home = raw_input("Enter home: ")
        self.mobile = raw_input("Enter mobile: ")
        self.email = raw_input("Enter email :")
    def Coll_data(self):
        self.info = """
Home : %s
Mobile : %s
Email : %s
""" % (self.home, self.mobile, self.email)
I intend to add it to the dictionary using self.info as the value.
But I really need a function that will create a variable and asign it to class Data.

sneekula Jun 28th, 2009 12:40 pm
Re: Adress book
 
This works just fine:
# create a dictionary of class instance values

class Data(object):
    def __init__(self, home, mobile, email):
        self.home = home
        self.mobile = mobile
        self.email = email
        self.get_info()

    def get_info(self):
        sf = \
"""Home Phone: %s
Mobile Phone: %s
Email: %s
"""
        return sf % (self.home, self.mobile, self.email)


name_dic = {}

# first set of data
name = 'frank'
home = '123-456-7890'
mobile = '456-789-0123'
email = 'frank23@gmail.com'

name_dic[name] = Data(home, mobile, email)

# second set of data
name = 'bob'
home = '234-567-8901'
mobile = '567-690-1234'
email = 'bob.dork@hotmail.com'

name_dic[name] = Data(home, mobile, email)

# get bob's email
name = 'bob'
print( name_dic[name].email )

print('-'*30)

# get the info for all names added so far
for key in name_dic:
    print( "Name = %s" % key )
    print( name_dic[key].get_info() )

"""
my output -->
bob.dork@hotmail.com
------------------------------
Name = frank
Home Phone: 123-456-7890
Mobile Phone: 456-789-0123
Email: frank23@gmail.com

Name = bob
Home Phone: 234-567-8901
Mobile Phone: 567-690-1234
Email: bob.dork@hotmail.com
"""
Remember that the name string is the dictionary key and has to be unique.

Your_mum Jun 28th, 2009 1:09 pm
Re: Adress book
 
Thanks
that was extremely helpful

Your_mum Jun 29th, 2009 1:45 pm
Re: Adress book
 
Using the code you gave me, I modified it a bit to enable more easy user interaction, but I want a function to change an attribute of an entry, say the email.
heres the code so far
intro = "Welcome to Adress_Book.py"
name_dic = {}
loop = 1
menu_choice = 0

class Data(object):
    def __init__(self, home, mobile, email):
        self.home = home
        self.mobile = mobile
        self.email = email
        self.get_info()

    def get_info(self):
        sf = \
"""Home Phone: %s
Mobile Phone: %s
Email: %s
"""
        return sf % (self.home, self.mobile, self.email)

def add_new():
    name = raw_input("Enter name: ")
    home = raw_input("Enter home: ")
    mobile = raw_input("Enter mobile: ")
    email = raw_input("Enter email: ")

    name_dic[name] = Data(home, mobile, email)

def printout():
    for key in name_dic:
        print( "Name = %s" % key )
        print( name_dic[key].get_info() )

def look_up():
    search = raw_input("Enter name to be searched: ")
    if name_dic.has_key(search):
        print( "Name = %s" % search )
        print( name_dic[search].get_info() )

    else:
        print( "Name %s not found" % search )

def del_entry():
    d_search = raw_input("Enter name to be deleted: ")
    if name_dic.has_key(d_search):
        del name_dic[d_search]
        print( "Name %s deleted" % d_search)

    else:
        print( "Name %s not found" % d_search )

def change_att():
    c_search = raw_input("Enter name to alter: ")
    if name_dic.has_key(c_search):
        c_name = raw_input("What element do you want to change: ")
        if c_name == 'email':
            n_e = raw_input("Enter new email adress: ")
            #n_e being the new email adress
so how can I take n_e and swap it for the old email adress

Your_mum Jun 29th, 2009 2:12 pm
Re: Adress book
 
sorry my mistake :)
intro = "Welcome to Adress_Book.py"
name_dic = {}
loop = 1
menu_choice = 0

class Data(object):
    def __init__(self, home, mobile, email):
        self.home = home
        self.mobile = mobile
        self.email = email
        self.get_info()

    def get_info(self):
        sf = \
"""Home Phone: %s
Mobile Phone: %s
Email: %s
"""
        return sf % (self.home, self.mobile, self.email)

def add_new():
    name = raw_input("Enter name: ")
    home = raw_input("Enter home: ")
    mobile = raw_input("Enter mobile: ")
    email = raw_input("Enter email: ")

    name_dic[name] = Data(home, mobile, email)

def printout():
    for key in name_dic:
        print( "Name = %s" % key )
        print( name_dic[key].get_info() )

def look_up():
    search = raw_input("Enter name to be searched: ")
    if name_dic.has_key(search):
        print( "Name = %s" % search )
        print( name_dic[search].get_info() )

    else:
        print( "Name %s not found" % search )

def del_entry():
    d_search = raw_input("Enter name to be deleted: ")
    if name_dic.has_key(d_search):
        del name_dic[d_search]
        print( "Name %s deleted" % d_search)

    else:
        print( "Name %s not found" % d_search )

def change_att():
    c_search = raw_input("Enter name to alter: ")
    if name_dic.has_key(c_search):
        c_name = raw_input("What element do you want to change: ")
        if c_name == 'email':
            n_e = raw_input("Enter new email adress: ")
            name_dic[c_search].email = n_e
        elif c_name == 'home':
            n_h = raw_input("Enter new home number: ")
            name_dic[c_search].home = n_h
        elif c_name == 'mobile':
            n_m = raw_input("Enter new mobile number: ")
            name_dic[c_search].mobile = n_m
    else:
        print( "Name %s not found" % c_search )

print intro

while loop != 0:
    print """
1) Add new entry
2) Remove an entry
3) Look up an entry
4) Print out adress book
5) Edit an entry
6) Exit
    """

    menu_choice = input("Enter choice: ")

    if menu_choice == 1:
        add_new()
    elif menu_choice == 2:
        del_entry()
    elif menu_choice == 3:
        look_up()
    elif menu_choice == 4:
        printout()
    elif menu_choice == 5:
        change_att()
    elif menu_choice == 6:
        loop = 0
    else:
        print "Please anter a valid option"
I want to write file I/O for this, having tried before and failed, so could someone pls point me in the right direction.

sneekula Jun 29th, 2009 2:46 pm
Re: Adress book
 
Let's say you want to change the email:
# a test setup

class Data(object):
    def __init__(self, home, mobile, email):
        self.home = home
        self.mobile = mobile
        self.email = email
        self.get_info()

    def get_info(self):
        sf = \
"""Home Phone: %s
Mobile Phone: %s
Email: %s
"""
        return sf % (self.home, self.mobile, self.email)


name_dic = {}

# first set of data for testing
name = 'frank millato'
home = '123-456-7890'
mobile = '456-789-0123'
email = 'frank23@gmail.com'
name_dic[name] = Data(home, mobile, email)

# second set of data for testing
name = 'bob dork'
home = '234-567-8901'
mobile = '567-690-1234'
email = 'bob.dork@hotmail.com'
name_dic[name] = Data(home, mobile, email)

def change_att():
    #c_search = raw_input("Enter name to alter: ")
    c_search = 'bob dork'  # for testing only!!!
    if name_dic.has_key(c_search):
        #c_name = raw_input("What element do you want to change: ")
        c_name = 'email'  # for tesing only!!!
        if c_name == 'email':
            #n_e = raw_input("Enter new email adress: ")
            n_e = 'bob.dork@yahoo.com'  # for testing only!!!
            # make the change
            k = name_dic[c_search]
            name_dic[c_search] = Data(k.home, k.mobile, n_e)

# get the info for all names added before change
for key in name_dic:
    print( "Name = %s" % key )
    print( name_dic[key].get_info() )

print('-'*40)

change_att()

# get the info for all names added after change
for key in name_dic:
    print( "Name = %s" % key )
    print( name_dic[key].get_info() )

"""
my output -->
Name = frank millato
Home Phone: 123-456-7890
Mobile Phone: 456-789-0123
Email: frank23@gmail.com

Name = bob dork
Home Phone: 234-567-8901
Mobile Phone: 567-690-1234
Email: bob.dork@hotmail.com

----------------------------------------
Name = frank millato
Home Phone: 123-456-7890
Mobile Phone: 456-789-0123
Email: frank23@gmail.com

Name = bob dork
Home Phone: 234-567-8901
Mobile Phone: 567-690-1234
Email: bob.dork@yahoo.com
"""

Your_mum Jun 29th, 2009 2:54 pm
Re: Adress book
 
cheers, but do you have any ideas on pickleing this to create file io
especially regarding the load

leegeorg07 Jun 29th, 2009 3:44 pm
Re: Adress book
 
I have to say that from python 2.5 and on you can use the 'a' file open method (append), which just adds to the data already in the file, i changed all of my programs to use this instead and it works so much faster


All times are GMT -4. The time now is 3:42 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC