8000 Fix tests of input-output exercises (#108) · marcusberr/python-tutorial@af22df4 · GitHub
[go: up one dir, main page]

Skip to content

Commit af22df4

Browse files
yakutovichaedoardob90baffelli
authored
Fix tests of input-output exercises (empa-scientific-it#108)
Co-authored-by: edoardob90 <edoardob90@gmail.com> Co-authored-by: Simone Baffelli <simone.baffelli@gmail.com>
1 parent 86b6571 commit af22df4

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

input_output.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@
12171217
"tags": []
12181218
},
12191219
"source": [
1220-
"### Exercise 4: Translating words 🌶️🌶️"
1220+
"### Exercise 5: Binary format 🌶️🌶️🌶️"
12211221
]
12221222
},
12231223
{

tutorial/tests/test_input_output.py

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,70 @@
1-
import pytest
1+
import contextlib
2+
import csv
3+
import itertools
24
import pathlib as pl
3-
import csv
4-
import itertools
5-
from .testsuite import get_module_name
6-
from .. import prepare_magic_file
75
import sys
8-
96
from io import StringIO
10-
import contextlib
117

12-
def get_data(name: str, data_dir:str="data") -> pl.Path:
13-
current_module = sys.modules[__name__]
8+
import pytest
9+
10+
from .. import prepare_magic_file
11+
12+
13+
def get_data(name: str, data_dir: str = "data") -> pl.Path:
14+
current_module = sys.modules[__name__]
1415
return (pl.Path(current_module.__file__).parent / f"{data_dir}/{name}").resolve()
1516

17+
1618
def reference_solution_find_all_files(f: pl.Path) -> "list[pl.Path]":
17-
return list(f.parent.glob(f.name))
19+
return list(f.parent.iterdir())
20+
1821

1922
def test_find_all_files(function_to_test):
2023
f = pl.Path("data/")
2124
assert function_to_test(f) == reference_solution_find_all_files(f)
2225

26+
2327
def reference_solution_count_parents(f: pl.Path) -> int:
2428
return len([ob for ob in f.parent.glob("*") if ob.is_dir()])
2529

30+
2631
def test_count_parents(function_to_test):
27-
f = pl.Path(sys.modules[__name__].__file__).parent.parent
28-
print(f)
32+
f = pl.Path(sys.modules[__name__].__file__).parent.parent
2933
assert function_to_test(f) == reference_solution_count_parents(f)
3034

35+
3136
def reference_solution_read_file(f: pl.Path) -> "list[str]":
3237
with open(f) as lines:
3338
return [l for l in lines.readlines()]
3439

40+
3541
def test_read_file(function_to_test):
3642
for fp in ["lines.txt", "example.csv"]:
3743
data = get_data(fp)
3844
assert function_to_test(data) == reference_solution_read_file(data)
3945

46+
4047
def reference_solution_write_file(f: pl.Path, lines: "list[str]") -> None:
4148
with open(f, "w") as f:
4249
f.writelines(lines)
4350

51+
4452
def test_write_file(function_to_test):
4553
lines = ["python tutorial 2023"]
4654
f = pl.Path("test.txt")
4755
function_to_test(f)
4856
with open(f) as input_file:
4957
assert input_file.readlines() == lines
5058

59+
5160
def reference_solution_exercise1(f: pl.Path) -> "dict[str, list[int]]":
5261
with open(f) as lines:
5362
reader = csv.reader(lines)
5463
headers = next(reader)
55-
transposed = {k:list(v) for k,v in zip(headers, itertools.zip_longest(*(l for l in reader)))}
64+
transposed = {
65+
k: list(v)
66+
for k, v in zip(headers, itertools.zip_longest(*(l for l in reader)))
67+
}
5668
return transposed
5769

5870

@@ -61,9 +73,10 @@ def reference_solution_print_odd(n: int) -> None:
6173
if i % 2 != 0:
6274
print(i)
6375

76+
6477
@pytest.mark.parametrize("n", [1, 2, 3, 4, 5])
65-
def test_print_odd(function_to_test, n:int):
66-
#redirect stdout of function_to_test to a string
78+
def test_print_odd(function_to_test, n: int):
79+
# redirect stdout of function_to_test to a string
6780
solution_stdout = StringIO()
6881
with contextlib.redirect_stdout(solution_stdout):
6982
function_to_test(n)
@@ -72,28 +85,14 @@ def test_print_odd(function_to_test, n:int):
7285
reference_solution_print_odd(n)
7386
assert reference_stdout.getvalue() == solution_stdout.getvalue()
7487

75-
76-
def reference_solution_print_salutation() -> None:
77-
name = input()
78-
print(f"Hello {name}")
79-
80-
def test_print_salutation(function_to_test):
81-
#redirect stdout of function_to_test to a string
82-
solution_stdout = StringIO()
83-
solution_stdin = StringIO()
84-
with contextlib.redirect_stdout(solution_stdout), contextlib.redirect_stdin(solution_stdin):
85-
function_to_test()
86-
reference_stdout = StringIO()
87-
reference_stdin = StringIO(solution_stdin.getvalue())
88-
with contextlib.redirect_stdout(reference_stdout), contextlib.redirect_stdin(reference_stdin):
89-
reference_solution_print_salutation()
90-
assert reference_stdout.getvalue() == solution_stdout.getvalue()
9188

92-
93-
def reference_solution_read_write_file(input_file: pl.Path, output_file: pl.Path) -> None:
94-
with open(input_file) as read_file, open(output_file, "w") as write_file:
89+
def reference_solution_read_write_file(
90+
input_file: pl.Path, output_file: pl.Path
91+
) -> None:
92+
with open(input_file) as read_file, open(output_file, "w") as write_file:
9593
for line in read_file.readlines():
96-
write_file.write(f"{line}, {len(line)}")
94+
write_file.write(f"{line.strip(["\n","\r"])}, {len(line)}\n")
95+
9796

9897
def test_read_write_file(function_to_test):
9998
input_file = get_data("lines.txt")
@@ -104,14 +103,18 @@ def test_read_write_file(function_to_test):
104103
with open(output_file) as output_file, open(test_output_file) as tes:
105104
assert output_file.readlines() == tes.readlines()
106105

106+
107107
def test_exercise1(function_to_test):
108108
f = get_data("example.csv")
109109
assert function_to_test(f) == reference_solution_exercise1(f)
110110

111111

112-
def reference_solution_exercise2(f: pl.Path)-> int:
112+
def reference_solution_exercise2(f: pl.Path) -> int:
113113
with open(f) as lines:
114-
return len(list(itertools.chain.from_iterable([l.split() for l in lines.readlines()])))
114+
return len(
115+
list(itertools.chain.from_iterable([l.split() for l in lines.readlines()]))
116+
)
117+
115118

116119
def test_exercise2(function_to_test):
117120
f = get_data("lines.txt")
@@ -120,28 +123,45 @@ def test_exercise2(function_to_test):
120123

121124
def reference_solution_exercise3(f: pl.Path) -> "dict[str, int]":
122125
with open(f) as lines:
123-
res = {k: len(list(v)) for k,v in itertools.groupby(sorted([l for l in itertools.chain(*itertools.chain(lines.readlines())) if l.isalpha()]))}
126+
res = {
127+
k: len(list(v))
128+
for k, v in itertools.groupby(
129+
sorted(
130+
[
131+
l
132+
for l in itertools.chain(*itertools.chain(lines.readlines()))
133+
if l.isalpha()
134+
]
135+
)
136+
)
137+
}
124138
return res
125139

140+
126141
def test_exercise3(function_to_test):
127142
f = get_data("lines.txt")
128143
assert function_to_test(f) == reference_solution_exercise3(f)
129144

130145

131-
def reference_solution_exercise4(english: pl.Path, dictionary: pl.Path) -> "list[(str, str)]":
146+
def reference_solution_exercise4(
147+
english: pl.Path, dictionary: pl.Path
148+
) -> "list[(str, str)]":
132149
with open(english) as english_file:
133150
english_reader = csv.reader(english_file)
134-
english_words = [w for w, *rest in english_reader]
151+
english_words = [w for w, _ in english_reader]
135152
with open(dictionary) as dict_file:
136153
dict_reader = csv.reader(dict_file)
137154
next(dict_reader)
138-
translations = {en:it for index,it, en,*rest in dict_reader}
155+
translations = {en: it for _, it, en, _ in dict_reader}
139156
return [(e, translations[e]) for e in english_words if e in translations.keys()]
140157

158+
141159
def test_exercise4(function_to_test):
142160
words = get_data("english.csv")
143161
dictionary = get_data("dict.csv")
144-
assert function_to_test(words, dictionary) == reference_solution_exercise4(words, dictionary)
162+
assert function_to_test(words, dictionary) == reference_solution_exercise4(
163+
words, dictionary
164+
)
145165

146166

147167
def reference_solution_exercise5(secret_file: pl.Path) -> str:
@@ -151,4 +171,3 @@ def reference_solution_exercise5(secret_file: pl.Path) -> str:
151171
def test_exercise5(function_to_test):
152172
message = get_data("secret_message.dat")
153173
assert function_to_test(message) == reference_solution_exercise5(message)
154-

0 commit comments

Comments
 (0)
0