1
- import pytest
1
+ import contextlib
2
+ import csv
3
+ import itertools
2
4
import pathlib as pl
3
- import csv
4
- import itertools
5
- from .testsuite import get_module_name
6
- from .. import prepare_magic_file
7
5
import sys
8
-
9
6
from io import StringIO
10
- import contextlib
11
7
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__ ]
14
15
return (pl .Path (current_module .__file__ ).parent / f"{ data_dir } /{ name } " ).resolve ()
15
16
17
+
16
18
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
+
18
21
19
22
def test_find_all_files (function_to_test ):
20
23
f = pl .Path ("data/" )
21
24
assert function_to_test (f ) == reference_solution_find_all_files (f )
22
25
26
+
23
27
def reference_solution_count_parents (f : pl .Path ) -> int :
24
28
return len ([ob for ob in f .parent .glob ("*" ) if ob .is_dir ()])
25
29
30
+
26
31
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
29
33
assert function_to_test (f ) == reference_solution_count_parents (f )
30
34
35
+
31
36
def reference_solution_read_file (f : pl .Path ) -> "list[str]" :
32
37
with open (f ) as lines :
33
38
return [l for l in lines .readlines ()]
34
39
40
+
35
41
def test_read_file (function_to_test ):
36
42
for fp in ["lines.txt" , "example.csv" ]:
37
43
data = get_data (fp )
38
44
assert function_to_test (data ) == reference_solution_read_file (data )
39
45
46
+
40
47
def reference_solution_write_file (f : pl .Path , lines : "list[str]" ) -> None :
41
48
with open (f , "w" ) as f :
42
49
f .writelines (lines )
43
50
51
+
44
52
def test_write_file (function_to_test ):
45
53
lines = ["python tutorial 2023" ]
46
54
f = pl .Path ("test.txt" )
47
55
function_to_test (f )
48
56
with open (f ) as input_file :
49
57
assert input_file .readlines () == lines
50
58
59
+
51
60
def reference_solution_exercise1 (f : pl .Path ) -> "dict[str, list[int]]" :
52
61
with open (f ) as lines :
53
62
reader = csv .reader (lines )
54
63
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
+ }
56
68
return transposed
57
69
58
70
@@ -61,9 +73,10 @@ def reference_solution_print_odd(n: int) -> None:
61
73
if i % 2 != 0 :
62
74
print (i )
63
75
76
+
64
77
@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
67
80
solution_stdout = StringIO ()
68
81
with contextlib .redirect_stdout (solution_stdout ):
69
82
function_to_test (n )
@@ -72,28 +85,14 @@ def test_print_odd(function_to_test, n:int):
72
85
reference_solution_print_odd (n )
73
86
assert reference_stdout .getvalue () == solution_stdout .getvalue ()
74
87
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 ()
91
88
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 :
95
93
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
+
97
96
98
97
def test_read_write_file (function_to_test ):
99
98
input_file = get_data ("lines.txt" )
@@ -104,14 +103,18 @@ def test_read_write_file(function_to_test):
104
103
with open (output_file ) as output_file , open (test_output_file ) as tes :
105
104
assert output_file .readlines () == tes .readlines ()
106
105
106
+
107
107
def test_exercise1 (function_to_test ):
108
108
f = get_data ("example.csv" )
109
109
assert function_to_test (f ) == reference_solution_exercise1 (f )
110
110
111
111
112
- def reference_solution_exercise2 (f : pl .Path )-> int :
112
+ def reference_solution_exercise2 (f : pl .Path ) -> int :
113
113
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
+
115
118
116
119
def test_exercise2 (function_to_test ):
117
120
f = get_data ("lines.txt" )
@@ -120,28 +123,45 @@ def test_exercise2(function_to_test):
120
123
121
124
def reference_solution_exercise3 (f : pl .Path ) -> "dict[str, int]" :
122
125
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
+ }
124
138
return res
125
139
140
+
126
141
def test_exercise3 (function_to_test ):
127
142
f = get_data ("lines.txt" )
128
143
assert function_to_test (f ) == reference_solution_exercise3 (f )
129
144
130
145
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)]" :
132
149
with open (english ) as english_file :
133
150
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 ]
135
152
with open (dictionary ) as dict_file :
136
153
dict_reader = csv .reader (dict_file )
137
154
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 }
139
156
return [(e , translations [e ]) for e in english_words if e in translations .keys ()]
140
157
158
+
141
159
def test_exercise4 (function_to_test ):
142
160
words = get_data ("english.csv" )
143
161
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
+ )
145
165
146
166
147
167
def reference_solution_exercise5 (secret_file : pl .Path ) -> str :
@@ -151,4 +171,3 @@ def reference_solution_exercise5(secret_file: pl.Path) -> str:
151
171
def test_exercise5 (function_to_test ):
152
172
message = get_data ("secret_message.dat" )
153
173
assert function_to_test (message ) == reference_solution_exercise5 (message )
154
-
0 commit comments