diff --git a/__pycache__/testing.cpython-311.pyc b/__pycache__/testing.cpython-311.pyc new file mode 100644 index 0000000..b10e0cc Binary files /dev/null and b/__pycache__/testing.cpython-311.pyc differ diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..ec555be 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,18 +41,146 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, + "id": "2f4d7695", + "metadata": {}, + "outputs": [], + "source": [ + "from testing import *\n", + "import unittest" + ] + }, + { + "cell_type": "code", + "execution_count": 70, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def greater(a,b):\n", + " try:\n", + " return a if a > b else b\n", + " except TypeError:\n", + " print(f\"Warning: Comparing {a} and {b} as str\")\n", + " a = str(a)\n", + " b = str(b)\n", + " return a if a > b else b\n" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "a1a27ebd", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.033s\n", + "\n", + "OK\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Warning: Comparing sdf and 213 as str\n" + ] + }, + { + "data": { + "text/plain": [ + "'sdf'" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "test_greater(greater)\n", + "\n", + "greater(\"sdf\", 213)" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "3484cf5d", + "metadata": {}, + "outputs": [], + "source": [ + "class EmptyListParameter(Exception):\n", + " def __init__(self, message):\n", + " self.message = message\n", + " super().__init__(message)\n", + "\n", + "def greatest(numbers):\n", + " if not numbers: # if numbers is empty list\n", + " raise EmptyListParameter(\"You passed empty list\")\n", + " \n", + " biggest = numbers[0]\n", + " for number in numbers:\n", + " if number > biggest:\n", + " biggest = number \n", + " return biggest\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "id": "6154f9e0", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.025s\n", + "\n", + "OK\n" + ] + }, + { + "ename": "EmptyListParameter", + "evalue": "You passed empty list", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mEmptyListParameter\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[79]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m 1\u001b[39m test_greatest(greatest)\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m \u001b[43mgreatest\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[78]\u001b[39m\u001b[32m, line 8\u001b[39m, in \u001b[36mgreatest\u001b[39m\u001b[34m(numbers)\u001b[39m\n\u001b[32m 6\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mgreatest\u001b[39m(numbers):\n\u001b[32m 7\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m numbers: \u001b[38;5;66;03m# if numbers is empty list\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m8\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m EmptyListParameter(\u001b[33m\"\u001b[39m\u001b[33mYou passed empty list\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 10\u001b[39m biggest = numbers[\u001b[32m0\u001b[39m]\n\u001b[32m 11\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m number \u001b[38;5;129;01min\u001b[39;00m numbers:\n", + "\u001b[31mEmptyListParameter\u001b[39m: You passed empty list" + ] + } + ], + "source": [ + "test_greatest(greatest)\n", + "\n", + "greatest([])" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1c262250", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "ironhack_prework", "language": "python", "name": "python3" }, @@ -66,7 +194,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.14" } }, "nbformat": 4, diff --git a/testing.py b/testing.py new file mode 100644 index 0000000..6a247a7 --- /dev/null +++ b/testing.py @@ -0,0 +1,208 @@ +import unittest +import random +from functools import reduce +from math import factorial +from statistics import stdev, mode +from string import ascii_lowercase, ascii_uppercase, digits + + +def test_greater(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input, output): + super(TestKnown, self).__init__() + self.input = input + self.output = output + + def runTest(self): + self.assertEqual(fn(*self.input), self.output, f"Should be {self.output}") + suite = unittest.TestSuite() + for _ in range(100): + a,b = random.randint(-1000,1000),random.randint(-1000,1000) + suite.addTest(TestKnown([a,b],max([a,b]))) + unittest.TextTestRunner().run(suite) + +def test_greatest(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input, output): + super(TestKnown, self).__init__() + self.input = input + self.output = output + + def runTest(self): + self.assertEqual(fn(self.input), self.output, f"Should be {self.output}") + suite = unittest.TestSuite() + for _ in range(100): + arr = [random.randint(-1000,1000) for _ in range(random.randint(10,100))] + suite.addTest(TestKnown(arr,max(arr))) + unittest.TextTestRunner().run(suite) + +def test_sum(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input, output): + super(TestKnown, self).__init__() + self.input = input + self.output = output + + def runTest(self): + self.assertEqual(fn(self.input), self.output, f"Should be {self.output}") + suite = unittest.TestSuite() + for _ in range(100): + arr = [random.randint(-1000,1000) for _ in range(random.randint(10,100))] + suite.addTest(TestKnown(arr,sum(arr))) + unittest.TextTestRunner().run(suite) + + +def test_mult(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input, output): + super(TestKnown, self).__init__() + self.input = input + self.output = output + + def runTest(self): + self.assertEqual(fn(self.input), self.output, f"Should be {self.output}") + suite = unittest.TestSuite() + for _ in range(100): + arr = [random.randint(-10,10) for _ in range(random.randint(10,100))] + suite.addTest(TestKnown(arr,reduce(lambda a,b:a*b,arr,1))) + unittest.TextTestRunner().run(suite) + +def test_operations(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input, output): + super(TestKnown, self).__init__() + self.input = input + self.output = output + + def runTest(self): + self.assertEqual(fn(*self.input), self.output, f"Should be {self.output}") + suite = unittest.TestSuite() + for _ in range(100): + arr = ([random.randint(-10,10) for _ in range(random.randint(10,100))], random.choice(["+","*"])) + def ans(arr,op): + if op =="+": return sum(arr) + else: return reduce(lambda a,b:a*b,arr,1) + suite.addTest(TestKnown(arr,ans(*arr))) + unittest.TextTestRunner().run(suite) + +def test_factorial(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input, output): + super(TestKnown, self).__init__() + self.input = input + self.output = output + + def runTest(self): + self.assertEqual(fn(self.input), self.output, f"Should be {self.output}") + suite = unittest.TestSuite() + for _ in range(100): + n = random.randint(1,100) + suite.addTest(TestKnown(n,factorial(n))) + unittest.TextTestRunner().run(suite) + + +def test_unique(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input, output): + super(TestKnown, self).__init__() + self.input = input + self.output = output + + def runTest(self): + self.assertEqual(set(fn(self.input)), self.output, f"Should be {self.output}") + suite = unittest.TestSuite() + for _ in range(100): + arr = [random.randint(-100,100) for _ in range(random.randint(10,1000))] + suite.addTest(TestKnown(arr,set(arr))) + unittest.TextTestRunner().run(suite) + +def test_mode(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input, output): + super(TestKnown, self).__init__() + self.input = input + self.output = output + + def runTest(self): + self.assertEqual(fn(self.input), self.output, f"Should be {self.output}") + suite = unittest.TestSuite() + for _ in range(100): + arr = [random.randint(1,25) for _ in range(random.randint(100,125))] + 50 * [random.randint(1,25)] + suite.addTest(TestKnown(arr,mode(arr))) + unittest.TextTestRunner().run(suite) + +def test_stdev(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input, output): + super(TestKnown, self).__init__() + self.input = input + self.output = output + + def runTest(self): + self.assertAlmostEqual(fn(self.input), self.output, delta=5, msg=f"Should be {self.output}") + suite = unittest.TestSuite() + for _ in range(100): + arr = [random.randint(-1000,1000) for _ in range(random.randint(10,100))] + suite.addTest(TestKnown(arr,stdev(arr))) + unittest.TextTestRunner().run(suite) + +pangrams = ["Waltz, nymph, for quick jigs vex Bud.", +"Sphinx of black quartz, judge my vow.", +"Pack my box with five dozen liquor jugs.", +"Glib jocks quiz nymph to vex dwarf.", +"Jackdaws love my big sphinx of quartz.", +"The five boxing wizards jump quickly.", +"How vexingly quick daft zebras jump!", +"Quick zephyrs blow, vexing daft Jim.", +"Two driven jocks help fax my big quiz.", +"The jay, pig, fox, zebra and my wolves quack!", +"Sympathizing would fix Quaker objectives.", +"A wizard's job is to vex chumps quickly in fog.", +"Watch 'Jeopardy!', Alex Trebek's fun TV quiz game.", +"By Jove, my quick study of lexicography won a prize!", +"Waxy and quivering, jocks fumble the pizza."] + +def test_pangram(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input, output): + super(TestKnown, self).__init__() + self.input = input + self.output = output + def runTest(self): + self.assertEqual(fn(self.input), self.output, f"Should be {self.output}") + suite = unittest.TestSuite() + tests = pangrams + ["".join([random.choice(ascii_lowercase) for _ in range(random.randint(25,100))]) for _ in range(15)] + for test in tests: + suite.addTest(TestKnown(test,set(ascii_lowercase).issubset(set(test.lower())))) + unittest.TextTestRunner().run(suite) + +def test_alpha(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input, output): + super(TestKnown, self).__init__() + self.input = input + self.output = output + + def runTest(self): + self.assertEqual(fn(self.input), self.output, f"Should be {self.output}") + suite = unittest.TestSuite() + tests = [",".join(["".join([random.choice(ascii_lowercase) for _ in range(random.randint(4,10))]) for _ in range(random.randint(4,25))]) for _ in range(100)] + for test in tests: + suite.addTest(TestKnown(test,",".join(sorted(test.split(","))))) + unittest.TextTestRunner().run(suite) + +def test_pass(fn): + class TestKnown(unittest.TestCase): + def __init__(self, input_, output): + super(TestKnown, self).__init__() + self.input_ = input_ + self.output = output + + def runTest(self): + self.assertEqual(fn(self.input_), self.output, f"For this password, {self.input_}, the output should be {self.output}") + suite = unittest.TestSuite() + check_p = lambda string: sum([len(set(string)&set(c))>0 for c in [ascii_lowercase, ascii_uppercase, digits, "#@!$%&()^*[]{}"]] + [len(string) >= 8]) >= 5 + tests = ["".join([random.choice(ascii_lowercase*3+ascii_uppercase+digits+"#@!$%&()^*[]{}") for _ in range(random.randint(2,16))]) for _ in range(100)] + for test in tests: + suite.addTest(TestKnown(test,check_p(test))) + unittest.TextTestRunner().run(suite) \ No newline at end of file