Skip to content

Commit 62ef4dc

Browse files
committed
Add examples
1 parent 49d9c30 commit 62ef4dc

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Doc/library/compression.zstd.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,11 +704,65 @@ Miscellaneous
704704
recorded in the frame header, the frame may or may not need a dictionary
705705
to be decoded, or the ID of such a dictionary is not specified.
706706

707+
707708
.. attribute:: COMPRESSION_LEVEL_DEFAULT
708709

709710
The default compression level for Zstandard, currently '3'.
710711

712+
711713
.. attribute:: zstd_version_info
712714

713715
Version number of the runtime zstd library as a tuple of int
714716
(major, minor, release).
717+
718+
719+
Examples
720+
--------
721+
722+
Reading in a compressed file::
723+
724+
from compression import zstd
725+
with zstd.open("file.zst") as f:
726+
file_content = f.read()
727+
728+
Creating a compressed file::
729+
730+
from compression import zstd
731+
data = b"Insert Data Here"
732+
with zstd.open("file.zst", "w") as f:
733+
f.write(data)
734+
735+
Compressing data in memory::
736+
737+
from compression import zstd
738+
data_in = b"Insert Data Here"
739+
data_out = zstd.compress(data_in)
740+
741+
Incremental compression::
742+
743+
from compression import zstd
744+
comp = zstd.ZstdCompressor()
745+
out1 = comp.compress(b"Some data\n")
746+
out2 = comp.compress(b"Another piece of data\n")
747+
out3 = comp.compress(b"Even more data\n")
748+
out4 = comp.flush()
749+
# Concatenate all the partial results:
750+
result = b"".join([out1, out2, out3, out4])
751+
752+
Writing compressed data to an already-open file::
753+
754+
from compression import zstd
755+
with open("file.zst", "wb") as f:
756+
f.write(b"This data will not be compressed\n")
757+
with zstd.open(f, "w") as zstf:
758+
zstf.write(b"This *will* be compressed\n")
759+
f.write(b"Not compressed\n")
760+
761+
Creating a compressed file using compression parameters::
762+
763+
from compression import zstd
764+
options = {
765+
zstd.CompressionParameter.checksum_flag: 1
766+
}
767+
with zstd.open("file.zst", "w", options=options) as f:
768+
f.write(b"blah blah blah")

0 commit comments

Comments
 (0)