Return a ZstdDict representing a trained Zstandard dictionary. *samples* is an iterable of samples, where a sample is a bytes-like object representing a file. *dict_size* is the dictionary's maximum size, in bytes.
(samples, dict_size)
| 76 | |
| 77 | |
| 78 | def train_dict(samples, dict_size): |
| 79 | """Return a ZstdDict representing a trained Zstandard dictionary. |
| 80 | |
| 81 | *samples* is an iterable of samples, where a sample is a bytes-like |
| 82 | object representing a file. |
| 83 | |
| 84 | *dict_size* is the dictionary's maximum size, in bytes. |
| 85 | """ |
| 86 | if not isinstance(dict_size, int): |
| 87 | ds_cls = type(dict_size).__qualname__ |
| 88 | raise TypeError(f'dict_size must be an int object, not {ds_cls!r}.') |
| 89 | |
| 90 | samples = tuple(samples) |
| 91 | chunks = b''.join(samples) |
| 92 | chunk_sizes = tuple(_nbytes(sample) for sample in samples) |
| 93 | if not chunks: |
| 94 | raise ValueError("samples contained no data; can't train dictionary.") |
| 95 | dict_content = _zstd.train_dict(chunks, chunk_sizes, dict_size) |
| 96 | return ZstdDict(dict_content) |
| 97 | |
| 98 | |
| 99 | def finalize_dict(zstd_dict, /, samples, dict_size, level): |
searching dependent graphs…