Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # phone_book_for_pasting.py
- # Win 7, Python 3.2.1
- """
- The data file is "C:/P32Working/Data/phone_book.txt"
- phone_book.py both adds data (items) to the data file, and accesses this data.
- Here are some of the lines in the data file, with the info changed to protect the innocent:
- as=Amy Snodgrass h:212-782-3456 m:212-947-5387
- bn0=Barnes and Noble Book Store 212-421-8473
- cm0=Fred Thomas m:212-234-2272
- mm0=Michael McHenry h:718-633-3036 m:718-810-2807
- mm1=Martin Morse h:35# 1-201-637-1421 m:34# 1-201-720-1241
- ms0=Mark Sanders (Millie's son) 422-318-2346 msanders@stanfordalumni.org
- aaa0=AAA 575-3992 or (call for service 1-800-472-4630) Membership #422 260 0131863 00 8
- staples0=Staples 1-800-234-5677
- All keys end in a digit. However, the user ignores this digit when entering or accessing items.
- Because keys must be unique,
- if there is already an mm0 key, when another mm is added, it is automatically assigned the key mm1;
- if there is already an mm1 key, the next new mm is assigned the key mm2, and so on.
- In the data above there are 2 mm's, so when the user inputs mm, he gets both values:
- ==========================
- Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
- Enter: mm
- Michael McHenry h:718-633-3036 m:718-810-2807
- Martin Morse h:35# 1-201-637-1421 m:34# 1-201-720-1241
- =========================
- Here's a sample run that shows both accessing data and entering data:
- Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
- Enter:
- You didn't enter anything. Try again.
- Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
- Enter: as
- Amy Snodgrass h:212-782-3456 m:212-947-5387
- Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
- Enter: add
- Enter new item in form 'key=value' or enter 'q' to quit: jw=John Wang 1-305-234-5678
- new item = jw0=John Wang 1-305-234-5678
- Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
- Enter: jw
- John Wang 1-305-234-5678
- Enter initials of name, or 'all' to print phone book, or 'add' to add an item, or 'q' to quit.
- Enter: q
- Bye.
- ==============================================
- """
- def add_item_to_data(p):
- keys_list = []
- file = open("C:/P32Working/Data/phone_book.txt")
- lines = file.readlines()
- for line in lines:
- key = line.partition('=')[0]
- keys_list.append(key)
- file.close()
- while True:
- file = open("C:/P32Working/Data/phone_book.txt", "a+")
- new_item = input("Enter new item in form 'key=value' or enter 'q' to quit: ")
- if new_item in 'Qq':
- file.close()
- break
- new_key = (new_item.partition('=')[0] + '0').lower()
- new_value = new_item.partition('=')[2]
- while new_key in keys_list:
- digit = int(new_key[-1])
- digit += 1
- digit = str(digit)
- new_key = new_key.replace(new_key[-1], digit)
- print(new_key)
- keys_list += [new_key]
- new_item = new_key + '=' + new_value
- if new_item[-1] == '=':
- print("The new item has no value. Try again.")
- continue
- print("new item =", new_item)
- file.write(new_item + '\n')
- file.close()
- #add the new item to p so that if searched for while
- #this script is still open, the item will be found.
- p[new_key] = new_value
- return p
- def create_dict_p():
- p = {}
- file = open("C:/P32Working/Data/phone_book.txt")
- lines = file.readlines()
- for line in lines:
- key = line.partition('=')[0]
- value = line.partition('=')[2][:-1]
- p[key] = value
- file.close()
- return p
- def main_loop(p):
- while True:
- print()
- print("Enter initials of name, or 'all' to print phone book,")
- print(" or 'add' to add an item, or 'q' to quit.")
- try:
- initials = input("Enter: ").lower()
- print()
- if initials == 'q':
- print("Bye.")
- break
- elif initials == 'all':
- list_all = list(p.items())
- list_all.sort()
- for x in list_all:
- print(x)
- elif initials == 'add':
- add_item_to_data(p)
- else:
- print(p[initials])
- except KeyError:
- keys_list = list(p.keys())
- keys_list.sort()
- initials_found_list = []
- for x in '0123456789':
- if (initials + x) in keys_list:
- initials_found_list.append(initials + x)
- if initials_found_list:
- for x in initials_found_list:
- print(p[x])
- elif initials == '':
- print("You didn't enter anything. Try again.")
- else:
- print("The initials '", initials, "' are not in phone book.", sep='')
- print("This is the list of entry initials:")
- print(keys_list)
- def main():
- # create dict p
- p = create_dict_p()
- main_loop(p)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement