From a2119706c34a825dfef7204041d49cb29a32ab5b Mon Sep 17 00:00:00 2001 From: Corey Schafer Date: Tue, 15 Aug 2017 22:46:20 -0600 Subject: [PATCH] Updated Code Snippets --- Cron-Tasks/snippets.txt | 19 +++++ Python-Unit-Testing/calc.py | 21 +++++ Python-Unit-Testing/employee.py | 31 ++++++++ Python-Unit-Testing/snippets.txt | 115 +++++++++++++++++++++++++++ Python-Unit-Testing/test_calc.py | 33 ++++++++ Python-Unit-Testing/test_employee.py | 71 +++++++++++++++++ random_data.py | 29 +++++++ random_names.py | 11 --- 8 files changed, 319 insertions(+), 11 deletions(-) create mode 100644 Cron-Tasks/snippets.txt create mode 100644 Python-Unit-Testing/calc.py create mode 100644 Python-Unit-Testing/employee.py create mode 100644 Python-Unit-Testing/snippets.txt create mode 100644 Python-Unit-Testing/test_calc.py create mode 100644 Python-Unit-Testing/test_employee.py create mode 100644 random_data.py delete mode 100644 random_names.py diff --git a/Cron-Tasks/snippets.txt b/Cron-Tasks/snippets.txt new file mode 100644 index 000000000..137ec5e1d --- /dev/null +++ b/Cron-Tasks/snippets.txt @@ -0,0 +1,19 @@ +# ┌───────────── minute (0 - 59) +# │ ┌───────────── hour (0 - 23) +# │ │ ┌───────────── day of month (1 - 31) +# │ │ │ ┌───────────── month (1 - 12) +# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday; +# │ │ │ │ │ 7 is also Sunday on some systems) +# │ │ │ │ │ +# │ │ │ │ │ +# * * * * * command_to_execute + + + +###### Sample crontab ###### + +# Empty temp folder every Friday at 5pm +0 5 * * 5 rm -rf /tmp/* + +# Backup images to Google Drive every night at midnight +0 0 * * * rsync -a ~/Pictures/ ~/Google\ Drive/Pictures/ diff --git a/Python-Unit-Testing/calc.py b/Python-Unit-Testing/calc.py new file mode 100644 index 000000000..27e39d313 --- /dev/null +++ b/Python-Unit-Testing/calc.py @@ -0,0 +1,21 @@ + +def add(x, y): + """Add Function""" + return x + y + + +def subtract(x, y): + """Subtract Function""" + return x - y + + +def multiply(x, y): + """Multiply Function""" + return x * y + + +def divide(x, y): + """Divide Function""" + if y == 0: + raise ValueError('Can not divide by zero!') + return x / y diff --git a/Python-Unit-Testing/employee.py b/Python-Unit-Testing/employee.py new file mode 100644 index 000000000..0470fe328 --- /dev/null +++ b/Python-Unit-Testing/employee.py @@ -0,0 +1,31 @@ + +import requests + + +class Employee: + """A sample Employee class""" + + raise_amt = 1.05 + + def __init__(self, first, last, pay): + self.first = first + self.last = last + self.pay = pay + + @property + def email(self): + return '{}.{}@email.com'.format(self.first, self.last) + + @property + def fullname(self): + return '{} {}'.format(self.first, self.last) + + def apply_raise(self): + self.pay = int(self.pay * self.raise_amt) + + def monthly_schedule(self, month): + response = requests.get(f'http://company.com/{self.last}/{month}') + if response.ok: + return response.text + else: + return 'Bad Response!' diff --git a/Python-Unit-Testing/snippets.txt b/Python-Unit-Testing/snippets.txt new file mode 100644 index 000000000..c67a9ba28 --- /dev/null +++ b/Python-Unit-Testing/snippets.txt @@ -0,0 +1,115 @@ +import unittest +from employee import Employee + + +class TestEmployee(unittest.TestCase): + + def test_email(self): + emp_1 = Employee('Corey', 'Schafer', 50000) + emp_2 = Employee('Sue', 'Smith', 60000) + + self.assertEqual(emp_1.email, 'Corey.Schafer@email.com') + self.assertEqual(emp_2.email, 'Sue.Smith@email.com') + + emp_1.first = 'John' + emp_2.first = 'Jane' + + self.assertEqual(emp_1.email, 'John.Schafer@email.com') + self.assertEqual(emp_2.email, 'Jane.Smith@email.com') + + def test_fullname(self): + emp_1 = Employee('Corey', 'Schafer', 50000) + emp_2 = Employee('Sue', 'Smith', 60000) + + self.assertEqual(emp_1.fullname, 'Corey Schafer') + self.assertEqual(emp_2.fullname, 'Sue Smith') + + emp_1.first = 'John' + emp_2.first = 'Jane' + + self.assertEqual(emp_1.fullname, 'John Schafer') + self.assertEqual(emp_2.fullname, 'Jane Smith') + + def test_apply_raise(self): + emp_1 = Employee('Corey', 'Schafer', 50000) + emp_2 = Employee('Sue', 'Smith', 60000) + + emp_1.apply_raise() + emp_2.apply_raise() + + self.assertEqual(emp_1.pay, 52500) + self.assertEqual(emp_2.pay, 63000) + + +if __name__ == '__main__': + unittest.main() + +###### With Prints ###### + +import unittest +from employee import Employee + + +class TestEmployee(unittest.TestCase): + + def setUp(self): + print('setUp') + self.emp_1 = Employee('Corey', 'Schafer', 50000) + self.emp_2 = Employee('Sue', 'Smith', 60000) + + def tearDown(self): + print('tearDown\n') + + def test_email(self): + print('test_email') + self.assertEqual(self.emp_1.email, 'Corey.Schafer@email.com') + self.assertEqual(self.emp_2.email, 'Sue.Smith@email.com') + + self.emp_1.first = 'John' + self.emp_2.first = 'Jane' + + self.assertEqual(self.emp_1.email, 'John.Schafer@email.com') + self.assertEqual(self.emp_2.email, 'Jane.Smith@email.com') + + def test_fullname(self): + print('test_fullname') + self.assertEqual(self.emp_1.fullname, 'Corey Schafer') + self.assertEqual(self.emp_2.fullname, 'Sue Smith') + + self.emp_1.first = 'John' + self.emp_2.first = 'Jane' + + self.assertEqual(self.emp_1.fullname, 'John Schafer') + self.assertEqual(self.emp_2.fullname, 'Jane Smith') + + def test_apply_raise(self): + print('test_apply_raise') + self.emp_1.apply_raise() + self.emp_2.apply_raise() + + self.assertEqual(self.emp_1.pay, 52500) + self.assertEqual(self.emp_2.pay, 63000) + + +if __name__ == '__main__': + unittest.main() + + +###### setUpClass and tearDownClass ###### + +@classmethod +def setUpClass(cls): + print('setupClass') + +@classmethod +def tearDownClass(cls): + print('teardownClass') + + +##### Mocking ##### +def monthly_schedule(self, month): + response = requests.get(f'http://company.com/{self.last}/{month}') + if response.ok: + return response.text + else: + return 'Bad Response!' diff --git a/Python-Unit-Testing/test_calc.py b/Python-Unit-Testing/test_calc.py new file mode 100644 index 000000000..c6d15d74c --- /dev/null +++ b/Python-Unit-Testing/test_calc.py @@ -0,0 +1,33 @@ +import unittest +import calc + + +class TestCalc(unittest.TestCase): + + def test_add(self): + self.assertEqual(calc.add(10, 5), 15) + self.assertEqual(calc.add(-1, 1), 0) + self.assertEqual(calc.add(-1, -1), -2) + + def test_subtract(self): + self.assertEqual(calc.subtract(10, 5), 5) + self.assertEqual(calc.subtract(-1, 1), -2) + self.assertEqual(calc.subtract(-1, -1), 0) + + def test_multiply(self): + self.assertEqual(calc.multiply(10, 5), 50) + self.assertEqual(calc.multiply(-1, 1), -1) + self.assertEqual(calc.multiply(-1, -1), 1) + + def test_divide(self): + self.assertEqual(calc.divide(10, 5), 2) + self.assertEqual(calc.divide(-1, 1), -1) + self.assertEqual(calc.divide(-1, -1), 1) + self.assertEqual(calc.divide(5, 2), 2.5) + + with self.assertRaises(ValueError): + calc.divide(10, 0) + + +if __name__ == '__main__': + unittest.main() diff --git a/Python-Unit-Testing/test_employee.py b/Python-Unit-Testing/test_employee.py new file mode 100644 index 000000000..03d127ad7 --- /dev/null +++ b/Python-Unit-Testing/test_employee.py @@ -0,0 +1,71 @@ +import unittest +from unittest.mock import patch +from employee import Employee + + +class TestEmployee(unittest.TestCase): + + @classmethod + def setUpClass(cls): + print('setupClass') + + @classmethod + def tearDownClass(cls): + print('teardownClass') + + def setUp(self): + print('setUp') + self.emp_1 = Employee('Corey', 'Schafer', 50000) + self.emp_2 = Employee('Sue', 'Smith', 60000) + + def tearDown(self): + print('tearDown\n') + + def test_email(self): + print('test_email') + self.assertEqual(self.emp_1.email, 'Corey.Schafer@email.com') + self.assertEqual(self.emp_2.email, 'Sue.Smith@email.com') + + self.emp_1.first = 'John' + self.emp_2.first = 'Jane' + + self.assertEqual(self.emp_1.email, 'John.Schafer@email.com') + self.assertEqual(self.emp_2.email, 'Jane.Smith@email.com') + + def test_fullname(self): + print('test_fullname') + self.assertEqual(self.emp_1.fullname, 'Corey Schafer') + self.assertEqual(self.emp_2.fullname, 'Sue Smith') + + self.emp_1.first = 'John' + self.emp_2.first = 'Jane' + + self.assertEqual(self.emp_1.fullname, 'John Schafer') + self.assertEqual(self.emp_2.fullname, 'Jane Smith') + + def test_apply_raise(self): + print('test_apply_raise') + self.emp_1.apply_raise() + self.emp_2.apply_raise() + + self.assertEqual(self.emp_1.pay, 52500) + self.assertEqual(self.emp_2.pay, 63000) + + def test_monthly_schedule(self): + with patch('employee.requests.get') as mocked_get: + mocked_get.return_value.ok = True + mocked_get.return_value.text = 'Success' + + schedule = self.emp_1.monthly_schedule('May') + mocked_get.assert_called_with('http://company.com/Schafer/May') + self.assertEqual(schedule, 'Success') + + mocked_get.return_value.ok = False + + schedule = self.emp_2.monthly_schedule('June') + mocked_get.assert_called_with('http://company.com/Smith/June') + self.assertEqual(schedule, 'Bad Response!') + + +if __name__ == '__main__': + unittest.main() diff --git a/random_data.py b/random_data.py new file mode 100644 index 000000000..cdc566b82 --- /dev/null +++ b/random_data.py @@ -0,0 +1,29 @@ +''' Super simple module to create basic random data for tutorials''' +import random + +first_names = ['John', 'Jane', 'Corey', 'Travis', 'Dave', 'Kurt', 'Neil', 'Sam', 'Steve', 'Tom', 'James', 'Robert', 'Michael', 'Charles', 'Joe', 'Mary', 'Maggie', 'Nicole', 'Patricia', 'Linda', 'Barbara', 'Elizabeth', 'Laura', 'Jennifer', 'Maria'] + +last_names = ['Smith', 'Doe', 'Jenkins', 'Robinson', 'Davis', 'Stuart', 'Jefferson', 'Jacobs', 'Wright', 'Patterson', 'Wilks', 'Arnold', 'Johnson', 'Williams', 'Jones', 'Brown', 'Davis', 'Miller', 'Wilson', 'Moore', 'Taylor', 'Anderson', 'Thomas', 'Jackson', 'White', 'Harris', 'Martin'] + +street_names = ['Main', 'High', 'Pearl', 'Maple', 'Park', 'Oak', 'Pine', 'Cedar', 'Elm', 'Washington', 'Lake', 'Hill'] + +fake_cities = ['Metropolis', 'Eerie', "King's Landing", 'Sunnydale', 'Bedrock', 'South Park', 'Atlantis', 'Mordor', 'Olympus', 'Dawnstar', 'Balmora', 'Gotham', 'Springfield', 'Quahog', 'Smalltown', 'Epicburg', 'Pythonville', 'Faketown', 'Westworld', 'Thundera', 'Vice City', 'Blackwater', 'Oldtown', 'Valyria', 'Winterfell', 'Braavos‎', 'Lakeview'] + +states = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'] + +for num in range(100): + first = random.choice(first_names) + last = random.choice(last_names) + + phone = f'{random.randint(100, 999)}-555-{random.randint(1000,9999)}' + + street_num = random.randint(100, 999) + street = random.choice(street_names) + city = random.choice(fake_cities) + state = random.choice(states) + zip_code = random.randint(10000, 99999) + address = f'{street_num} {street} St., {city} {state} {zip_code}' + + email = first.lower() + last.lower() + '@bogusemail.com' + + print(f'{first} {last}\n{phone}\n{address}\n{email}\n') diff --git a/random_names.py b/random_names.py deleted file mode 100644 index dee0b22c5..000000000 --- a/random_names.py +++ /dev/null @@ -1,11 +0,0 @@ -''' Super simple module to get basic random names and emails''' -import random - -random_first = ['John', 'Jane', 'Corey', 'Travis', 'Dave', 'Kurt', 'Neil', 'Sam', 'Mary', 'Maggie', 'Nicole', 'Steve', 'Tom'] -random_last = ['Smith', 'Doe', 'Jenkins', 'Robinson', 'Davis', 'Stuart', 'Jefferson', 'Jacobs', 'Wright', 'Patterson', 'Wilks', 'Arnold'] - -for num in range(20): - first = random.choice(random_first) - last = random.choice(random_last) - email = first.lower() + last.lower() + '@bogusemail.com' - print(f'{first} {last} - {email}')