- Write Python code using any editor.
- Execute the program from the command line.
- Unit test the program.
- Upload on Canvas for submission
-
Setting Up Python Environment
- Verify Python Installation:
- Open your terminal (Command Prompt for Windows, Terminal for macOS/Linux).
- Type
python --versionand press Enter. - You should see the installed Python version displayed.
-
Configuring Visual Studio Code (VS Code)
- Install Python Extension:
- Open VS Code.
- Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or by pressing Ctrl+Shift+X.
- Search for
Pythonin the Extensions Marketplace. - Click “Install” on the Python extension by Microsoft.
-
Running Python in VS Code
- Create a New Python File:
- In VS Code, open a folder where you want to save your Python projects.
- Click on File > New File or press Ctrl+N to create a new file.
- Save the file with a .py extension (e.g.,
hello.py).
-
Write a Python program that performs as a Tuffy Titan Contact List which contains a dictionary of contacts that is stored on the hard drive and that can have contacts added, modified, or deleted.
-
Create a
contactsmodule to meet the following requirements:- Create a file named
contacts.py.- Define a class named
Contacts.- Define a member function named
__init__to meet the following requirements:- Take a
selfas a positional parameter. - Take a
filenameas a keyword parameter. - Set a member variable equal to the filename.
- Set a member varialbe equal to an empty data dictionary.
- Open the filename and load the JSON decoded contents to the empty data dictionary.
- Cleanly manage the
FileNotFoundErrorif the filename does not exist.
- Take a
- Define a member function named
add_contactto meet the following requirements:- Take a
selfas a positional parameter. - Take a
idas a keyword parameter. - Take a
first_nameas a keyword parameter. - Take a
last_nameas a keyword parameter. - If the id exists in the data dictionary, return the string
error. - Set the id:[first_name, last_name] key:value pair to the data dictionary.
- Sort the data dictionary in ascending order by last name, and then by first name, ignoring case.
- Write the contents of the data dictionary to the filename that was set to the member variable.
- Return the key:value pair that was added.
- Take a
- Define a member function named
modify_contactto meet the following requirements:- Take a
selfas a positional parameter. - Take a
idas a keyword parameter. - Take a
first_nameas a keyword parameter. - Take a
last_nameas a keyword parameter. - If the id does not exists in the dictionary, return the string
error. - Set the id:[first_name, last_name'] key:value pair to the contact dictionary.
- Sort the data dictionary in ascending order by last name, and then by first name, ignoring case.
- Write the contents of the data dictionary to the filename that was set to the member variable.
- Return the key:value pair that was modified.
- Take a
- Define a member function named
delete_contactto meet the following requirements:- Take a
selfas a positional parameter. - Take a
idas a keyword parameter. - If the id does not exists in the dictionary, return the string
error. - Remove the key:value pair with the key equal to id.
- Write the contents of the data dictionary to the filename that was set to the member variable.
- Return the key:value pair with the key equal to id.
- Take a
- Define a member function named
- Define a class named
- Create a file named
-
Create a
maindriver program to meet the following requirements:- Create a file named
main.py. - Import the
contactsmodule. - Instantiate a
Contactsobject with any default filename. - Implement a menu within a loop with following choices:
- Add contact
- Modify contact
- Delete contact
- Print contact list
- Set contact filename
- Exit the program
- Prompt the user for the menu choice.
- Prompt the user for the information needed for the appropriate
Contactsmember function and call the function. - Print out appropriate errors with function return values of
error.
- Create a file named
-
Run the program using the command below and repeat the steps above until you are satisfied your program output meets the above requirements.
python3 -m main -
Typical input and output for the program:
*** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 1 Enter phone number: 7145551212 Enter first name: Steve Enter last name: Jobs Added: Steve Jobs. *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 1 Enter phone number: 5625553333 Enter first name: Bill Enter last name: Gates Added: Bill Gates. *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 4 ==================== CONTACT LIST ==================== Last Name First Name Phone ==================== ==================== ========== Gates Bill 5625553333 Jobs Steve 7145551212 *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 1 Enter phone number: 7145551111 Enter first name: Alpha Enter last name: Jobs Added: Alpha Jobs. *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 4 ==================== CONTACT LIST ==================== Last Name First Name Phone ==================== ==================== ========== Gates Bill 5625553333 Jobs Alpha 7145551111 Jobs Steve 7145551212 *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 1 Enter phone number: 7145551111 Enter first name: Richard Enter last name: Stallman Phone number already exists. *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 2 Enter phone number: 7145551111 Enter first name: Richard Enter last name: Stallman Modified: Richard Stallman. *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 4 ==================== CONTACT LIST ==================== Last Name First Name Phone ==================== ==================== ========== Gates Bill 5625553333 Jobs Steve 7145551212 Stallman Richard 7145551111 *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 3 Enter phone number: 5625553333 Deleted: Bill Gates. *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 4 ==================== CONTACT LIST ==================== Last Name First Name Phone ==================== ==================== ========== Jobs Steve 7145551212 Stallman Richard 7145551111 *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 3 Enter phone number: 7145559999 Invalid phone number. *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 4 ==================== CONTACT LIST ==================== Last Name First Name Phone ==================== ==================== ========== Jobs Steve 7145551212 Stallman Richard 7145551111 *** TUFFY TITAN CONTACT MAIN MENU 1. Add contact 2. Modify contact 3. Delete contact 4. Print contact list 5. Set contact filename 6. Exit the program Enter menu choice: 6 -
Run the unit testing program to ensure that your program runs as expected.
For Linux and Mac users
./test.sh or source ./test.shFor windows users
./win_test.batThe unit testing will output the results of a series of tests using specific input and expected output. Any error will provide information on where the expected output is different from the actual output. You will need to edit your source code to fix the error and run
./test.shor./win_test.batrepeatedly until it passes all the test.
Submit a zip file containing all the code files on canvas
Naming Convention: _.zip
Your zipped folder should contain below files:
CWID_LASTNAME.zip -
| > test.py
| > test.sh
| > main.py
| > contacts.py
| > test.txt
| > win_test.bat
- All points add up to a total of 100 points possible as detailed below. Partial credit will be given where applicable.
| Points | Description |
|---|---|
| 50 | Environment setup and Lab Submission |
| 10 | main.py file submitted and meets the program requirements |
| 10 | contacts.py file submitted and meets the program requirements |
| 5 | unit test passes Test01_AddContact |
| 5 | unit test passes Test02_AddContact |
| 5 | unit test passes Test03_ModifyContact |
| 5 | unit test passes Test04_ModifyContact |
| 5 | unit test passes Test05_DeleteContact |
| 5 | unit test passes Test06_DeleteContact |