Skip to content

REF/TST: Add more pytest idiom to test_read_fwf.py #24361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 1 addition & 59 deletions pandas/tests/io/parser/test_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,15 @@
of the parsers defined in parsers.py
"""

import bz2
import gzip
import os
import zipfile

import pytest

import pandas.compat as compat

import pandas as pd
import pandas.util.testing as tm


def lzma_file():
"""
Try to load the `LZMAFile` class from `backports.lzma`.

Returns
-------
klass : type or None
"""
try:
lzma = compat.import_lzma()
except ImportError:
lzma = None

return getattr(lzma, "LZMAFile", None)


def write_to_compressed(compress_type, path, data, dest="test"):
"""
Write data to a compressed file.

Parameters
----------
compress_type : type
The compression type (or class) to use.
path : str
The file path to write the data.
data : str
The data to write.
dest : str, default "test"
The destination file (for ZIP only)
"""
# compression --> compression_method
compression_mappings = {
"zip": zipfile.ZipFile,
"gzip": gzip.GzipFile,
"bz2": bz2.BZ2File,
"xz": lzma_file(),
}

compress_method = compression_mappings[compress_type]

if compress_type == "zip":
mode = "w"
args = (dest, data)
method = "writestr"
else:
mode = "wb"
args = (data,)
method = "write"

with compress_method(path, mode=mode) as f:
getattr(f, method)(*args)


@pytest.fixture(params=[True, False])
def buffer(request):
return request.param
Expand Down Expand Up @@ -154,7 +96,7 @@ def test_compression(parser_and_data, compression_only, buffer, filename):
"buffer of compressed data.")

with tm.ensure_clean(filename=filename) as path:
write_to_compressed(compress_type, path, data)
tm.write_to_compressed(compress_type, path, data)
compression = "infer" if filename else compress_type

if buffer:
Expand Down
Loading