Skip to content

Commit 51fafa2

Browse files
committed
Simplify file path usage
1 parent aac221c commit 51fafa2

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

test/test_xarray_io.py

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def mem_check_function(
7979
)
8080

8181

82-
def create_and_write_jinja(tmp_path, data: xr.DataArray):
82+
def create_and_write_jinja(file_path, data: xr.DataArray):
8383
env = jinja2.Environment(
8484
loader=jinja2.PackageLoader("flopy4.xarray_jinja"),
8585
trim_blocks=True,
@@ -92,36 +92,35 @@ def create_and_write_jinja(tmp_path, data: xr.DataArray):
9292
data=data
9393
)
9494
with np.printoptions(precision=4, linewidth=sys.maxsize):
95-
with open(tmp_path / "test_xarray_to_text_jinja.disu", "w") as f:
95+
with open(file_path, "w") as f:
9696
f.writelines(generator)
9797

9898

99-
@pytest.mark.parametrize(
100-
"max_size,chunks",
101-
test_combinations,
102-
)
99+
@pytest.mark.parametrize("max_size,chunks", test_combinations)
103100
@pytest.mark.skip("Too slow for large data")
104101
@pytest.mark.timing
105102
def test_xarray_to_text_jinja(tmp_path, max_size, chunks, time_file):
106103
data = xr.DataArray(da.arange(0, max_size, 1), dims="x")
107104
data = data.chunk(chunks)
105+
file_path = tmp_path / "test_xarray_to_text_jinja.disu"
106+
108107
profile_function(
109108
create_and_write_jinja,
110-
(tmp_path, data),
109+
(file_path, data),
111110
time_file,
112111
print_args={"max_size": max_size, "chunks": chunks},
113112
)
114113

115-
with open(tmp_path / "test_xarray_to_text_jinja.disu", "r") as f:
114+
with open(file_path, "r") as f:
116115
output = f.readlines()
117116
assert (
118117
len(output) == 2 + max_size / chunks
119118
) # begin + end + lines of data
120119

121120

122-
def create_and_write_pandas(tmp_path, data: xr.DataArray):
121+
def create_and_write_pandas(file_path, data: xr.DataArray):
123122
pandas_data = data.to_pandas()
124-
with open(tmp_path / "test_xarray_to_text_extras.disu", "w") as f:
123+
with open(file_path, "w") as f:
125124
f.write("BEGIN GRIDDATA\n")
126125
pandas_data.to_csv(
127126
f,
@@ -133,58 +132,55 @@ def create_and_write_pandas(tmp_path, data: xr.DataArray):
133132
f.write("\nEND GRIDDATA\n")
134133

135134

136-
@pytest.mark.parametrize(
137-
"max_size,chunks",
138-
test_combinations,
139-
)
135+
@pytest.mark.parametrize("max_size,chunks", test_combinations)
140136
@pytest.mark.timing
141137
def test_xarray_to_text_pandas(tmp_path, max_size, chunks, time_file):
142138
data = xr.DataArray(da.arange(0, max_size, 1), dims="x")
143139
data = data.chunk(chunks)
140+
file_path = tmp_path / "test_xarray_to_text_extras.disu"
141+
144142
profile_function(
145143
create_and_write_pandas,
146-
(tmp_path, data),
144+
(file_path, data),
147145
time_file,
148146
print_args={"max_size": max_size, "chunks": chunks},
149147
)
150148

151-
with open(tmp_path / "test_xarray_to_text_extras.disu", "r") as f:
149+
with open(file_path, "r") as f:
152150
output = f.readlines()
153151
assert len(output) == 3 # begin + end + 1 line of data
154152

155153

156-
def create_and_write_np_savetxt(tmp_path, data: xr.DataArray):
157-
with open(tmp_path / "test_xarray_to_text_raw.disu", "w") as f:
154+
def create_and_write_np_savetxt(file_path, data: xr.DataArray):
155+
with open(file_path, "w") as f:
158156
f.write("BEGIN GRIDDATA\n")
159157
for block in data.data.to_delayed():
160158
block_data = block.compute()
161159
np.savetxt(f, block_data, newline=" ", fmt="%.4f")
162160
f.write("\nEND GRIDDATA\n")
163161

164162

165-
@pytest.mark.parametrize(
166-
"max_size,chunks",
167-
test_combinations,
168-
)
163+
@pytest.mark.parametrize("max_size,chunks", test_combinations)
169164
@pytest.mark.skip("Too slow for large data")
170165
@pytest.mark.timing
171166
def test_xarray_to_text_np_savetxt(tmp_path, max_size, chunks, time_file):
172167
data = xr.DataArray(da.arange(0, max_size, 1), dims="x")
173168
data = data.chunk(chunks)
169+
file_path = tmp_path / "test_xarray_to_text_raw.disu"
170+
174171
profile_function(
175172
create_and_write_np_savetxt,
176-
(tmp_path, data),
173+
(file_path, data),
177174
time_file,
178175
print_args={"max_size": max_size, "chunks": chunks},
179176
)
180177

181-
with open(tmp_path / "test_xarray_to_text_raw.disu", "r") as f:
178+
with open(file_path, "r") as f:
182179
output = f.readlines()
183180
assert len(output) == 3
184181

185182

186-
def create_and_write_extras(tmp_path, data: xr.DataArray):
187-
file_path = tmp_path / "test_xarray_to_text_extras.disu"
183+
def create_and_write_extras(file_path, data: xr.DataArray):
188184
with open(file_path, "w") as f:
189185
f.write("BEGIN GRIDDATA\n")
190186
promise = xarray_extras.csv.to_csv(
@@ -204,41 +200,39 @@ def create_and_write_extras(tmp_path, data: xr.DataArray):
204200
f.write("\nEND GRIDDATA\n")
205201

206202

207-
@pytest.mark.parametrize(
208-
"max_size,chunks",
209-
test_combinations,
210-
)
203+
@pytest.mark.parametrize("max_size,chunks", test_combinations)
211204
@pytest.mark.timing
212205
def test_xarray_to_text_extras(tmp_path, max_size, chunks, time_file):
213206
data = xr.DataArray(da.arange(0, max_size, 1), dims="x")
214207
data = data.chunk(chunks)
208+
file_path = tmp_path / "test_xarray_to_text_extras.disu"
209+
215210
profile_function(
216211
create_and_write_extras,
217-
(tmp_path, data),
212+
(file_path, data),
218213
time_file,
219214
print_args={"max_size": max_size, "chunks": chunks},
220215
)
221216

222-
with open(tmp_path / "test_xarray_to_text_extras.disu", "r") as f:
217+
with open(file_path, "r") as f:
223218
output = f.readlines()
224219
assert len(output) == 3
225220

226221

227-
@pytest.mark.parametrize(
228-
"max_size,chunks",
229-
test_combinations,
230-
)
222+
@pytest.mark.parametrize("max_size,chunks", test_combinations)
231223
@pytest.mark.memory
232224
def test_xarray_to_text_extras_mem(tmp_path, max_size, chunks, memory_file):
233225
data = xr.DataArray(da.arange(0, max_size, 1), dims="x")
234226
data = data.chunk(chunks)
227+
file_path = tmp_path / "test_xarray_to_text_extras.disu"
228+
235229
mem_check_function(
236230
create_and_write_extras,
237-
(tmp_path, data),
231+
(file_path, data),
238232
memory_file,
239233
print_args={"max_size": max_size, "chunks": chunks},
240234
)
241235

242-
with open(tmp_path / "test_xarray_to_text_extras.disu", "r") as f:
236+
with open(file_path, "r") as f:
243237
output = f.readlines()
244238
assert len(output) == 3

0 commit comments

Comments
 (0)