Return a ZstdDict representing a finalized Zstandard dictionary. Given a custom content as a basis for dictionary, and a set of samples, finalize *zstd_dict* by adding headers and statistics according to the Zstandard dictionary format. You may compose an effective dictionary conte
(zstd_dict, /, samples, dict_size, level)
| 97 | |
| 98 | |
| 99 | def finalize_dict(zstd_dict, /, samples, dict_size, level): |
| 100 | """Return a ZstdDict representing a finalized Zstandard dictionary. |
| 101 | |
| 102 | Given a custom content as a basis for dictionary, and a set of samples, |
| 103 | finalize *zstd_dict* by adding headers and statistics according to the |
| 104 | Zstandard dictionary format. |
| 105 | |
| 106 | You may compose an effective dictionary content by hand, which is used as |
| 107 | basis dictionary, and use some samples to finalize a dictionary. The basis |
| 108 | dictionary may be a "raw content" dictionary. See *is_raw* in ZstdDict. |
| 109 | |
| 110 | *samples* is an iterable of samples, where a sample is a bytes-like object |
| 111 | representing a file. |
| 112 | *dict_size* is the dictionary's maximum size, in bytes. |
| 113 | *level* is the expected compression level. The statistics for each |
| 114 | compression level differ, so tuning the dictionary to the compression level |
| 115 | can provide improvements. |
| 116 | """ |
| 117 | |
| 118 | if not isinstance(zstd_dict, ZstdDict): |
| 119 | raise TypeError('zstd_dict argument should be a ZstdDict object.') |
| 120 | if not isinstance(dict_size, int): |
| 121 | raise TypeError('dict_size argument should be an int object.') |
| 122 | if not isinstance(level, int): |
| 123 | raise TypeError('level argument should be an int object.') |
| 124 | |
| 125 | samples = tuple(samples) |
| 126 | chunks = b''.join(samples) |
| 127 | chunk_sizes = tuple(_nbytes(sample) for sample in samples) |
| 128 | if not chunks: |
| 129 | raise ValueError("The samples are empty content, can't finalize the " |
| 130 | "dictionary.") |
| 131 | dict_content = _zstd.finalize_dict(zstd_dict.dict_content, chunks, |
| 132 | chunk_sizes, dict_size, level) |
| 133 | return ZstdDict(dict_content) |
| 134 | |
| 135 | |
| 136 | def compress(data, level=None, options=None, zstd_dict=None): |
searching dependent graphs…