Skip to content

Commit b34a288

Browse files
authored
Merge branch 'canbula:master' into master
2 parents 62b73a7 + 5ae1705 commit b34a288

File tree

7 files changed

+431
-0
lines changed

7 files changed

+431
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
3+
4+
name: Week03 Homework
5+
6+
on:
7+
push:
8+
branches: ["master"]
9+
paths: ["Week03/**"]
10+
pull_request:
11+
branches: ["master"]
12+
paths: ["Week03/**"]
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- uses: actions/checkout@v3
23+
- name: Set up Python 3.12
24+
uses: actions/setup-python@v3
25+
with:
26+
python-version: "3.12"
27+
- name: Install dependencies
28+
run: |
29+
python -m pip install --upgrade pip
30+
pip install flake8 pytest
31+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
32+
- name: Lint with flake8
33+
run: |
34+
# stop the build if there are Python syntax errors or undefined names
35+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
36+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
37+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
38+
- name: Test with pytest
39+
run: |
40+
pytest -q --tb=no 'Week03/test_pyramid.py'
41+
pytest -q --tb=no 'Week03/test_sequences.py'
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
3+
4+
name: Week04 Homework
5+
6+
on:
7+
push:
8+
branches: [ "master" ]
9+
paths: ['Week04/**']
10+
pull_request:
11+
branches: [ "master" ]
12+
paths: ['Week04/**']
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
build:
19+
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- uses: actions/checkout@v3
24+
- name: Set up Python 3.12
25+
uses: actions/setup-python@v3
26+
with:
27+
python-version: "3.12"
28+
- name: Install dependencies
29+
run: |
30+
python -m pip install --upgrade pip
31+
pip install flake8 pytest
32+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
33+
- name: Lint with flake8
34+
run: |
35+
# stop the build if there are Python syntax errors or undefined names
36+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
37+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
38+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
39+
- name: Test with pytest
40+
run: |
41+
pytest -q --tb=no 'Week04/test_functions.py'
42+
pytest -q --tb=no 'Week04/test_decorators.py'

LectureNotes.pdf

3.02 MB
Binary file not shown.

Week03/test_pyramid.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
3+
4+
files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("pyramid")]
5+
for f in files:
6+
exec("import " + f[:-3] + " as " + f[:-3])
7+
print(f"The module {f[:-3]} has been imported.")
8+
9+
10+
def test_names():
11+
for f in files:
12+
assert "calculate_pyramid_height" in dir(eval(f[:-3])), (
13+
"calculate_pyramid_height is not defined in " + f[:-3]
14+
)
15+
16+
17+
def test_types():
18+
for f in files:
19+
assert callable(eval(f[:-3]).calculate_pyramid_height), (
20+
"calculate_pyramid_height is not callable in " + f[:-3]
21+
)
22+
assert isinstance(eval(f[:-3]).calculate_pyramid_height(1), int), (
23+
"calculate_pyramid_height is not returning an int in " + f[:-3]
24+
)
25+
26+
27+
def test_calculate_pyramid_height():
28+
for f in files:
29+
assert eval(f[:-3]).calculate_pyramid_height(1) == 1, (
30+
"calculate_pyramid_height is not working in " + f[:-3]
31+
)
32+
assert eval(f[:-3]).calculate_pyramid_height(2) == 1, (
33+
"calculate_pyramid_height is not working in " + f[:-3]
34+
)
35+
assert eval(f[:-3]).calculate_pyramid_height(6) == 3, (
36+
"calculate_pyramid_height is not working in " + f[:-3]
37+
)
38+
assert eval(f[:-3]).calculate_pyramid_height(20) == 5, (
39+
"calculate_pyramid_height is not working in " + f[:-3]
40+
)
41+
assert eval(f[:-3]).calculate_pyramid_height(100) == 13, (
42+
"calculate_pyramid_height is not working in " + f[:-3]
43+
)
44+
assert eval(f[:-3]).calculate_pyramid_height(1000) == 44, (
45+
"calculate_pyramid_height is not working in " + f[:-3]
46+
)
47+
assert eval(f[:-3]).calculate_pyramid_height(10000) == 140, (
48+
"calculate_pyramid_height is not working in " + f[:-3]
49+
)
50+
assert eval(f[:-3]).calculate_pyramid_height(100000) == 446, (
51+
"calculate_pyramid_height is not working in " + f[:-3]
52+
)

Week03/test_sequences.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import os
2+
3+
4+
files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("sequences")]
5+
for f in files:
6+
exec("import " + f[:-3] + " as " + f[:-3])
7+
print(f"The module {f[:-3]} has been imported.")
8+
9+
10+
def test_names():
11+
for f in files:
12+
assert "remove_duplicates" in dir(eval(f[:-3])), (
13+
"remove_duplicates is not defined in " + f[:-3]
14+
)
15+
assert "list_counts" in dir(eval(f[:-3])), (
16+
"list_counts is not defined in " + f[:-3]
17+
)
18+
assert "reverse_dict" in dir(eval(f[:-3])), (
19+
"reverse_dict is not defined in " + f[:-3]
20+
)
21+
22+
23+
def test_types():
24+
for f in files:
25+
assert callable(eval(f[:-3]).remove_duplicates), (
26+
"remove_duplicates is not callable in " + f[:-3]
27+
)
28+
assert callable(eval(f[:-3]).list_counts), (
29+
"list_counts is not callable in " + f[:-3]
30+
)
31+
assert callable(eval(f[:-3]).reverse_dict), (
32+
"reverse_dict is not callable in " + f[:-3]
33+
)
34+
assert isinstance(eval(f[:-3]).remove_duplicates([1, 2, 3, 4, 5, 6]), list), (
35+
"remove_duplicates is not returning a list in " + f[:-3]
36+
)
37+
assert isinstance(eval(f[:-3]).list_counts([1, 2, 3, 4, 5, 6]), dict), (
38+
"list_counts is not returning a dict in " + f[:-3]
39+
)
40+
assert isinstance(eval(f[:-3]).reverse_dict({1: 1, 2: 2, 3: 3}), dict), (
41+
"reverse_dict is not returning a dict in " + f[:-3]
42+
)
43+
44+
45+
def test_remove_duplicates():
46+
for f in files:
47+
assert eval(f[:-3]).remove_duplicates([1, 2, 3, 3, 4, 5, 5, 5, 6]) == [
48+
1,
49+
2,
50+
3,
51+
4,
52+
5,
53+
6,
54+
], (
55+
"remove_duplicates is not working in " + f[:-3]
56+
)
57+
assert eval(f[:-3]).remove_duplicates([1, 2, 3, 4, 5, 6]) == [
58+
1,
59+
2,
60+
3,
61+
4,
62+
5,
63+
6,
64+
], (
65+
"remove_duplicates is not working in " + f[:-3]
66+
)
67+
assert eval(f[:-3]).remove_duplicates([1, 1, 1, 1, 1, 1]) == [1], (
68+
"remove_duplicates is not working in " + f[:-3]
69+
)
70+
assert eval(f[:-3]).remove_duplicates([]) == [], (
71+
"remove_duplicates is not working in " + f[:-3]
72+
)
73+
74+
75+
def test_list_counts():
76+
for f in files:
77+
assert eval(f[:-3]).list_counts([1, 2, 3, 3, 4, 5, 5, 5, 6]) == {
78+
1: 1,
79+
2: 1,
80+
3: 2,
81+
4: 1,
82+
5: 3,
83+
6: 1,
84+
}, (
85+
"list_counts is not working in " + f[:-3]
86+
)
87+
assert eval(f[:-3]).list_counts([1, 2, 3, 4, 5, 6]) == {
88+
1: 1,
89+
2: 1,
90+
3: 1,
91+
4: 1,
92+
5: 1,
93+
6: 1,
94+
}, (
95+
"list_counts is not working in " + f[:-3]
96+
)
97+
assert eval(f[:-3]).list_counts([1, 1, 1, 1, 1, 1]) == {1: 6}, (
98+
"list_counts is not working in " + f[:-3]
99+
)
100+
assert eval(f[:-3]).list_counts([]) == {}, (
101+
"list_counts is not working in " + f[:-3]
102+
)
103+
104+
105+
def test_reverse_dict():
106+
for f in files:
107+
assert eval(f[:-3]).reverse_dict({1: 1, 2: 2, 3: 3}) == {1: 1, 2: 2, 3: 3}, (
108+
"reverse_dict is not working in " + f[:-3]
109+
)
110+
assert eval(f[:-3]).reverse_dict({1: 2, 2: 3, 3: 4}) == {2: 1, 3: 2, 4: 3}, (
111+
"reverse_dict is not working in " + f[:-3]
112+
)
113+
assert eval(f[:-3]).reverse_dict({1: 1, 2: 1, 3: 1}) == {1: 3}, (
114+
"reverse_dict is not working in " + f[:-3]
115+
)
116+
assert eval(f[:-3]).reverse_dict({}) == {}, (
117+
"reverse_dict is not working in " + f[:-3]
118+
)

Week04/test_decorators.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import inspect
3+
import time
4+
import random
5+
6+
7+
files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("decorators")]
8+
for f in files:
9+
exec("import " + f[:-3] + " as " + f[:-3])
10+
11+
12+
def test_names():
13+
for f in files:
14+
assert "performance" in dir(eval(f[:-3])), "timer is not defined in " + f[:-3]
15+
16+
def test_callables():
17+
for f in files:
18+
assert callable(eval(f[:-3]).performance), "timer is not callable in " + f[:-3]
19+
20+
def test_performance():
21+
for f in files:
22+
@eval(f[:-3]).performance
23+
def dummy_timer(x):
24+
time.sleep(x)
25+
@eval(f[:-3]).performance
26+
def dummy_memory(x):
27+
return [random.randint(0, 100) for _ in range(x)]
28+
dummy_timer(1)
29+
assert eval(f[:-3]).performance.counter == 1, \
30+
"performance is not working in " + f[:-3] + " (counter)"
31+
assert eval(f[:-3]).performance.total_time > 1, \
32+
"performance is not working in " + f[:-3] + " (total_time)"
33+
dummy_timer(1)
34+
dummy_timer(2)
35+
dummy_timer(3)
36+
assert eval(f[:-3]).performance.counter == 4, \
37+
"performance is not working in " + f[:-3] + " (counter)"
38+
assert eval(f[:-3]).performance.total_time > 7, \
39+
"performance is not working in " + f[:-3] + " (total_time)"
40+
dummy_memory(1000000)
41+
assert eval(f[:-3]).performance.counter == 5, \
42+
"performance is not working in " + f[:-3] + " (counter)"
43+
assert eval(f[:-3]).performance.total_mem > 8.e6, \
44+
"performance is not working in " + f[:-3] + " (total_mem)"

0 commit comments

Comments
 (0)