Guest User

Untitled

a guest
Jan 22nd, 2018
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. """
  2. Simple phone book app
  3. (c) Nick Zarczynski
  4. license: BSD
  5. """
  6. import cPickle
  7. from Tkinter import *
  8.  
  9.  
  10. FILENAME = 'book.pk'
  11.  
  12. #############################################################################
  13. ## Storing and Loading
  14. #############################################################################
  15.  
  16. def store(book, filename=FILENAME):
  17. with open(filename, 'w') as f:
  18. cPickle.dump(book, f)
  19.  
  20. def load(filename=FILENAME):
  21. with open(filename, 'r') as f:
  22. return cPickle.load(f)
  23.  
  24. #############################################################################
  25. ## Search Functions
  26. #############################################################################
  27.  
  28. def general_find(criteria, term):
  29. for entry in book:
  30. if entry[criteria] == term:
  31. return entry
  32.  
  33. ## This is just a convenience function and is not strictly necessary
  34. def find_by_name(name):
  35. return general_find('name', name)
  36.  
  37. ## This uses nested functions, you could make to_initials a top-level function
  38. ## if you think you'll use it again
  39. def find_by_initials(initials):
  40. def to_initials(name):
  41. return ''.join([i[0] for i in name.split(' ')])
  42. for entry in book:
  43. if to_initials(entry['name']) == initials:
  44. return entry
  45.  
  46.  
  47. #############################################################################
  48. ## Add and Remove
  49. #############################################################################
  50.  
  51. def add_entry(entry):
  52. """Add an entry only if it is not an exact duplicate of another entry"""
  53. if entry not in book:
  54. book.append(entry)
  55.  
  56. def remove_entry(entry):
  57. """Remove the given entry from book. Use search functions to find the entry
  58.  
  59. Example:
  60. remove_entry(find_by_name('Mark Sanders'))
  61. """
  62. book.remove(entry)
  63.  
  64.  
  65. #############################################################################
  66. ## String to Entry
  67. #############################################################################
  68.  
  69. def string_to_entry(s):
  70. """Accepts a string of the format: "name = Mark Sanders | phone = 414-24..."
  71. and returns a dict {'name':'Mark Sanders', 'phone':'414-24...'}"""
  72. tempList = s.split('|')
  73. keyvalstrings = [tuple(i.split('=')) for i in tempList]
  74. new_entry = {}
  75. for key, val in keyvalstrings:
  76. new_entry[key.strip()] = val.strip()
  77. return new_entry
  78.  
  79.  
  80. #############################################################################
  81. ## User Input
  82. #############################################################################
  83.  
  84. def main_loop():
  85. while True:
  86. choice = raw_input("""
  87. What's next:
  88. (q) Quit
  89. (a) Add new Entry
  90. (v) View all Entries
  91. (s) General Search
  92. (si) Search by Initials
  93. (sn) Search by Name
  94. (g) Search and Edit by Name (GUI Display)
  95.  
  96. > """)
  97.  
  98. if choice == 'q':
  99. break
  100. elif choice == 'a':
  101. user_add_new()
  102. elif choice == 'v':
  103. print_book()
  104. elif choice == 's':
  105. user_general_find()
  106. elif choice == 'si':
  107. user_find_by_initials()
  108. elif choice == 'sn':
  109. user_find_by_name()
  110. elif choice == 'g':
  111. show_by_name()
  112. else:
  113. print "You entered something incorrectly, try again."
  114.  
  115. def user_add_new():
  116. s = raw_input("""
  117. Enter a new entry
  118. Format: name=Bob Smith | phone= 452-2355 | email = bob@g.com
  119.  
  120. > """)
  121. try:
  122. add_entry(string_to_entry(s))
  123. print "Successful"
  124. except:
  125. print "Oh my! Something went horribly wrong!"
  126.  
  127. def print_entry(entry):
  128. for key, val in entry.items():
  129. print key, '=', val
  130.  
  131. def print_book():
  132. for entry in book:
  133. print_entry(entry)
  134.  
  135. def user_general_find():
  136. k = raw_input("Enter the key (eg name, state, etc): ")
  137. v = raw_input("Enter the value (eg Fred, CA, etc): ")
  138. e = general_find(k, v)
  139. if e:
  140. for key, val in e.items():
  141. print key, '=', val
  142. else:
  143. print "Dude, I couldn't find it at all!"
  144.  
  145. def user_find_by_initials():
  146. i = raw_input("Enter initials: ")
  147. e = find_by_initials(i)
  148. if e:
  149. print_entry(e)
  150. else:
  151. print "I can't believe it, it's not there. I just can't believe it."
  152.  
  153. def user_find_by_name():
  154. n = raw_input("Enter name: ")
  155. e = find_by_name(n)
  156. if e:
  157. print_entry(e)
  158. else:
  159. print "You don't really know this person do you?"
  160.  
  161. def show_by_name():
  162. n = raw_input("Enter name: ")
  163. e = find_by_name(n)
  164. if e:
  165. show_entry(e)
  166. else:
  167. print "Cannot show GUI"
  168.  
  169.  
  170. #############################################################################
  171. ## GUI
  172. #############################################################################
  173.  
  174. def show_entry(entry):
  175. def make_label_entry(key, value):
  176. l = Label(parent, text=key)
  177. e = Entry(parent)
  178. e.insert(0, value)
  179. return (l, e)
  180.  
  181. def grid_widgets(low):
  182. for i, w in enumerate(low):
  183. w[0].grid(row=i, column=0)
  184. w[1].grid(row=i, column=1)
  185.  
  186. def save():
  187. for l, e in widgets:
  188. entry[l.config()['text'][-1]] = e.get()
  189.  
  190. parent =Tk()
  191. parent.title('Entry Display')
  192.  
  193. widgets = [make_label_entry(key, val) for key, val in entry.items()]
  194. grid_widgets(widgets)
  195.  
  196. save_button = Button(parent, text='save', command=save)
  197. save_button.grid(row=len(widgets)+1, column=1)
  198.  
  199. parent.mainloop()
  200.  
  201.  
  202.  
  203. #############################################################################
  204. ## Startup
  205. #############################################################################
  206.  
  207. if __name__ == '__main__':
  208. ## First load the book from the file or make a new one if the file doesn't exist
  209. try:
  210. book = load()
  211. except IOError:
  212. book = []
  213.  
  214. ## Enter the main loop
  215. main_loop()
  216.  
  217. ## After the user quits we can store the changes
  218. store(book)
Add Comment
Please, Sign In to add comment