module 6 start

This commit is contained in:
Yavuz Sava 2025-01-27 16:51:28 +03:00
parent 2b36dffae3
commit b2d1b97ff5
2 changed files with 137 additions and 1 deletions

View File

@ -878,7 +878,15 @@
" fullname = str(argv[1] + \" \" + argv[2])\n",
"IndexError: list index out of range\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python3\n",
"import unittest\n",
"from emails import find_email\n",
@ -892,8 +900,129 @@
" expected = \"Missing parameters\"\n",
" self.assertEqual(find_email(testcase), expected)\n",
"if __name__ == '__main__':\n",
" unittest.main()\n",
"\n",
"# Output:\n",
"# .E\n",
"# ======================================================================\n",
"# ERROR: test_one_name (__main__.TestFile)\n",
"# ----------------------------------------------------------------------\n",
"# Traceback (most recent call last):\n",
"# File \"./emails_test.py\", line 15, in test_one_name\n",
"# self.assertEqual(find_email(testcase), expected)\n",
"# File \"/home/student/scripts/emails.py\", line 20, in find_email\n",
"# fullname = str(argv[1] + \" \" + argv[2])\n",
"# IndexError: list index out of range\n",
"# ----------------------------------------------------------------------\n",
"# Ran 2 tests in 0.002s\n",
"# FAILED (errors=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python3\n",
"import sys\n",
"import csv\n",
"def populate_dictionary(filename):\n",
" \"\"\"Populate a dictionary with name/email pairs for easy lookup.\"\"\"\n",
" email_dict = {}\n",
" with open(filename) as csvfile:\n",
" lines = csv.reader(csvfile, delimiter = ',')\n",
" for row in lines:\n",
" name = str(row[0].lower())\n",
" email_dict[name] = row[1]\n",
" return email_dict\n",
"def find_email(argv):\n",
" \"\"\" Return an email address based on the username given.\"\"\"\n",
" # Create the username based on the command line input.\n",
" try:\n",
" fullname = str(argv[1] + \" \" + argv[2])\n",
" # Preprocess the data\n",
" email_dict = populate_dictionary('/home/{{ username }}/data/user_emails.csv')\n",
" # Find and print the email\n",
" return email_dict.get(fullname.lower())\n",
" except IndexError:\n",
" return \"Missing parameters\"\n",
"def main():\n",
" print(find_email(sys.argv))\n",
"if __name__ == \"__main__\":\n",
" main()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Test Case 2: Random email address**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python3\n",
"import unittest\n",
"from emails import find_email\n",
"class EmailsTest(unittest.TestCase):\n",
" def test_basic(self):\n",
" testcase = [None, \"Bree\", \"Campbell\"]\n",
" expected = \"breee@abc.edu\"\n",
" self.assertEqual(find_email(testcase), expected)\n",
" def test_one_name(self):\n",
" testcase = [None, \"John\"]\n",
" expected = \"Missing parameters\"\n",
" self.assertEqual(find_email(testcase), expected)\n",
" def test_two_name(self):\n",
" testcase = [None, \"Roy\",\"Cooper\"]\n",
" expected = \"No email address found\"\n",
" self.assertEqual(find_email(testcase), expected)\n",
"if __name__ == '__main__':\n",
" unittest.main()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python3\n",
"import csv\n",
"import sys\n",
"def populate_dictionary(filename):\n",
" \"\"\"Populate a dictionary with name/email pairs for easy lookup.\"\"\"\n",
" email_dict = {}\n",
" with open(filename) as csvfile:\n",
" lines = csv.reader(csvfile, delimiter = ',')\n",
" for row in lines:\n",
" name = str(row[0].lower())\n",
" email_dict[name] = row[1]\n",
" return email_dict\n",
"def find_email(argv):\n",
" \"\"\" Return an email address based on the username given.\"\"\"\n",
" # Create the username based on the command line input.\n",
" try:\n",
" fullname = str(argv[1] + \" \" + argv[2])\n",
" # Preprocess the data\n",
" email_dict = populate_dictionary('/home/{{ username }}/data/user_emails.csv')\n",
" # If email exists, print it\n",
" if email_dict.get(fullname.lower()):\n",
" return email_dict.get(fullname.lower())\n",
" else:\n",
" return \"No email address found\"\n",
" except IndexError:\n",
" return \"Missing parameters\"\n",
"def main():\n",
" print(find_email(sys.argv))\n",
"if __name__ == \"__main__\":\n",
" main()\n"
]
}
],
"metadata": {

View File

@ -1,5 +1,12 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Interacting with the Command Line Shell"
]
},
{
"cell_type": "code",
"execution_count": null,