MCPcopy Index your code
hub / github.com/python/cpython / finalize_dict

Function finalize_dict

Lib/compression/zstd/__init__.py:99–133  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

97
98
99def 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
136def compress(data, level=None, options=None, zstd_dict=None):

Callers 2

test_finalize_dictMethod · 0.90

Calls 2

_nbytesFunction · 0.90
joinMethod · 0.45

Tested by 2

test_finalize_dictMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…