Skip to content

Commit

Permalink
add support for zst writing
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenkov committed Mar 21, 2024
1 parent 9fa61a1 commit 4402af8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions smart_open/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def _handle_gzip(file_obj, mode):


def _handle_zstd(file_obj, mode):
import zstandard as zstd
result = zstd.ZstdDecompressor().stream_reader(file_obj, closefd=True)
import zstandard # type: ignore
result = zstandard.open(filename=file_obj, mode=mode)
return result


Expand Down
13 changes: 13 additions & 0 deletions smart_open/tests/test_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#
import gzip
import io
import tempfile

import pytest
import zstandard as zstd
Expand Down Expand Up @@ -42,3 +43,15 @@ def label(thing, name):
def test_compression_wrapper_read(fileobj, compression, filename):
wrapped = smart_open.compression.compression_wrapper(fileobj, 'rb', compression, filename)
assert wrapped.read() == plain


def test_zst_write():
with tempfile.NamedTemporaryFile(suffix=".zst") as tmp:
with smart_open.open(tmp.name, "wt") as fout:
print("hello world", file=fout)
print("this is a test", file=fout)

with smart_open.open(tmp.name, "rt") as fin:
got = list(fin)

assert got == ["hello world\n", "this is a test\n"]

0 comments on commit 4402af8

Please sign in to comment.