Skip to content

Commit a211970

Browse files
committed
Updated Code Snippets
1 parent 5f9d635 commit a211970

File tree

8 files changed

+319
-11
lines changed

8 files changed

+319
-11
lines changed

Cron-Tasks/snippets.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ┌───────────── minute (0 - 59)
2+
# │ ┌───────────── hour (0 - 23)
3+
# │ │ ┌───────────── day of month (1 - 31)
4+
# │ │ │ ┌───────────── month (1 - 12)
5+
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
6+
# │ │ │ │ │ 7 is also Sunday on some systems)
7+
# │ │ │ │ │
8+
# │ │ │ │ │
9+
# * * * * * command_to_execute
10+
11+
12+
13+
###### Sample crontab ######
14+
15+
# Empty temp folder every Friday at 5pm
16+
0 5 * * 5 rm -rf /tmp/*
17+
18+
# Backup images to Google Drive every night at midnight
19+
0 0 * * * rsync -a ~/Pictures/ ~/Google\ Drive/Pictures/

Python-Unit-Testing/calc.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
def add(x, y):
3+
"""Add Function"""
4+
return x + y
5+
6+
7+
def subtract(x, y):
8+
"""Subtract Function"""
9+
return x - y
10+
11+
12+
def multiply(x, y):
13+
"""Multiply Function"""
14+
return x * y
15+
16+
17+
def divide(x, y):
18+
"""Divide Function"""
19+
if y == 0:
20+
raise ValueError('Can not divide by zero!')
21+
return x / y

Python-Unit-Testing/employee.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
import requests
3+
4+
5+
class Employee:
6+
"""A sample Employee class"""
7+
8+
raise_amt = 1.05
9+
10+
def __init__(self, first, last, pay):
11+
self.first = first
12+
self.last = last
13+
self.pay = pay
14+
15+
@property
16+
def email(self):
17+
return '{}.{}@email.com'.format(self.first, self.last)
18+
19+
@property
20+
def fullname(self):
21+
return '{} {}'.format(self.first, self.last)
22+
23+
def apply_raise(self):
24+
self.pay = int(self.pay * self.raise_amt)
25+
26+
def monthly_schedule(self, month):
27+
response = requests.get(f'http://company.com/{self.last}/{month}')
28+
if response.ok:
29+
return response.text
30+
else:
31+
return 'Bad Response!'

Python-Unit-Testing/snippets.txt

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import unittest
2+
from employee import Employee
3+
4+
5+
class TestEmployee(unittest.TestCase):
6+
7+
def test_email(self):
8+
emp_1 = Employee('Corey', 'Schafer', 50000)
9+
emp_2 = Employee('Sue', 'Smith', 60000)
10+
11+
self.assertEqual(emp_1.email, '[email protected]')
12+
self.assertEqual(emp_2.email, '[email protected]')
13+
14+
emp_1.first = 'John'
15+
emp_2.first = 'Jane'
16+
17+
self.assertEqual(emp_1.email, '[email protected]')
18+
self.assertEqual(emp_2.email, '[email protected]')
19+
20+
def test_fullname(self):
21+
emp_1 = Employee('Corey', 'Schafer', 50000)
22+
emp_2 = Employee('Sue', 'Smith', 60000)
23+
24+
self.assertEqual(emp_1.fullname, 'Corey Schafer')
25+
self.assertEqual(emp_2.fullname, 'Sue Smith')
26+
27+
emp_1.first = 'John'
28+
emp_2.first = 'Jane'
29+
30+
self.assertEqual(emp_1.fullname, 'John Schafer')
31+
self.assertEqual(emp_2.fullname, 'Jane Smith')
32+
33+
def test_apply_raise(self):
34+
emp_1 = Employee('Corey', 'Schafer', 50000)
35+
emp_2 = Employee('Sue', 'Smith', 60000)
36+
37+
emp_1.apply_raise()
38+
emp_2.apply_raise()
39+
40+
self.assertEqual(emp_1.pay, 52500)
41+
self.assertEqual(emp_2.pay, 63000)
42+
43+
44+
if __name__ == '__main__':
45+
unittest.main()
46+
47+
###### With Prints ######
48+
49+
import unittest
50+
from employee import Employee
51+
52+
53+
class TestEmployee(unittest.TestCase):
54+
55+
def setUp(self):
56+
print('setUp')
57+
self.emp_1 = Employee('Corey', 'Schafer', 50000)
58+
self.emp_2 = Employee('Sue', 'Smith', 60000)
59+
60+
def tearDown(self):
61+
print('tearDown\n')
62+
63+
def test_email(self):
64+
print('test_email')
65+
self.assertEqual(self.emp_1.email, '[email protected]')
66+
self.assertEqual(self.emp_2.email, '[email protected]')
67+
68+
self.emp_1.first = 'John'
69+
self.emp_2.first = 'Jane'
70+
71+
self.assertEqual(self.emp_1.email, '[email protected]')
72+
self.assertEqual(self.emp_2.email, '[email protected]')
73+
74+
def test_fullname(self):
75+
print('test_fullname')
76+
self.assertEqual(self.emp_1.fullname, 'Corey Schafer')
77+
self.assertEqual(self.emp_2.fullname, 'Sue Smith')
78+
79+
self.emp_1.first = 'John'
80+
self.emp_2.first = 'Jane'
81+
82+
self.assertEqual(self.emp_1.fullname, 'John Schafer')
83+
self.assertEqual(self.emp_2.fullname, 'Jane Smith')
84+
85+
def test_apply_raise(self):
86+
print('test_apply_raise')
87+
self.emp_1.apply_raise()
88+
self.emp_2.apply_raise()
89+
90+
self.assertEqual(self.emp_1.pay, 52500)
91+
self.assertEqual(self.emp_2.pay, 63000)
92+
93+
94+
if __name__ == '__main__':
95+
unittest.main()
96+
97+
98+
###### setUpClass and tearDownClass ######
99+
100+
@classmethod
101+
def setUpClass(cls):
102+
print('setupClass')
103+
104+
@classmethod
105+
def tearDownClass(cls):
106+
print('teardownClass')
107+
108+
109+
##### Mocking #####
110+
def monthly_schedule(self, month):
111+
response = requests.get(f'http://company.com/{self.last}/{month}')
112+
if response.ok:
113+
return response.text
114+
else:
115+
return 'Bad Response!'

Python-Unit-Testing/test_calc.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
import calc
3+
4+
5+
class TestCalc(unittest.TestCase):
6+
7+
def test_add(self):
8+
self.assertEqual(calc.add(10, 5), 15)
9+
self.assertEqual(calc.add(-1, 1), 0)
10+
self.assertEqual(calc.add(-1, -1), -2)
11+
12+
def test_subtract(self):
13+
self.assertEqual(calc.subtract(10, 5), 5)
14+
self.assertEqual(calc.subtract(-1, 1), -2)
15+
self.assertEqual(calc.subtract(-1, -1), 0)
16+
17+
def test_multiply(self):
18+
self.assertEqual(calc.multiply(10, 5), 50)
19+
self.assertEqual(calc.multiply(-1, 1), -1)
20+
self.assertEqual(calc.multiply(-1, -1), 1)
21+
22+
def test_divide(self):
23+
self.assertEqual(calc.divide(10, 5), 2)
24+
self.assertEqual(calc.divide(-1, 1), -1)
25+
self.assertEqual(calc.divide(-1, -1), 1)
26+
self.assertEqual(calc.divide(5, 2), 2.5)
27+
28+
with self.assertRaises(ValueError):
29+
calc.divide(10, 0)
30+
31+
32+
if __name__ == '__main__':
33+
unittest.main()

Python-Unit-Testing/test_employee.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import unittest
2+
from unittest.mock import patch
3+
from employee import Employee
4+
5+
6+
class TestEmployee(unittest.TestCase):
7+
8+
@classmethod
9+
def setUpClass(cls):
10+
print('setupClass')
11+
12+
@classmethod
13+
def tearDownClass(cls):
14+
print('teardownClass')
15+
16+
def setUp(self):
17+
print('setUp')
18+
self.emp_1 = Employee('Corey', 'Schafer', 50000)
19+
self.emp_2 = Employee('Sue', 'Smith', 60000)
20+
21+
def tearDown(self):
22+
print('tearDown\n')
23+
24+
def test_email(self):
25+
print('test_email')
26+
self.assertEqual(self.emp_1.email, '[email protected]')
27+
self.assertEqual(self.emp_2.email, '[email protected]')
28+
29+
self.emp_1.first = 'John'
30+
self.emp_2.first = 'Jane'
31+
32+
self.assertEqual(self.emp_1.email, '[email protected]')
33+
self.assertEqual(self.emp_2.email, '[email protected]')
34+
35+
def test_fullname(self):
36+
print('test_fullname')
37+
self.assertEqual(self.emp_1.fullname, 'Corey Schafer')
38+
self.assertEqual(self.emp_2.fullname, 'Sue Smith')
39+
40+
self.emp_1.first = 'John'
41+
self.emp_2.first = 'Jane'
42+
43+
self.assertEqual(self.emp_1.fullname, 'John Schafer')
44+
self.assertEqual(self.emp_2.fullname, 'Jane Smith')
45+
46+
def test_apply_raise(self):
47+
print('test_apply_raise')
48+
self.emp_1.apply_raise()
49+
self.emp_2.apply_raise()
50+
51+
self.assertEqual(self.emp_1.pay, 52500)
52+
self.assertEqual(self.emp_2.pay, 63000)
53+
54+
def test_monthly_schedule(self):
55+
with patch('employee.requests.get') as mocked_get:
56+
mocked_get.return_value.ok = True
57+
mocked_get.return_value.text = 'Success'
58+
59+
schedule = self.emp_1.monthly_schedule('May')
60+
mocked_get.assert_called_with('http://company.com/Schafer/May')
61+
self.assertEqual(schedule, 'Success')
62+
63+
mocked_get.return_value.ok = False
64+
65+
schedule = self.emp_2.monthly_schedule('June')
66+
mocked_get.assert_called_with('http://company.com/Smith/June')
67+
self.assertEqual(schedule, 'Bad Response!')
68+
69+
70+
if __name__ == '__main__':
71+
unittest.main()

random_data.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
''' Super simple module to create basic random data for tutorials'''
2+
import random
3+
4+
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']
5+
6+
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']
7+
8+
street_names = ['Main', 'High', 'Pearl', 'Maple', 'Park', 'Oak', 'Pine', 'Cedar', 'Elm', 'Washington', 'Lake', 'Hill']
9+
10+
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']
11+
12+
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']
13+
14+
for num in range(100):
15+
first = random.choice(first_names)
16+
last = random.choice(last_names)
17+
18+
phone = f'{random.randint(100, 999)}-555-{random.randint(1000,9999)}'
19+
20+
street_num = random.randint(100, 999)
21+
street = random.choice(street_names)
22+
city = random.choice(fake_cities)
23+
state = random.choice(states)
24+
zip_code = random.randint(10000, 99999)
25+
address = f'{street_num} {street} St., {city} {state} {zip_code}'
26+
27+
email = first.lower() + last.lower() + '@bogusemail.com'
28+
29+
print(f'{first} {last}\n{phone}\n{address}\n{email}\n')

random_names.py

-11
This file was deleted.

0 commit comments

Comments
 (0)