Skip to content

Commit 0912329

Browse files
committed
Replaced lzma skip_if funcs
1 parent 7a0ee19 commit 0912329

File tree

7 files changed

+43
-53
lines changed

7 files changed

+43
-53
lines changed

pandas/tests/frame/test_to_csv.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
ensure_clean,
2222
makeCustomDataframe as mkdf)
2323
import pandas.util.testing as tm
24+
import pandas.util._test_decorators as td
2425

2526
from pandas.tests.frame.common import TestData
2627

@@ -965,10 +966,10 @@ def test_to_csv_compression_bz2(self):
965966
for col in df.columns:
966967
assert col in text
967968

969+
@td.skip_if_no_lzma
968970
def test_to_csv_compression_xz(self):
969971
# GH11852
970972
# use the compression kw in to_csv
971-
tm._skip_if_no_lzma()
972973
df = DataFrame([[0.123456, 0.234567, 0.567567],
973974
[12.32112, 123123.2, 321321.2]],
974975
index=['A', 'B'], columns=['X', 'Y', 'Z'])

pandas/tests/io/json/test_compression.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import pandas as pd
44
from pandas import compat
55
import pandas.util.testing as tm
6+
import pandas.util._test_decorators as td
67
from pandas.util.testing import assert_frame_equal, assert_raises_regex
78

89

9-
COMPRESSION_TYPES = [None, 'bz2', 'gzip', 'xz']
10+
COMPRESSION_TYPES = [None, 'bz2', 'gzip',
11+
pytest.param('xz', marks=td.skip_if_no_lzma)]
1012

1113

1214
def decompress_file(path, compression):
@@ -32,9 +34,6 @@ def decompress_file(path, compression):
3234

3335
@pytest.mark.parametrize('compression', COMPRESSION_TYPES)
3436
def test_compression_roundtrip(compression):
35-
if compression == 'xz':
36-
tm._skip_if_no_lzma()
37-
3837
df = pd.DataFrame([[0.123456, 0.234567, 0.567567],
3938
[12.32112, 123123.2, 321321.2]],
4039
index=['A', 'B'], columns=['X', 'Y', 'Z'])
@@ -74,9 +73,6 @@ def test_with_s3_url(compression):
7473
pytest.importorskip('s3fs')
7574
moto = pytest.importorskip('moto')
7675

77-
if compression == 'xz':
78-
tm._skip_if_no_lzma()
79-
8076
df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}')
8177
with moto.mock_s3():
8278
conn = boto3.resource("s3", region_name="us-east-1")
@@ -94,9 +90,6 @@ def test_with_s3_url(compression):
9490

9591
@pytest.mark.parametrize('compression', COMPRESSION_TYPES)
9692
def test_lines_with_compression(compression):
97-
if compression == 'xz':
98-
tm._skip_if_no_lzma()
99-
10093
with tm.ensure_clean() as path:
10194
df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}')
10295
df.to_json(path, orient='records', lines=True, compression=compression)
@@ -107,9 +100,6 @@ def test_lines_with_compression(compression):
107100

108101
@pytest.mark.parametrize('compression', COMPRESSION_TYPES)
109102
def test_chunksize_with_compression(compression):
110-
if compression == 'xz':
111-
tm._skip_if_no_lzma()
112-
113103
with tm.ensure_clean() as path:
114104
df = pd.read_json('{"a": ["foo", "bar", "baz"], "b": [4, 5, 6]}')
115105
df.to_json(path, orient='records', lines=True, compression=compression)

pandas/tests/io/parser/compression.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import pytest
99

1010
import pandas as pd
11+
import pandas.compat as compat
1112
import pandas.util.testing as tm
13+
import pandas.util._test_decorators as td
1214

1315

1416
class CompressionTests(object):
@@ -117,8 +119,9 @@ def test_bz2(self):
117119
result = self.read_csv(path, compression='infer')
118120
tm.assert_frame_equal(result, expected)
119121

122+
@td.skip_if_no_lzma
120123
def test_xz(self):
121-
lzma = tm._skip_if_no_lzma()
124+
lzma = compat.import_lzma()
122125

123126
with open(self.csv1, 'rb') as data_file:
124127
data = data_file.read()

pandas/tests/io/parser/test_network.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
import pytest
88

99
import pandas.util.testing as tm
10+
import pandas.util._test_decorators as td
1011
from pandas import DataFrame
1112
from pandas.io.parsers import read_csv, read_table
1213
from pandas.compat import BytesIO
1314

1415

1516
@pytest.mark.network
1617
@pytest.mark.parametrize(
17-
"compression,extension",
18-
[('gzip', '.gz'), ('bz2', '.bz2'), ('zip', '.zip'),
19-
pytest.param('xz', '.xz',
20-
marks=pytest.mark.skipif(not tm._check_if_lzma(),
21-
reason='need backports.lzma '
22-
'to run'))])
18+
"compression,extension", [
19+
('gzip', '.gz'), ('bz2', '.bz2'), ('zip', '.zip'),
20+
pytest.param('xz', '.xz', marks=td.skip_if_no_lzma)
21+
]
22+
)
2323
@pytest.mark.parametrize('mode', ['explicit', 'infer'])
2424
@pytest.mark.parametrize('engine', ['python', 'c'])
2525
def test_compressed_urls(salaries_table, compression, extension, mode, engine):

pandas/tests/io/test_pickle.py

+17-19
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pandas.compat import is_platform_little_endian
2424
import pandas
2525
import pandas.util.testing as tm
26+
import pandas.util._test_decorators as td
2627
from pandas.tseries.offsets import Day, MonthEnd
2728
import shutil
2829
import sys
@@ -382,12 +383,11 @@ def decompress_file(self, src_path, dest_path, compression):
382383
fh.write(f.read())
383384
f.close()
384385

385-
@pytest.mark.parametrize('compression', [None, 'gzip', 'bz2', 'xz'])
386+
@pytest.mark.parametrize('compression', [
387+
None, 'gzip', 'bz2',
388+
pytest.param('xz', marks=td.skip_if_no_lzma) # issue 11666
389+
])
386390
def test_write_explicit(self, compression, get_random_path):
387-
# issue 11666
388-
if compression == 'xz':
389-
tm._skip_if_no_lzma()
390-
391391
base = get_random_path
392392
path1 = base + ".compressed"
393393
path2 = base + ".raw"
@@ -414,11 +414,11 @@ def test_write_explicit_bad(self, compression, get_random_path):
414414
df = tm.makeDataFrame()
415415
df.to_pickle(path, compression=compression)
416416

417-
@pytest.mark.parametrize('ext', ['', '.gz', '.bz2', '.xz', '.no_compress'])
417+
@pytest.mark.parametrize('ext', [
418+
'', '.gz', '.bz2', '.no_compress',
419+
pytest.param('.xz', marks=td.skip_if_no_lzma)
420+
])
418421
def test_write_infer(self, ext, get_random_path):
419-
if ext == '.xz':
420-
tm._skip_if_no_lzma()
421-
422422
base = get_random_path
423423
path1 = base + ext
424424
path2 = base + ".raw"
@@ -442,12 +442,11 @@ def test_write_infer(self, ext, get_random_path):
442442

443443
tm.assert_frame_equal(df, df2)
444444

445-
@pytest.mark.parametrize('compression', [None, 'gzip', 'bz2', 'xz', "zip"])
445+
@pytest.mark.parametrize('compression', [
446+
None, 'gzip', 'bz2', "zip",
447+
pytest.param('xz', marks=td.skip_if_no_lzma)
448+
])
446449
def test_read_explicit(self, compression, get_random_path):
447-
# issue 11666
448-
if compression == 'xz':
449-
tm._skip_if_no_lzma()
450-
451450
base = get_random_path
452451
path1 = base + ".raw"
453452
path2 = base + ".compressed"
@@ -466,12 +465,11 @@ def test_read_explicit(self, compression, get_random_path):
466465

467466
tm.assert_frame_equal(df, df2)
468467

469-
@pytest.mark.parametrize('ext', ['', '.gz', '.bz2', '.xz', '.zip',
470-
'.no_compress'])
468+
@pytest.mark.parametrize('ext', [
469+
'', '.gz', '.bz2', '.zip', '.no_compress',
470+
pytest.param('.xz', marks=td.skip_if_no_lzma)
471+
])
471472
def test_read_infer(self, ext, get_random_path):
472-
if ext == '.xz':
473-
tm._skip_if_no_lzma()
474-
475473
base = get_random_path
476474
path1 = base + ".raw"
477475
path2 = base + ext

pandas/util/_test_decorators.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def test_foo():
2828
import locale
2929
from distutils.version import LooseVersion
3030

31-
from pandas.compat import is_platform_windows, is_platform_32bit, PY3
31+
from pandas.compat import (is_platform_windows, is_platform_32bit, PY3,
32+
import_lzma)
3233

3334

3435
def safe_import(mod_name, min_version=None):
@@ -99,6 +100,13 @@ def _skip_if_no_scipy():
99100
safe_import('scipy.interpolate'))
100101

101102

103+
def _skip_if_no_lzma():
104+
try:
105+
import_lzma()
106+
except ImportError as e:
107+
return True
108+
109+
102110
def skip_if_no(package, min_version=None):
103111
"""
104112
Generic function to help skip test functions when required packages are not
@@ -153,3 +161,5 @@ def decorated_func(func):
153161
lang=locale.getlocale()[0]))
154162
skip_if_no_scipy = pytest.mark.skipif(_skip_if_no_scipy(),
155163
reason="Missing SciPy requirement")
164+
skip_if_no_lzma = pytest.mark.skipif(_skip_if_no_lzma(),
165+
reason="need backports.lzma to run")

pandas/util/testing.py

-12
Original file line numberDiff line numberDiff line change
@@ -336,18 +336,6 @@ def _skip_if_no_scipy():
336336
pytest.importorskip("scipy.interpolate")
337337

338338

339-
def _check_if_lzma():
340-
try:
341-
return compat.import_lzma()
342-
except ImportError:
343-
return False
344-
345-
346-
def _skip_if_no_lzma():
347-
import pytest
348-
return _check_if_lzma() or pytest.skip('need backports.lzma to run')
349-
350-
351339
def skip_if_no_ne(engine='numexpr'):
352340
from pandas.core.computation.expressions import (
353341
_USE_NUMEXPR,

0 commit comments

Comments
 (0)