|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# P1 - Address Book\n", |
| 8 | + "\n", |
| 9 | + "## Author: Swaroop Kallakuri\n", |
| 10 | + "\n", |
| 11 | + "## In this small mini project we will be creating a simple address book application that will store, search and delete records." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "markdown", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "# To-DO:\n", |
| 19 | + "1. test the application and create a list of errors\n", |
| 20 | + "2. design the smoothflow to have a nicest experience\n", |
| 21 | + "3. Smooth exit and bye message" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 1, |
| 27 | + "metadata": { |
| 28 | + "collapsed": true |
| 29 | + }, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "#Pickle library to persist the data\n", |
| 33 | + "import pickle, os\n", |
| 34 | + "\n", |
| 35 | + "#main Class address book which has methods for adding, displaying the contacts\n", |
| 36 | + "class AddressBook(object):\n", |
| 37 | + " def __init__(self, name = None, address = None, email = None, phone = None):\n", |
| 38 | + " self.name = name\n", |
| 39 | + " self.address = address\n", |
| 40 | + " self.email = email\n", |
| 41 | + " self.phone = phone\n", |
| 42 | + " self.contacts = {}\n", |
| 43 | + " self.filename = 'addressbook'\n", |
| 44 | + "\n", |
| 45 | + " def __str__(self):\n", |
| 46 | + " return '[Name: {0} | Address: {1} | Email: {2} | Phone: {3}]'.format(self.name, self.address, self.email, self.phone)\n", |
| 47 | + "\n", |
| 48 | + " def __repr__(self):\n", |
| 49 | + " return '[Name: {0} | Address: {1} | Email: {2} | Phone: {3}]'.format(self.name, self.address, self.email, self.phone)\n", |
| 50 | + "\n", |
| 51 | + " # Adding details provided by the user in our Address Book\n", |
| 52 | + " def addContacts(self):\n", |
| 53 | + " try:\n", |
| 54 | + " if os.path.exists(self.filename) and os.path.getsize(self.filename) > 0:\n", |
| 55 | + " myAddressBook = open(self.filename, 'rb')\n", |
| 56 | + " data = pickle.load(myAddressBook)\n", |
| 57 | + " myAddressBook.close()\n", |
| 58 | + " else:\n", |
| 59 | + " myAddressBook = open(self.filename, 'wb')\n", |
| 60 | + " data = {}\n", |
| 61 | + "\n", |
| 62 | + " contact = self.getDetailsFromUser()\n", |
| 63 | + " data[contact['Name']] = contact\n", |
| 64 | + " myAddressBook = open(self.filename, 'wb')\n", |
| 65 | + " pickle.dump(data, myAddressBook)\n", |
| 66 | + " myAddressBook.close()\n", |
| 67 | + " print('Contact Added Successfully!')\n", |
| 68 | + " except:\n", |
| 69 | + " print('There was an error! Contact was not added.')\n", |
| 70 | + " finally:\n", |
| 71 | + " myAddressBook.close()\n", |
| 72 | + "\n", |
| 73 | + " # Getting the details from the user to adding the Address Book\n", |
| 74 | + " def getDetailsFromUser(self):\n", |
| 75 | + " try:\n", |
| 76 | + " self.contacts['Name'] = str(input('Enter Contact\\'s Full Name: '))\n", |
| 77 | + " self.contacts['Address'] = str(input('Enter Contact\\'s Address: '))\n", |
| 78 | + " self.contacts['Email'] = str(input('Enter Contact\\'s Email Address: '))\n", |
| 79 | + " self.contacts['Phone'] = int(input('Enter Contact\\'s Phone Number: '))\n", |
| 80 | + " return self.contacts\n", |
| 81 | + " except KeyboardInterrupt as error:\n", |
| 82 | + " raise error\n", |
| 83 | + "\n", |
| 84 | + " # To display ALL the contact in our Address Book\n", |
| 85 | + " def displayContacts(self):\n", |
| 86 | + " if os.path.exists(self.filename) and os.path.getsize(self.filename) > 0:\n", |
| 87 | + " myAddressBook = open(self.filename, 'rb')\n", |
| 88 | + " data = pickle.load(myAddressBook)\n", |
| 89 | + " myAddressBook.close()\n", |
| 90 | + " if data:\n", |
| 91 | + " for records in data.values():\n", |
| 92 | + " print(records)\n", |
| 93 | + " myAddressBook.close()\n", |
| 94 | + " else:\n", |
| 95 | + " print('No Record in database.')\n", |
| 96 | + "\n", |
| 97 | + " # To search for a specific contact in our Address Book\n", |
| 98 | + " def searchContacts(self):\n", |
| 99 | + " if os.path.exists(self.filename) and os.path.getsize(self.filename) > 0:\n", |
| 100 | + " myAddressBook = open(self.filename, 'rb')\n", |
| 101 | + " data = pickle.load(myAddressBook)\n", |
| 102 | + " myAddressBook.close()\n", |
| 103 | + " try:\n", |
| 104 | + " contactToSearch = input('Enter the name of the contact to search: ')\n", |
| 105 | + " counter = 0\n", |
| 106 | + " for contact in data.values():\n", |
| 107 | + " if contactToSearch in contact['Name']:\n", |
| 108 | + " print(data[contact['Name']])\n", |
| 109 | + " counter += 1\n", |
| 110 | + " if counter == 0:\n", |
| 111 | + " print('No record found whose name is:', contactToSearch)\n", |
| 112 | + " except:\n", |
| 113 | + " print('Error occured!')\n", |
| 114 | + " else:\n", |
| 115 | + " print('No Record in database.')\n", |
| 116 | + "\n", |
| 117 | + " # For modifying contacts\n", |
| 118 | + " def modifyContacts(self):\n", |
| 119 | + " if os.path.exists(self.filename) and os.path.getsize(self.filename) > 0:\n", |
| 120 | + " myAddressBook = open(self.filename, 'rb')\n", |
| 121 | + " data = pickle.load(myAddressBook)\n", |
| 122 | + " myAddressBook.close()\n", |
| 123 | + " try:\n", |
| 124 | + " contactToModify = input('Enter the name of the contact to modify (Only enter full name): ')\n", |
| 125 | + " # Search for the record to update\n", |
| 126 | + " for contact in data.values():\n", |
| 127 | + " if contactToModify == contact['Name']:\n", |
| 128 | + " contact = data[contactToModify]\n", |
| 129 | + " break\n", |
| 130 | + " option = int(input('1. To modify name, 2. To modify address, 3. To modify email, 4. To modify phone: '))\n", |
| 131 | + " if option == 1:\n", |
| 132 | + " contact['Name'] = input('Enter Name to modify: ')\n", |
| 133 | + " del data[contactToModify]\n", |
| 134 | + " data[contact['Name']] = contact\n", |
| 135 | + " print('Successful')\n", |
| 136 | + " elif option == 2:\n", |
| 137 | + " contact['Address'] = input('Enter Address to modify: ')\n", |
| 138 | + " del data[contactToModify]\n", |
| 139 | + " data[contactToModify] = contact\n", |
| 140 | + " print('Successful')\n", |
| 141 | + " elif option == 3:\n", |
| 142 | + " contact['Email'] = input('Enter Email to modify: ')\n", |
| 143 | + " del data[contactToModify]\n", |
| 144 | + " data[contactToModify] = contact\n", |
| 145 | + " print('Successful')\n", |
| 146 | + " elif option == 4:\n", |
| 147 | + " contact['Phone'] = input('Enter Phone to modify: ')\n", |
| 148 | + " del data[contactToModify]\n", |
| 149 | + " data[contactToModify] = contact\n", |
| 150 | + " print('Successful')\n", |
| 151 | + " else:\n", |
| 152 | + " print('Incorrect option selected.')\n", |
| 153 | + " except:\n", |
| 154 | + " print('Error occured. No such record found. Try Again!')\n", |
| 155 | + " finally:\n", |
| 156 | + " myAddressBook = open(self.filename, 'wb')\n", |
| 157 | + " pickle.dump(data, myAddressBook)\n", |
| 158 | + " myAddressBook.close()\n", |
| 159 | + " else:\n", |
| 160 | + " print('No Record in database.')\n" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": null, |
| 166 | + "metadata": {}, |
| 167 | + "outputs": [ |
| 168 | + { |
| 169 | + "name": "stdout", |
| 170 | + "output_type": "stream", |
| 171 | + "text": [ |
| 172 | + "Enter 1. To Add Contacts 2. For Searching a Contact 3. For Modifying a Contact 4. To Display Contacts 5. To Exit\n", |
| 173 | + "Enter your choice: 1\n", |
| 174 | + "Enter Contact's Full Name: Swaroop\n", |
| 175 | + "Enter Contact's Address: 38660 lexington Street\n", |
| 176 | + "Enter Contact's Email Address: [email protected]\n", |
| 177 | + "Enter Contact's Phone Number: 2019129101\n", |
| 178 | + "Contact Added Successfully!\n", |
| 179 | + "Enter your choice: 5\n", |
| 180 | + "Enter your choice: 5\n", |
| 181 | + "Enter your choice: 5\n", |
| 182 | + "Enter your choice: 4\n", |
| 183 | + "{'Address': '38660 lexington Street', 'Email': '[email protected]', 'Phone': 2019129101, 'Name': 'Swaroop'}\n", |
| 184 | + "Enter your choice: 3\n", |
| 185 | + "Enter the name of the contact to modify (Only enter full name): 2\n", |
| 186 | + "1. To modify name, 2. To modify address, 3. To modify email, 4. To modify phone: 1\n", |
| 187 | + "Enter Name to modify: swaroop\n", |
| 188 | + "Error occured. No such record found. Try Again!\n" |
| 189 | + ] |
| 190 | + }, |
| 191 | + { |
| 192 | + "ename": "KeyboardInterrupt", |
| 193 | + "evalue": "", |
| 194 | + "output_type": "error", |
| 195 | + "traceback": [ |
| 196 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 197 | + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", |
| 198 | + "\u001b[0;32m/anaconda/lib/python3.5/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 729\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 730\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 731\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 199 | + "\u001b[0;32m/anaconda/lib/python3.5/site-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 795\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 796\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 797\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 200 | + "\u001b[0;32m/anaconda/lib/python3.5/site-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 394\u001b[0m \"\"\"\n\u001b[0;32m--> 395\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 396\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 201 | + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:7683)\u001b[0;34m()\u001b[0m\n", |
| 202 | + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:7460)\u001b[0;34m()\u001b[0m\n", |
| 203 | + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy (zmq/backend/cython/socket.c:2344)\u001b[0;34m()\u001b[0m\n", |
| 204 | + "\u001b[0;32m/anaconda/lib/python3.5/site-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:9621)\u001b[0;34m()\u001b[0m\n", |
| 205 | + "\u001b[0;31mKeyboardInterrupt\u001b[0m: ", |
| 206 | + "\nDuring handling of the above exception, another exception occurred:\n", |
| 207 | + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", |
| 208 | + "\u001b[0;32m<ipython-input-3-37ecbaa3772d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Enter 1. To Add Contacts 2. For Searching a Contact 3. For Modifying a Contact 4. To Display Contacts 5. To Exit'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mchoice\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Enter your choice: '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mchoice\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mmyBook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maddContacts\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 209 | + "\u001b[0;32m/anaconda/lib/python3.5/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 704\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 705\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 706\u001b[0m )\n\u001b[1;32m 707\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", |
| 210 | + "\u001b[0;32m/anaconda/lib/python3.5/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 734\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 735\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 736\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 737\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 211 | + "\u001b[0;31mKeyboardInterrupt\u001b[0m: " |
| 212 | + ] |
| 213 | + } |
| 214 | + ], |
| 215 | + "source": [ |
| 216 | + "if __name__ == '__main__':\n", |
| 217 | + " myBook = AddressBook()\n", |
| 218 | + " print('Enter 1. To Add Contacts 2. For Searching a Contact 3. For Modifying a Contact 4. To Display Contacts 5. To Exit')\n", |
| 219 | + " while True:\n", |
| 220 | + " choice = int(input('Enter your choice: '))\n", |
| 221 | + " if choice == 1:\n", |
| 222 | + " myBook.addContacts()\n", |
| 223 | + " elif choice == 2:\n", |
| 224 | + " myBook.searchContacts()\n", |
| 225 | + " elif choice == 3:\n", |
| 226 | + " myBook.modifyContacts()\n", |
| 227 | + " elif choice == 4:\n", |
| 228 | + " myBook.displayContacts()\n", |
| 229 | + " elif choice == 5:\n", |
| 230 | + " exit()\n", |
| 231 | + " else:\n", |
| 232 | + " print('Invalid Option. Try Again!')" |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "markdown", |
| 237 | + "metadata": {}, |
| 238 | + "source": [] |
| 239 | + } |
| 240 | + ], |
| 241 | + "metadata": { |
| 242 | + "kernelspec": { |
| 243 | + "display_name": "Python 3", |
| 244 | + "language": "python", |
| 245 | + "name": "python3" |
| 246 | + }, |
| 247 | + "language_info": { |
| 248 | + "codemirror_mode": { |
| 249 | + "name": "ipython", |
| 250 | + "version": 3 |
| 251 | + }, |
| 252 | + "file_extension": ".py", |
| 253 | + "mimetype": "text/x-python", |
| 254 | + "name": "python", |
| 255 | + "nbconvert_exporter": "python", |
| 256 | + "pygments_lexer": "ipython3", |
| 257 | + "version": "3.5.4" |
| 258 | + } |
| 259 | + }, |
| 260 | + "nbformat": 4, |
| 261 | + "nbformat_minor": 2 |
| 262 | +} |
0 commit comments