Skip to content
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

Buffer compression writers #78

Merged
merged 4 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 15 additions & 4 deletions src/xopen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,21 @@ def xopen(
detected_format = _detect_format_from_content(filename)

if detected_format == "gz":
return _open_gz(filename, mode, compresslevel, threads)
opened_file = _open_gz(filename, mode, compresslevel, threads)
elif detected_format == "xz":
return _open_xz(filename, mode)
opened_file = _open_xz(filename, mode)
elif detected_format == "bz2":
return _open_bz2(filename, mode, threads)
opened_file = _open_bz2(filename, mode, threads)
else:
return open(filename, mode)
opened_file = open(filename, mode)

# The "write" method for GzipFile is very costly. Lots of python calls are
# made. To a lesser extent this is true for LzmaFile and BZ2File. By
# putting a buffer in between, the expensive write method is called much
# less. The effect is very noticeable when writing small units such as
# lines or FASTQ records.
if (isinstance(opened_file, gzip.GzipFile) or
isinstance(opened_file, bz2.BZ2File) or
isinstance(opened_file, lzma.LZMAFile)) and "w" in mode:
rhpvorderman marked this conversation as resolved.
Show resolved Hide resolved
opened_file = io.BufferedWriter(opened_file) # type: ignore
return opened_file
6 changes: 4 additions & 2 deletions tests/test_xopen.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,14 +532,16 @@ def test_write_no_threads(tmpdir, ext):
klass = klasses[ext]
path = str(tmpdir.join(f"out.{ext}"))
with xopen(path, "wb", threads=0) as f:
assert isinstance(f, klass), f
assert isinstance(f, io.BufferedWriter)
if ext:
assert isinstance(f.raw, klass), f


def test_write_gzip_no_threads_no_isal(tmpdir, xopen_without_igzip):
import gzip
path = str(tmpdir.join("out.gz"))
with xopen_without_igzip(path, "wb", threads=0) as f:
assert isinstance(f, gzip.GzipFile), f
assert isinstance(f.raw, gzip.GzipFile), f


def test_write_stdout():
Expand Down