Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-132983: Add documentation for compression.zstd#133911

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gpshead merged 23 commits intopython:mainfromemmatyping:zstd-docs
May 21, 2025
Merged
Changes from1 commit
Commits
Show all changes
23 commits
Select commitHold shift + click to select a range
49d9c30
Add documentation for compression.zstd
emmatypingMay 12, 2025
62ef4dc
Add examples
emmatypingMay 12, 2025
0b154b1
Fix camelcase name references
emmatypingMay 12, 2025
63f963f
CParameter->CompressionParameter
emmatypingMay 12, 2025
cfe0590
Apply suggestions from AA-Turner
emmatypingMay 12, 2025
5115b4c
Apply suggestions from reviewers
emmatypingMay 12, 2025
4ab7fd7
Apply suggestions from reviewers
emmatypingMay 12, 2025
5eb5efc
Apply suggestions from reviewers
emmatypingMay 14, 2025
987bd27
Don't reference self when referring to items
emmatypingMay 14, 2025
615ed7f
Updates to respond to review
emmatypingMay 14, 2025
0f7bc05
Remove outdated paragraph
emmatypingMay 15, 2025
44173f3
Remove Zstandard dictionary after ZstdDict
emmatypingMay 16, 2025
8bd5500
Rewrite introduction to compression package to be more timeless
emmatypingMay 18, 2025
24f3761
Remove content_size_flag
emmatypingMay 18, 2025
d04ce4f
Merge branch 'zstd-docs' of github.com:emmatyping/cpython into zstd-docs
emmatypingMay 18, 2025
e61e9a1
Apply suggestions from Sumana and Stan
emmatypingMay 19, 2025
1149832
Remove ref to Meta and clean up mode usage
emmatypingMay 20, 2025
71ed7c3
Apply suggestions from vadmium
emmatypingMay 20, 2025
2f895dd
Many updates to respond to review
emmatypingMay 20, 2025
f25e6e7
Add examples to (De)compressionParameter
emmatypingMay 20, 2025
9ff6320
Add reference to zstd manual and blurb on algorithm
emmatypingMay 20, 2025
daa9df1
Expand on the connection between level and compression_level
emmatypingMay 21, 2025
b3fd3cd
Resolve review suggestions
emmatypingMay 21, 2025
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
PrevPrevious commit
NextNext commit
Updates to respond to review
- Rewrite the digested/undigested dict section- Add a section for exceptions- wrap lines to ~80 chars- clarify that an exception is raised if level is passed to a decompressor- make quotes in docs for open vs ZstdFile consistent- Remove currently and repeated "note"
  • Loading branch information
@emmatyping
emmatyping committedMay 14, 2025
commit615ed7f7e0a9e32745ba183a19024b167a0fa9ae
147 changes: 70 additions & 77 deletionsDoc/library/compression.zstd.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,9 +13,9 @@

This module provides classes and functions for compressing and
decompressing data using the Zstandard (or *zstd*) compression algorithm. Also
included is a file interface that supports reading and writing the contents of ``.zst`` files.
files created by the :program:`zstd` utility, as well as raw zstd compressed
streams.
included is a file interface that supports reading and writing the contents of
``.zst``files created by the :program:`zstd` utility, as well as raw zstd
compressedstreams.

The :mod:`!compression.zstd` module contains:

Expand All@@ -31,6 +31,9 @@ The :mod:`!compression.zstd` module contains:
:class:`Strategy` classes for setting advanced (de)compression parameters.


Exceptions
----------

.. exception:: ZstdError

This exception is raised when an error occurs during compression or
Expand All@@ -52,16 +55,17 @@ Reading and writing compressed files
to read from or write to.

The mode argument can be either ``'r'`` for reading (default), ``'w'`` for
overwriting, ``'a'`` for appending, or ``'x'`` for exclusive creation. These can
equivalently be given as ``'rb'``, ``'wb'``, ``'ab'``, and ``'xb'`` respectively. You may
also open in text mode with ``'rt'``, ``'wt'``, ``'at'``, and ``'xt'`` respectively.
overwriting, ``'a'`` for appending, or ``'x'`` for exclusive creation. These
can equivalently be given as ``'rb'``, ``'wb'``, ``'ab'``, and ``'xb'``
respectively. You may also open in text mode with ``'rt'``, ``'wt'``,
``'at'``, and ``'xt'`` respectively.

When opening a file for reading, the *options* argument can be a dictionary
providing advanced decompression parameters; see
:class:`DecompressionParameter` for detailed information about supported
parameters. The *zstd_dict* argument is a :class:`ZstdDict` instance to be
used during decompression. When opening a file for reading, the *level*
argumentshould notbeused.
used during decompression. When opening a file for reading,ifthe *level*
argumentis passed a :exc:`!TypeError` willberaised.

When opening a file for writing, the *options* argument can be a dictionary
providing advanced decompression parameters; see
Expand All@@ -76,11 +80,12 @@ Reading and writing compressed files
*encoding*, *errors*, and *newline* parameters must not be provided.

In text mode, a :class:`ZstdFile` object is created, and wrapped in an
:class:`io.TextIOWrapper` instance with the specified encoding, error handling
behavior, and line endings.
:class:`io.TextIOWrapper` instance with the specified encoding, error
handlingbehavior, and line endings.


.. class:: ZstdFile(file, /, mode='r', *, level=None, options=None, zstd_dict=None)
.. class:: ZstdFile(file, /, mode='r', *, level=None, options=None, \
zstd_dict=None)

Open a Zstandard-compressed file in binary mode.

Expand All@@ -91,20 +96,20 @@ Reading and writing compressed files
wrapping an existing file object, the wrapped file will not be closed when
the :class:`ZstdFile` is closed.

The *mode* argument can be either ``"r"`` for reading (default), ``"w"`` for
overwriting, ``"x"`` for exclusive creation, or ``"a"`` for appending. These
can equivalently be given as ``"rb"``, ``"wb"``, ``"xb"`` and ``"ab"``
The *mode* argument can be either ``'r'`` for reading (default), ``'w'`` for
overwriting, ``'x'`` for exclusive creation, or ``'a'`` for appending. These
can equivalently be given as ``'rb'``, ``'wb'``, ``'xb'`` and ``'ab'``
respectively.

If *file* is a file object (rather than an actual file name), a mode of
``"w"`` does not truncate the file, and is instead equivalent to ``"a"``.
``'w'`` does not truncate the file, and is instead equivalent to ``'a'``.

When opening a file for reading, the *options* argument can be a dictionary
providing advanced decompression parameters, see
:class:`DecompressionParameter` for detailed information about supported
parameters. The *zstd_dict* argument is a :class:`!ZstdDict` instance to be
used during decompression. When opening a file for reading, the *level*
argumentshould notbeused.
used during decompression. When opening a file for reading,ifthe *level*
argumentis passed a :exc:`!TypeError` willberaised.

When opening a file for writing, the *options* argument can be a dictionary
providing advanced decompression parameters, see
Expand DownExpand Up@@ -132,8 +137,8 @@ Reading and writing compressed files

.. note:: While calling :meth:`peek` does not change the file position of
the :class:`ZstdFile`, it may change the position of the underlying
file object (for example, if the :class:`ZstdFile` was constructed by passing a
file object for *filename*).
file object (for example, if the :class:`ZstdFile` was constructed by
passing afile object for *file*).

.. attribute:: mode

Expand DownExpand Up@@ -307,7 +312,7 @@ Compressing and decompressing data in memory

Data found after the end of the compressed stream.

Before the end of the stream is reached, this will be ``b""``.
Before the end of the stream is reached, this will be ``b''``.

.. attribute:: needs_input

Expand DownExpand Up@@ -377,6 +382,40 @@ Zstandard dictionaries
is an ordinary Zstandard dictionary, created from Zstandard functions,
for example, :func:`train_dict` or the ``zstd`` CLI.

When passing a :class:`!ZstdDict` to a function, the
:attr:`!as_digested_dict` and :attr:`!as_undigested_dict` attributes can
control how the dictionary is loaded by passing them as the ``zstd_dict``
argument, for example, ``compress(data, zstd_dict=zd.as_digested_dict)``.
Digesting a dictionary is a costly operation that occurs when loading a
Zstandard dictionary. When making multiple calls to compression or
decompression, passing a digested dictionary will reduce the overhead of
loading the dictionary.

.. list-table:: Difference for compression

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think that simple nested item lists would look better here. Maybe not even nested item lists, just an item lists with two or three paragraphs in every item.

Copy link
MemberAuthor

@emmatypingemmatypingMay 21, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Sorry, I'm not sure I understand what you have in mind, perhaps it can be resolved in a follow-up?

gpshead reacted with thumbs up emoji
:widths: 10 14 10
:header-rows: 1

* -
- Digested dictionary
- Undigested dictionary
* - Advanced parameters of the compressor which may be overridden by
the dictionary's parameters
- ``window_log``, ``hash_log``, ``chain_log``, ``search_log``,
``min_match``, ``target_length``, ``strategy``,
``enable_long_distance_matching``, ``ldm_hash_log``,
``ldm_min_match``, ``ldm_bucket_size_log``, ``ldm_hash_rate_log``,
and some non-public parameters.
- None
* - :class:`!ZstdDict` internally caches the dictionary
- Yes. It's faster when loading a digested dictionary again with the
same compression level.
- No. If you wish to load an undigested dictionary multiple times,
consider reusing a compressor object.

If passing a :class:`!ZstdDict` without any attribute, an undigested
dictionary is passed by default when compressing and a digested dictionary
is passed by default when decompressing.

.. attribute:: dict_content

The content of the Zstandard dictionary, a ``bytes`` object. It's the
Expand DownExpand Up@@ -407,53 +446,6 @@ Zstandard dictionaries

Load as an undigested dictionary.

Digesting a dictionary is a costly operation. These two attributes can
control how the dictionary is loaded to the compressor, by passing them
as the ``zstd_dict`` argument, for example,
``compress(data, zstd_dict=zd.as_digested_dict)``.

If don't use one of these attributes, an **undigested** dictionary is
passed by default.

.. list-table:: Difference for compression
:widths: 12 12 12
:header-rows: 1

* -
- | Digested
| dictionary
- | Undigested
| dictionary
* - | Some advanced
| parameters of the
| compressor may
| be overridden
| by dictionary's
| parameters
- | ``window_log``, ``hash_log``,
| ``chain_log``, ``search_log``,
| ``min_match``, ``target_length``,
| ``strategy``,
| ``enable_long_distance_matching``,
| ``ldm_hash_log``, ``ldm_min_match``,
| ``ldm_bucket_size_log``,
| ``ldm_hash_rate_log``, and some
| non-public parameters.
- No
* - | ZstdDict internally
| caches the dictionary
- | Yes. It's faster when
| loading a digested
| dictionary again with the same
| compression level.
- | No. If you wish to load an undigested
| dictionary multiple times,
| consider reusing a
| compressor object.

A **digested** dictionary is used for decompression by default, which
is faster when loaded multiple times.


Advanced parameter control
--------------------------
Expand DownExpand Up@@ -482,14 +474,14 @@ Advanced parameter control
.. attribute:: compression_level

A high-level means of setting other compression parameters that affect
the speed and ratio of compressing data. Setting the level to zero uses the
default :attr:`COMPRESSION_LEVEL_DEFAULT`.
the speed and ratio of compressing data. Setting the level to zero uses
thedefault :attr:`COMPRESSION_LEVEL_DEFAULT`.

.. attribute:: window_log

Maximum allowed back-reference distance the compressor can use when
compressing data, expressed as power of two, ``1 << window_log`` bytes. This
parameter greatly influences the memory usage of compression. Higher
compressing data, expressed as power of two, ``1 << window_log`` bytes.
Thisparameter greatly influences the memory usage of compression. Higher
values require more memory but gain better compression values.

.. attribute:: hash_log
Expand DownExpand Up@@ -519,9 +511,9 @@ Advanced parameter control
Minimum size of searched matches. Larger values increase compression and
decompression speed, but decrease ratio. Note that Zstandard can still
find matches of smaller size, it just tweaks its search algorithm to look
for this size and larger.Note that currently, forall strategies
< :attr:`~Strategy.btopt`,the effective minimum is ``4``, for all
strategies> :attr:`~Strategy.fast`, the effective maximum is ``6``.
for this size and larger.Forall strategies < :attr:`~Strategy.btopt`,
the effective minimum is ``4``, for all strategies
> :attr:`~Strategy.fast`, the effective maximum is ``6``.

.. attribute:: target_length

Expand DownExpand Up@@ -599,7 +591,7 @@ Advanced parameter control

Select how many threads will be spawned to compress in parallel. When
:attr:`!nb_workers` >= 1, enables multi-threaded compression, 1
means "1-thread multi-threaded mode". More workers improve speed, but
means "one-thread multi-threaded mode". More workers improve speed, but
also increase memory usage and slightly reduce compression ratio.

.. attribute:: job_size
Expand DownExpand Up@@ -655,8 +647,9 @@ Advanced parameter control

.. note::

The values of attributes of :class:`Strategy` are not necessarily stable
between zstd versions. Only the ordering may be relied upon.
The values of attributes of :class:`!Strategy` are not necessarily stable
across zstd versions. Only the ordering of the attributes may be relied
upon.

The following strategies are available:

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp