Adress book

Reply

Join Date: Jun 2009
Posts: 28
Reputation: Your_mum is an unknown quantity at this point 
Solved Threads: 0
Your_mum Your_mum is offline Offline
Light Poster

Re: Adress book

 
0
  #11
Jun 29th, 2009
being fairly unfamiliar with such code, how would that be placed within my code to enable me to save and load the data.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 395
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

Re: Adress book

 
0
  #12
Jun 29th, 2009
I think the easiest way to explain it is to say its like a list where you would use:
  1. mylist = []
  2. mylist.append('hello')
  3. print mylist
  4. #outputs
  5. ['hello']
and in yours it would be something like:
  1. people = open('people_data.txt', 'a')
  2. class Data(object):
  3. def __init__(self, home, mobile, email):
  4. self.home = home
  5. self.mobile = mobile
  6. self.email = email
  7. self.get_info()
  8.  
  9. def get_info(self):
  10. sf = \
  11. """Home Phone: %s
  12. Mobile Phone: %s
  13. Email: %s
  14. """
  15. return sf % (self.home, self.mobile, self.email)
  16.  
  17.  
  18. name_dic = {}
  19.  
  20. # first set of data for testing
  21. name = 'frank millato'
  22. home = '123-456-7890'
  23. mobile = '456-789-0123'
  24. email = 'frank23@gmail.com'
  25. name_dic[name] = Data(home, mobile, email)
  26.  
  27. # second set of data for testing
  28. name = 'bob dork'
  29. home = '234-567-8901'
  30. mobile = '567-690-1234'
  31. email = 'bob.dork@hotmail.com'
  32. name_dic[name] = Data(home, mobile, email)
  33.  
  34. def change_att():
  35. #c_search = raw_input("Enter name to alter: ")
  36. c_search = 'bob dork' # for testing only!!!
  37. if name_dic.has_key(c_search):
  38. #c_name = raw_input("What element do you want to change: ")
  39. c_name = 'email' # for tesing only!!!
  40. if c_name == 'email':
  41. #n_e = raw_input("Enter new email adress: ")
  42. n_e = 'bob.dork@yahoo.com' # for testing only!!!
  43. # make the change
  44. k = name_dic[c_search]
  45. name_dic[c_search] = Data(k.home, k.mobile, n_e)
  46.  
  47. # get the info for all names added before change
  48. for key in name_dic:
  49. print( "Name = %s" % key )
  50. print( name_dic[key].get_info() )
  51.  
  52. print('-'*40)
  53.  
  54. change_att()
  55.  
  56. # get the info for all names added after change
  57. for key in name_dic:
  58. print( "Name = %s" % key )
  59. print( name_dic[key].get_info() )
  60. #and then after sowing it you can append it:
  61. # get the info for all names added after change
  62. for key in name_dic:
  63. if key not in people.read():
  64. people.write("Name = %s" % key)
  65. people.write(name_dic[key].get_info())
please bare in mind that what ive added i havent tested so take it with a pinch of salt
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 395
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

Re: Adress book

 
0
  #13
Jun 29th, 2009
i just tested it and it had a couple of my errors so here is a fix:
  1. people = open('people_data.txt', 'a')
  2. people2 = open('people_data.txt', 'r')
  3. class Data(object):
  4. def __init__(self, home, mobile, email):
  5. self.home = home
  6. self.mobile = mobile
  7. self.email = email
  8. self.get_info()
  9.  
  10. def get_info(self):
  11. sf = \
  12. """Home Phone: %s
  13. Mobile Phone: %s
  14. Email: %s
  15. """
  16. return sf % (self.home, self.mobile, self.email)
  17.  
  18.  
  19. name_dic = {}
  20.  
  21. # first set of data for testing
  22. name = 'frank millato'
  23. home = '123-456-7890'
  24. mobile = '456-789-0123'
  25. email = 'frank23@gmail.com'
  26. name_dic[name] = Data(home, mobile, email)
  27.  
  28. # second set of data for testing
  29. name = 'bob dork'
  30. home = '234-567-8901'
  31. mobile = '567-690-1234'
  32. email = 'bob.dork@hotmail.com'
  33. name_dic[name] = Data(home, mobile, email)
  34.  
  35. def change_att():
  36. #c_search = raw_input("Enter name to alter: ")
  37. c_search = 'bob dork' # for testing only!!!
  38. if name_dic.has_key(c_search):
  39. #c_name = raw_input("What element do you want to change: ")
  40. c_name = 'email' # for tesing only!!!
  41. if c_name == 'email':
  42. #n_e = raw_input("Enter new email adress: ")
  43. n_e = 'bob.dork@yahoo.com' # for testing only!!!
  44. # make the change
  45. k = name_dic[c_search]
  46. name_dic[c_search] = Data(k.home, k.mobile, n_e)
  47.  
  48. # get the info for all names added before change
  49. for key in name_dic:
  50. print( "Name = %s" % key )
  51. print( name_dic[key].get_info() )
  52.  
  53. print('-'*40)
  54.  
  55. change_att()
  56.  
  57. # get the info for all names added after change
  58. for key in name_dic:
  59. print( "Name = %s" % key )
  60. print( name_dic[key].get_info() )
  61. #and then after sowing it you can append it:
  62. # get the info for all names added after change
  63. for key in name_dic:
  64. if key not in people2:
  65. people.write("Name = %s" % key)
  66. people.write(name_dic[key].get_info())
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: Your_mum is an unknown quantity at this point 
Solved Threads: 0
Your_mum Your_mum is offline Offline
Light Poster

Re: Adress book

 
0
  #14
Jun 29th, 2009
So how could I put this into my code - saving the contents of the dictionary, and reopeninging them later.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 395
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

Re: Adress book

 
0
  #15
Jun 29th, 2009
I did that in there but basically all it is that does it are 2 snippets:
  1. people = open('people_data.txt', 'a')
  2. people2 = open('people_data.txt', 'r')
  3. #which opens 2 versions of the file, 1 to append and 1 to read to make sure the information is all unique
  4. #and
  5. for key in name_dic:
  6. if key not in people2:
  7. people.write("Name = %s" % key)
  8. people.write(name_dic[key].get_info())
  9. #that checks for previous entries that are the same and then writes entries that aren't
  10. #I cant really explain it, I'm going to have to rely on someone else to make it simple as the list example is my best attempt
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: Your_mum is an unknown quantity at this point 
Solved Threads: 0
Your_mum Your_mum is offline Offline
Light Poster

Re: Adress book

 
0
  #16
Jun 30th, 2009
OKay then, how would I place that in my code to solve the prblem?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,003
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 927
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Adress book

 
0
  #17
Jun 30th, 2009
I took the liberty to use sneekula's test setup ...
  1. # a test setup for pickle dump and load
  2. # note that pickle does not save the class itself
  3. # if the dumped data file is used in another program,
  4. # you need to code a matching class in that program
  5.  
  6. import pickle
  7.  
  8. class Data(object):
  9. def __init__(self, home, mobile, email):
  10. self.home = home
  11. self.mobile = mobile
  12. self.email = email
  13. self.get_info()
  14.  
  15. def get_info(self):
  16. sf = \
  17. """Home Phone: %s
  18. Mobile Phone: %s
  19. Email: %s
  20. """
  21. return sf % (self.home, self.mobile, self.email)
  22.  
  23.  
  24. name_dic = {}
  25.  
  26. # first set of data for testing
  27. name = 'frank millato'
  28. home = '123-456-7890'
  29. mobile = '456-789-0123'
  30. email = 'frank23@gmail.com'
  31. name_dic[name] = Data(home, mobile, email)
  32.  
  33. # second set of data for testing
  34. name = 'bob dork'
  35. home = '234-567-8901'
  36. mobile = '567-690-1234'
  37. email = 'bob.dork@hotmail.com'
  38. name_dic[name] = Data(home, mobile, email)
  39.  
  40. # get the info for all names before dump
  41. print( "Original data:" )
  42. for key in name_dic:
  43. print( "Name = %s" % key )
  44. print( name_dic[key].get_info() )
  45.  
  46. print('-'*40)
  47.  
  48. fname = "PhoneDict.dat"
  49. # pickle dump the dictionary
  50. fout = open(fname, "w")
  51. pickle.dump(name_dic, fout)
  52. fout.close()
  53.  
  54. # delete the name_dic object
  55. del name_dic
  56.  
  57. # pickle load the dictionary
  58. fin = open(fname, "r")
  59. name_dic = pickle.load(fin)
  60. fin.close()
  61.  
  62. # get the info for all names after load
  63. print( "Data dumped and reloaded with module pickle:" )
  64. for key in name_dic:
  65. print( "Name = %s" % key )
  66. print( name_dic[key].get_info() )
  67.  
  68. """
  69. my output -->
  70. Original data:
  71. Name = frank millato
  72. Home Phone: 123-456-7890
  73. Mobile Phone: 456-789-0123
  74. Email: frank23@gmail.com
  75.  
  76. Name = bob dork
  77. Home Phone: 234-567-8901
  78. Mobile Phone: 567-690-1234
  79. Email: bob.dork@hotmail.com
  80.  
  81. ----------------------------------------
  82. Data dumped and reloaded with module pickle:
  83. Name = frank millato
  84. Home Phone: 123-456-7890
  85. Mobile Phone: 456-789-0123
  86. Email: frank23@gmail.com
  87.  
  88. Name = bob dork
  89. Home Phone: 234-567-8901
  90. Mobile Phone: 567-690-1234
  91. Email: bob.dork@hotmail.com
  92. """
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: Your_mum is an unknown quantity at this point 
Solved Threads: 0
Your_mum Your_mum is offline Offline
Light Poster

Re: Adress book

 
0
  #18
Jul 1st, 2009
Thanks guys, I'm all sorted now.
My comments on this page all sound the same right, well this sounds stupid, but i had no idea that the post had gon onto a second page therefore, I just assumed my post hadn't been posted, so i posted it again, sorry.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 395
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

Re: Adress book

 
0
  #19
Jul 2nd, 2009
@vegaseat: just wondering, why did you use pickle? I found that using the append function of open works easier and faster
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,003
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 927
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Adress book

 
0
  #20
Jul 3rd, 2009
Originally Posted by leegeorg07 View Post
@vegaseat: just wondering, why did you use pickle? I found that using the append function of open works easier and faster
If you save the data as a text file like in your case, then you have to rebuild the entire dictionary each time when you load the saved data. As the size of the data grows, this gets to be time consuming. Also, if you edit the data and simply append to a text file, then the old data would still be there.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC