Advertisement
pyzuki

phone_book.py

Sep 10th, 2011
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.35 KB | None | 0 0
  1. # phone_book_for_pasting.py
  2. # Win 7, Python 3.2.1
  3.  
  4. """
  5. The data file is "C:/P32Working/Data/phone_book.txt"
  6. phone_book.py both adds data (items) to the data file, and accesses this data.
  7.  
  8. Here are some of the lines in the data file, with the info changed to protect the innocent:
  9.  
  10. as=Amy Snodgrass h:212-782-3456 m:212-947-5387
  11. bn0=Barnes and Noble Book Store  212-421-8473
  12. cm0=Fred Thomas m:212-234-2272
  13. mm0=Michael McHenry h:718-633-3036 m:718-810-2807
  14. mm1=Martin Morse h:35# 1-201-637-1421 m:34# 1-201-720-1241
  15. ms0=Mark Sanders (Millie's son) 422-318-2346  msanders@stanfordalumni.org
  16. aaa0=AAA 575-3992 or (call for service 1-800-472-4630) Membership #422 260 0131863 00 8
  17. staples0=Staples 1-800-234-5677
  18.  
  19. All keys end in a digit. However, the user ignores this digit when entering or accessing items.
  20.  
  21. Because keys must be unique,
  22. if there is already an mm0 key, when another mm is added, it is automatically assigned the key mm1;
  23. if there is already an mm1 key, the next new mm is assigned the key mm2, and so on.
  24. In the data above there are 2 mm's, so when the user inputs mm, he gets both values:
  25. ==========================
  26. Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
  27. Enter: mm
  28.  
  29. Michael McHenry h:718-633-3036 m:718-810-2807
  30. Martin Morse h:35# 1-201-637-1421 m:34# 1-201-720-1241
  31. =========================
  32.  
  33. Here's a sample run that shows both accessing data and entering data:
  34.  
  35. Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
  36. Enter:
  37.  
  38. You didn't enter anything. Try again.
  39.  
  40. Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
  41. Enter: as
  42.  
  43. Amy Snodgrass h:212-782-3456 m:212-947-5387
  44.  
  45. Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
  46. Enter: add
  47.  
  48. Enter new item in form 'key=value' or enter 'q' to quit: jw=John Wang 1-305-234-5678
  49. new item = jw0=John Wang 1-305-234-5678
  50.  
  51. Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
  52. Enter: jw
  53.  
  54. John Wang 1-305-234-5678
  55.  
  56. Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
  57. Enter: q
  58.  
  59. Bye.
  60. ==============================================
  61. """
  62.  
  63. def add_item_to_data(p):
  64.     keys_list = []
  65.     file = open("C:/P32Working/Data/phone_book.txt")
  66.     lines = file.readlines()
  67.     for line in lines:
  68.         key = line.partition('=')[0]
  69.         keys_list.append(key)
  70.     file.close()
  71.    
  72.     while True:
  73.         file = open("C:/P32Working/Data/phone_book.txt", "a+")
  74.         new_item = input("Enter new item in form 'key=value' or enter 'q' to quit: ")
  75.         if new_item in 'Qq':
  76.             file.close()
  77.             break
  78.         new_key = (new_item.partition('=')[0] + '0').lower()
  79.         new_value = new_item.partition('=')[2]
  80.         while new_key in keys_list:
  81.             digit = int(new_key[-1])
  82.             digit += 1
  83.             digit = str(digit)
  84.             new_key = new_key.replace(new_key[-1], digit)
  85.             print(new_key)
  86.         keys_list += [new_key]
  87.         new_item = new_key + '=' + new_value
  88.         if new_item[-1] == '=':
  89.             print("The new item has no value. Try again.")
  90.             continue
  91.         print("new item =", new_item)
  92.        
  93.         file.write(new_item + '\n')
  94.         file.close()
  95.        
  96.         #add the new item to p so that if searched for while
  97.         #this script is still open, the item will be found.
  98.         p[new_key] = new_value
  99.         return p
  100.  
  101. def create_dict_p():
  102.     p = {}
  103.     file = open("C:/P32Working/Data/phone_book.txt")
  104.     lines = file.readlines()
  105.     for line in lines:
  106.         key = line.partition('=')[0]
  107.         value = line.partition('=')[2][:-1]
  108.         p[key] = value
  109.     file.close()
  110.     return p
  111.  
  112. def main_loop(p):
  113.     while True:
  114.         print()
  115.         print("Enter initials of name, or 'all' to print phone book,")
  116.         print("  or 'add' to add an item, or 'q' to quit.")
  117.         try:
  118.             initials = input("Enter: ").lower()
  119.             print()
  120.            
  121.             if initials == 'q':
  122.                 print("Bye.")
  123.                 break
  124.             elif initials == 'all':
  125.                 list_all = list(p.items())
  126.                 list_all.sort()
  127.                 for x in list_all:
  128.                     print(x)
  129.             elif initials == 'add':
  130.                 add_item_to_data(p)
  131.             else:
  132.                 print(p[initials])
  133.                
  134.         except KeyError:
  135.             keys_list = list(p.keys())
  136.             keys_list.sort()
  137.             initials_found_list = []
  138.             for x in '0123456789':
  139.                 if (initials + x) in keys_list:
  140.                     initials_found_list.append(initials + x)
  141.             if initials_found_list:
  142.                 for x in initials_found_list:
  143.                     print(p[x])
  144.             elif initials == '':
  145.                 print("You didn't enter anything. Try again.")
  146.             else:
  147.                 print("The initials '", initials, "' are not in phone book.", sep='')
  148.                 print("This is the list of entry initials:")
  149.                 print(keys_list)
  150.                
  151. def main():
  152.     # create dict p
  153.     p = create_dict_p()
  154.     main_loop(p)
  155.    
  156. if __name__ == '__main__':
  157.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement