Skip to content

Commit 0d2eb42

Browse files
gfyoungTomAugspurger
authored andcommitted
REF/TST: Add more pytest idiom to test_read_fwf.py (pandas-dev#24361)
Also refactors out a compression writing utility function originally just for test_compression in the same directory.
1 parent 03c5a7e commit 0d2eb42

File tree

3 files changed

+449
-335
lines changed

3 files changed

+449
-335
lines changed

pandas/tests/io/parser/test_compression.py

+1-59
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,15 @@
55
of the parsers defined in parsers.py
66
"""
77

8-
import bz2
9-
import gzip
108
import os
119
import zipfile
1210

1311
import pytest
1412

15-
import pandas.compat as compat
16-
1713
import pandas as pd
1814
import pandas.util.testing as tm
1915

2016

21-
def lzma_file():
22-
"""
23-
Try to load the `LZMAFile` class from `backports.lzma`.
24-
25-
Returns
26-
-------
27-
klass : type or None
28-
"""
29-
try:
30-
lzma = compat.import_lzma()
31-
except ImportError:
32-
lzma = None
33-
34-
return getattr(lzma, "LZMAFile", None)
35-
36-
37-
def write_to_compressed(compress_type, path, data, dest="test"):
38-
"""
39-
Write data to a compressed file.
40-
41-
Parameters
42-
----------
43-
compress_type : type
44-
The compression type (or class) to use.
45-
path : str
46-
The file path to write the data.
47-
data : str
48-
The data to write.
49-
dest : str, default "test"
50-
The destination file (for ZIP only)
51-
"""
52-
# compression --> compression_method
53-
compression_mappings = {
54-
"zip": zipfile.ZipFile,
55-
"gzip": gzip.GzipFile,
56-
"bz2": bz2.BZ2File,
57-
"xz": lzma_file(),
58-
}
59-
60-
compress_method = compression_mappings[compress_type]
61-
62-
if compress_type == "zip":
63-
mode = "w"
64-
args = (dest, data)
65-
method = "writestr"
66-
else:
67-
mode = "wb"
68-
args = (data,)
69-
method = "write"
70-
71-
with compress_method(path, mode=mode) as f:
72-
getattr(f, method)(*args)
73-
74-
7517
@pytest.fixture(params=[True, False])
7618
def buffer(request):
7719
return request.param
@@ -154,7 +96,7 @@ def test_compression(parser_and_data, compression_only, buffer, filename):
15496
"buffer of compressed data.")
15597

15698
with tm.ensure_clean(filename=filename) as path:
157-
write_to_compressed(compress_type, path, data)
99+
tm.write_to_compressed(compress_type, path, data)
158100
compression = "infer" if filename else compress_type
159101

160102
if buffer:

0 commit comments

Comments
 (0)