Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita211970

Browse files
committed
Updated Code Snippets
1 parent5f9d635 commita211970

File tree

8 files changed

+319
-11
lines changed

8 files changed

+319
-11
lines changed

‎Cron-Tasks/snippets.txt

Lines changed: 19 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
defadd(x,y):
3+
"""Add Function"""
4+
returnx+y
5+
6+
7+
defsubtract(x,y):
8+
"""Subtract Function"""
9+
returnx-y
10+
11+
12+
defmultiply(x,y):
13+
"""Multiply Function"""
14+
returnx*y
15+
16+
17+
defdivide(x,y):
18+
"""Divide Function"""
19+
ify==0:
20+
raiseValueError('Can not divide by zero!')
21+
returnx/y

‎Python-Unit-Testing/employee.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
importrequests
3+
4+
5+
classEmployee:
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+
defemail(self):
17+
return'{}.{}@email.com'.format(self.first,self.last)
18+
19+
@property
20+
deffullname(self):
21+
return'{} {}'.format(self.first,self.last)
22+
23+
defapply_raise(self):
24+
self.pay=int(self.pay*self.raise_amt)
25+
26+
defmonthly_schedule(self,month):
27+
response=requests.get(f'http://company.com/{self.last}/{month}')
28+
ifresponse.ok:
29+
returnresponse.text
30+
else:
31+
return'Bad Response!'

‎Python-Unit-Testing/snippets.txt

Lines changed: 115 additions & 0 deletions
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, 'Corey.Schafer@email.com')
12+
self.assertEqual(emp_2.email, 'Sue.Smith@email.com')
13+
14+
emp_1.first = 'John'
15+
emp_2.first = 'Jane'
16+
17+
self.assertEqual(emp_1.email, 'John.Schafer@email.com')
18+
self.assertEqual(emp_2.email, 'Jane.Smith@email.com')
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, 'Corey.Schafer@email.com')
66+
self.assertEqual(self.emp_2.email, 'Sue.Smith@email.com')
67+
68+
self.emp_1.first = 'John'
69+
self.emp_2.first = 'Jane'
70+
71+
self.assertEqual(self.emp_1.email, 'John.Schafer@email.com')
72+
self.assertEqual(self.emp_2.email, 'Jane.Smith@email.com')
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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
importunittest
2+
importcalc
3+
4+
5+
classTestCalc(unittest.TestCase):
6+
7+
deftest_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+
deftest_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+
deftest_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+
deftest_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+
withself.assertRaises(ValueError):
29+
calc.divide(10,0)
30+
31+
32+
if__name__=='__main__':
33+
unittest.main()

‎Python-Unit-Testing/test_employee.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
importunittest
2+
fromunittest.mockimportpatch
3+
fromemployeeimportEmployee
4+
5+
6+
classTestEmployee(unittest.TestCase):
7+
8+
@classmethod
9+
defsetUpClass(cls):
10+
print('setupClass')
11+
12+
@classmethod
13+
deftearDownClass(cls):
14+
print('teardownClass')
15+
16+
defsetUp(self):
17+
print('setUp')
18+
self.emp_1=Employee('Corey','Schafer',50000)
19+
self.emp_2=Employee('Sue','Smith',60000)
20+
21+
deftearDown(self):
22+
print('tearDown\n')
23+
24+
deftest_email(self):
25+
print('test_email')
26+
self.assertEqual(self.emp_1.email,'Corey.Schafer@email.com')
27+
self.assertEqual(self.emp_2.email,'Sue.Smith@email.com')
28+
29+
self.emp_1.first='John'
30+
self.emp_2.first='Jane'
31+
32+
self.assertEqual(self.emp_1.email,'John.Schafer@email.com')
33+
self.assertEqual(self.emp_2.email,'Jane.Smith@email.com')
34+
35+
deftest_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+
deftest_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+
deftest_monthly_schedule(self):
55+
withpatch('employee.requests.get')asmocked_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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
''' Super simple module to create basic random data for tutorials'''
2+
importrandom
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+
fornuminrange(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

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp