From 721300c90064421fcdf6f90766763d0cb8a9c5ba Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Mon, 7 Aug 2023 09:13:51 -0700 Subject: [PATCH 1/6] ported contacts features to python --- python/src/contacts/__init__.py | 0 python/src/contacts/contacts.py | 19 +++++++++++++++++ python/src/main.py | 38 +++++++++++++++++++++++++++------ 3 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 python/src/contacts/__init__.py create mode 100644 python/src/contacts/contacts.py diff --git a/python/src/contacts/__init__.py b/python/src/contacts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python/src/contacts/contacts.py b/python/src/contacts/contacts.py new file mode 100644 index 0000000..ec7c7ae --- /dev/null +++ b/python/src/contacts/contacts.py @@ -0,0 +1,19 @@ +def show_contacts(address_book): + for contact in address_book: + print(f"{contact['name']} {contact['email']} : {contact['phone']}") + + +def add_contact(address_book): + new_contact = {} + for s in ["name", "phone", "email"]: + new_contact[s] = input(f"Enter {s}: ") + address_book.append(new_contact) + + +def delete_contact(address_book): + partial = input("Enter a part of their name: ") + return ( + contact + for contact in address_book + if partial.lower() not in contact["name"].lower() + ) diff --git a/python/src/main.py b/python/src/main.py index 7bd07c8..4b6e7b6 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,11 +1,35 @@ -import contacts +import contacts.contacts -addressbook = [] +address_book = [ + {"name": "Bruce Wayne", "phone": "555-123-4567", "email": "bruce@wayne.com"}, + {"name": "Clark Kent", "phone": "555-222-3333", "email": "clark@dailyplanet.com"}, + {"name": "Diana Prince", "phone": "555-444-5555", "email": "diana@amazon.com"}, +] +menus = [ + "[1] Display all contacts", + "[2] Add a new contact", + "[3] Delete a contact", + "[4] Exit", +] -def menu(): - pass -def main(): - pass +def main(address_book): + selection = "" + while selection != "4": + for menu in menus: + print(menu) + selection = input("Enter a selection: ") + match selection: + case "1": + contacts.contacts.show_contacts(address_book) + case "2": + contacts.contacts.add_contact(address_book) + case "3": + address_book = contacts.contacts.delete_contact(address_book) + case "4": + print("Goodbye!") + case _: + print("That selection is not valid, please try again!") -main() + +main(address_book) From 1a6dab080282d140a4617bf17529f735a85a1db7 Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Mon, 7 Aug 2023 09:26:44 -0700 Subject: [PATCH 2/6] moved file --- python/src/contacts.py | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 python/src/contacts.py diff --git a/python/src/contacts.py b/python/src/contacts.py deleted file mode 100644 index 4dce478..0000000 --- a/python/src/contacts.py +++ /dev/null @@ -1,8 +0,0 @@ -def show_contacts(addressbook): - pass - -def add_contact(addressbook): - pass - -def delete_contact(addressbook): - pass From 36313eb0059ffdc8a14e7e9fff9652366a552bcf Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Mon, 7 Aug 2023 09:51:30 -0700 Subject: [PATCH 3/6] added whitespace for usability --- python/src/contacts/contacts.py | 3 +++ python/src/main.py | 1 + 2 files changed, 4 insertions(+) diff --git a/python/src/contacts/contacts.py b/python/src/contacts/contacts.py index ec7c7ae..5fb055b 100644 --- a/python/src/contacts/contacts.py +++ b/python/src/contacts/contacts.py @@ -1,16 +1,19 @@ def show_contacts(address_book): + print("\nContacts:\n") for contact in address_book: print(f"{contact['name']} {contact['email']} : {contact['phone']}") def add_contact(address_book): new_contact = {} + print("\nAdding a contact\n") for s in ["name", "phone", "email"]: new_contact[s] = input(f"Enter {s}: ") address_book.append(new_contact) def delete_contact(address_book): + print("\nDeleting one or more contacts\n") partial = input("Enter a part of their name: ") return ( contact diff --git a/python/src/main.py b/python/src/main.py index 4b6e7b6..3208a85 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -30,6 +30,7 @@ def main(address_book): print("Goodbye!") case _: print("That selection is not valid, please try again!") + print("") main(address_book) From e92f93b7a8efdab172d00b04517ae131b15f5bcd Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Mon, 7 Aug 2023 10:47:09 -0700 Subject: [PATCH 4/6] small change to output --- python/src/contacts/contacts.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/src/contacts/contacts.py b/python/src/contacts/contacts.py index 5fb055b..e97e161 100644 --- a/python/src/contacts/contacts.py +++ b/python/src/contacts/contacts.py @@ -14,7 +14,8 @@ def add_contact(address_book): def delete_contact(address_book): print("\nDeleting one or more contacts\n") - partial = input("Enter a part of their name: ") + partial = input("Enter a part of their name: ").lower() + print(f"Deleting contacts with name matching {partial}") return ( contact for contact in address_book From 8bfa294333a3240aca69bd6ad388ce6a479e8d11 Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Mon, 7 Aug 2023 11:13:23 -0700 Subject: [PATCH 5/6] simpler delete --- python/src/contacts/contacts.py | 12 ++++++------ python/src/main.py | 2 ++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/python/src/contacts/contacts.py b/python/src/contacts/contacts.py index e97e161..ebea7fe 100644 --- a/python/src/contacts/contacts.py +++ b/python/src/contacts/contacts.py @@ -11,13 +11,13 @@ def add_contact(address_book): new_contact[s] = input(f"Enter {s}: ") address_book.append(new_contact) - def delete_contact(address_book): print("\nDeleting one or more contacts\n") partial = input("Enter a part of their name: ").lower() print(f"Deleting contacts with name matching {partial}") - return ( - contact - for contact in address_book - if partial.lower() not in contact["name"].lower() - ) + ndx = 0 + for contact in address_book: + if partial in contact["name"].lower(): + print(f"Deleting {contact['name']}") + address_book.pop(ndx) + ndx = ndx + 1 diff --git a/python/src/main.py b/python/src/main.py index 3208a85..59cff52 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -28,6 +28,8 @@ def main(address_book): address_book = contacts.contacts.delete_contact(address_book) case "4": print("Goodbye!") + case "5": + contacts.contacts.delete_proper(address_book) case _: print("That selection is not valid, please try again!") print("") From 4236cab0bbb0c77dc3c6e4bcc1205c3f64c04802 Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Mon, 7 Aug 2023 11:21:04 -0700 Subject: [PATCH 6/6] now that we are using pop don't set value of address_book --- python/src/contacts/contacts.py | 5 ++--- python/src/main.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/python/src/contacts/contacts.py b/python/src/contacts/contacts.py index ebea7fe..3868f66 100644 --- a/python/src/contacts/contacts.py +++ b/python/src/contacts/contacts.py @@ -11,13 +11,12 @@ def add_contact(address_book): new_contact[s] = input(f"Enter {s}: ") address_book.append(new_contact) + def delete_contact(address_book): print("\nDeleting one or more contacts\n") partial = input("Enter a part of their name: ").lower() print(f"Deleting contacts with name matching {partial}") - ndx = 0 - for contact in address_book: + for ndx, contact in enumerate(address_book): if partial in contact["name"].lower(): print(f"Deleting {contact['name']}") address_book.pop(ndx) - ndx = ndx + 1 diff --git a/python/src/main.py b/python/src/main.py index 59cff52..465f485 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -25,7 +25,7 @@ def main(address_book): case "2": contacts.contacts.add_contact(address_book) case "3": - address_book = contacts.contacts.delete_contact(address_book) + contacts.contacts.delete_contact(address_book) case "4": print("Goodbye!") case "5":