document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/env python
  2. #file name : cb.py
  3. #Copyright (c) Arun.K.R
  4. #Creative Commons Attribution-Share Alike 3.0 Unported\' <http: 3.0="" by-sa="" creativecommons.org="" licenses="">
  5. #short for \'c\'ontact \'b\'ook
  6. #this module as it\'s doesn\'t do anything; because I succeded in splitting source file into modules
  7. #I doubt wether the first line is needed. I think don\'t since this is not a stand alone module.
  8.  
  9. import os.path
  10. import cPickle as pkl
  11.  
  12. # class to represent a contact
  13. class ContactBook:
  14. \'\'\'Each object of this class represent a contact book\'\'\'
  15.  
  16. # A dictionary is used to keep contacts, and name is used as key
  17. # although this is not a good practice (because it can\'t be Primary Key)
  18. # we use it for simplicity. And is class variable.
  19. contacts = {}
  20. #this will look like
  21. #{ NAME1:[ [EMAILS],[PHONE NUMBERS] ]
  22. #  NAME2:[ [EMAILS],[PHONE NUMBERS] ]
  23. #  ..................................
  24. #  NAMEn:[ [EMAILS],[PHONE NUMBERS] ] }
  25.  
  26. def __init__(self):
  27. \'\'\' loading of contacts already persisted occures here\'\'\'
  28. self.dbName = \'contacts.cm\' #our persistance store
  29. #ToDo what if user installs this program and runs this program from different directories
  30. #contact will be created in those directories this effects program
  31. #so depending upon platform create a file in users home directory
  32. #in linux os.environ.get(\'HOME\') will give just that. find windows\'s case also mac
  33. ContactBook.contacts.clear() #we starts program with an empty contact book
  34. if os.path.exists(self.dbName): #file already exists, ie., not first run
  35. try:
  36. self.f = file(self.dbName, \'rb\') #open file for reading in binary mode
  37. ContactBook.contacts = pkl.load(self.f) #reads persisted contact book
  38. finally:
  39. self.f.close() #closes opened file handle
  40. else: #this is first run of program
  41. print \'welcome to cm (contact manager) program\'
  42. print \' \'*5, \'written by Arun.K.R (using python)\'
  43. print \' \'*5, "with the aid of \'Byte of Python\' by Swaroop.C.H\'"
  44. print \'Thank You for using it\'
  45. #initialization compleates here
  46.  
  47. def __del__(self):
  48. \'\'\' before closing all existing contacts will be persisted\'\'\'
  49. try:
  50. self.f = file(self.dbName, \'wb\') #open file to write in binary mode
  51. pkl.dump(ContactBook.contacts, self.f) #our contact book persisted.
  52. finally:
  53. self.f.close() #closes file handle.
  54.  
  55. def updateContact(self, name, email, phone):
  56. \'\'\'To add a new contact or update an existing contact\'\'\'
  57. ContactBook.contacts[name] = [email, phone]
  58. #or ContactBook.contacts.update({name:[email, phone]})
  59. # I don\'t know any difference b/n the two
  60.  
  61. def rmContact(self, name):
  62. \'\'\'removes entry corresponding to given name\'\'\'
  63. del ContactBook.contacts[name]
  64. #or ContactBook.contacts.pop(name) is also fine
  65.  
  66. def showNames(self):
  67. \'\'\'used to list all contact\'s name\'\'\'
  68. for n in ContactBook.contacts.keys():
  69. print \'%s\' % n
  70.  
  71. def showContact(self, name):
  72. \'\'\'used to show a specific contact\'\'\'
  73. #note the \'self.hasContact()\' if we call it as simply \'hasContact()\' it\'ll fail.
  74. #ie., even a member function need to be called in this way.
  75. #A difference b/n C++, Java etc with Python
  76. if self.hasContact(name) == True:
  77. print \'name  : %s\' % name
  78. tmp = ContactBook.contacts[name] #give phone and email of given name as a list of lists
  79. print \'emails:\'
  80. for i in range(0,len(tmp[0])): #fist item is email
  81. print \' \'*5, \'%s\' % tmp[0][i]
  82. print \'phone :\'
  83. for i in range(0,len(tmp[1])): #2nd item is phone
  84. print \' \'*5, \'%s\' % tmp[1][i]
  85.  
  86. def hasContact(self, name):
  87. \'\'\'look wether there is a contact for specified name\'\'\'
  88. #return True of there is given name, False otherwise
  89. return ContactBook.contacts.has_key(name)
  90.  
  91. def getEmail(self, name):
  92. if self.hasContact(name) == True:
  93. #ContactBook.contacts[name] returns a list, of wich first item is email.
  94. return ContactBook.contacts[name][0]
  95.  
  96. def getPhone(self, name):
  97. if self.hasContact(name) == True:
  98. #ContactBook.contacts[name] returns a list, of wich 2nd item is phone.
  99. return ContactBook.contacts[name][1]
  100. #class definition ends here
');