Skip to content

Commit 8b7cb6d

Browse files
Apply suggestions from AA-Turner
Co-authored-by: Adam Turner <[email protected]>
1 parent 0c438ac commit 8b7cb6d

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

Lib/compression/zstd/__init__.py

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@
33
"""
44

55
__all__ = (
6-
# From this file
6+
# compression.zstd
77
"compressionLevel_values",
8-
"get_frame_info",
8+
"compress",
99
"CParameter",
10+
"decompress",
1011
"DParameter",
11-
"Strategy",
1212
"finalize_dict",
13+
"get_frame_info",
14+
"Strategy",
1315
"train_dict",
1416
"zstd_support_multithread",
15-
"compress",
16-
"decompress",
17-
# From _zstd
17+
18+
# compression.zstd.zstdfile
19+
"open",
20+
"ZstdFile",
21+
22+
# _zstd
23+
"get_frame_size",
24+
"zstd_version",
25+
"zstd_version_info",
1826
"ZstdCompressor",
1927
"ZstdDecompressor",
2028
"ZstdDict",
2129
"ZstdError",
22-
"get_frame_size",
23-
"zstd_version",
24-
"zstd_version_info",
25-
# From zstd.zstdfile
26-
"open",
27-
"ZstdFile",
2830
)
2931

3032
import enum
@@ -43,43 +45,55 @@
4345
_finalize_dict = _zstd._finalize_dict
4446

4547

46-
@dataclasses.dataclass(frozen=True)
47-
class _CompressionLevelValues:
48-
default: int
49-
min: int
50-
max: int
48+
class _CLValues:
49+
__slots__ = 'default', 'min', 'max'
50+
51+
def __init__(self, default, min, max):
52+
super().__setattr__('default', default)
53+
super().__setattr__('min', min)
54+
super().__setattr__('max', max)
55+
56+
def __repr__(self):
57+
return (f'compression_level_values(default={self.default}, '
58+
f'min={self.min}, max={self.max})')
59+
60+
def __setattr__(self, name, _):
61+
raise AttributeError(f"can't set attribute {name!r}")
5162

5263
compressionLevel_values = _CompressionLevelValues(*_zstd._compressionLevel_values)
5364

54-
@dataclasses.dataclass(frozen=True)
5565
class FrameInfo:
56-
"""A dataclass storing information about a Zstandard frame."""
57-
decompressed_size: int
58-
dictionary_id: int
66+
"""Information about a Zstandard frame."""
67+
__slots__ = 'decompressed_size', 'dictionary_id'
68+
69+
def __init__(self, decompressed_size, dictionary_id):
70+
super().__setattr__('decompressed_size', decompressed_size)
71+
super().__setattr__('dictionary_id', dictionary_id)
72+
73+
def __repr__(self):
74+
return (f'FrameInfo(decompressed_size={self.decompressed_size}, '
75+
f'dictionary_id={self.dictionary_id})')
76+
77+
def __setattr__(self, name, _):
78+
raise AttributeError(f"can't set attribute {name!r}")
5979

6080

6181
def get_frame_info(frame_buffer):
6282
"""Get zstd frame information from a frame header.
6383
64-
Parameter
65-
frame_buffer: A bytes-like object. It should starts from the beginning of
66-
a frame, and needs to include at least the frame header (6 to
67-
18 bytes).
84+
*frame_buffer* is a bytes-like object. It should starts from the beginning of
85+
a frame, and needs to include at least the frame header (6 to 18 bytes).
6886
6987
Return a FrameInfo dataclass, which currently has two attributes
7088
7189
'decompressed_size' is the size in bytes of the data in the frame when
72-
decompressed.
73-
74-
If decompressed_size is None, decompressed size is unknown.
90+
decompressed, or None when the decompressed size is unknown.
7591
7692
'dictionary_id' is a 32-bit unsigned integer value. 0 means dictionary ID was
7793
not recorded in the frame header, the frame may or may not need a dictionary
7894
to be decoded, and the ID of such a dictionary is not specified.
7995
"""
80-
81-
ret_tuple = _zstd._get_frame_info(frame_buffer)
82-
return FrameInfo(*ret_tuple)
96+
return FrameInfo(*_zstd._get_frame_info(frame_buffer))
8397

8498

8599
def _nbytes(dat):
@@ -92,14 +106,13 @@ def _nbytes(dat):
92106
def train_dict(samples, dict_size):
93107
"""Train a zstd dictionary, return a ZstdDict object.
94108
95-
Parameters
96-
samples: An iterable of samples, a sample is a bytes-like object
97-
represents a file.
98-
dict_size: The dictionary's maximum size, in bytes.
109+
*samples* is an iterable of samples, where a sample is a bytes-like
110+
object representing a file.
111+
*dict_size* is the dictionary's maximum size, in bytes.
99112
"""
100-
# Check argument's type
101113
if not isinstance(dict_size, int):
102-
raise TypeError('dict_size argument should be an int object.')
114+
ds_cls = type(dict_size).__qualname__
115+
raise TypeError('dict_size must be an int object, not {ds_cls!r}.')
103116

104117
# Prepare data
105118
chunks = []
@@ -169,11 +182,10 @@ def finalize_dict(zstd_dict, samples, dict_size, level):
169182
dict_content = _finalize_dict(zstd_dict.dict_content,
170183
chunks, chunk_sizes,
171184
dict_size, level)
172-
173-
return _zstd.ZstdDict(dict_content)
185+
return ZstdDict(dict_content)
174186

175187
def compress(data, level=None, options=None, zstd_dict=None):
176-
"""Compress a block of data, return a bytes object of zstd compressed data.
188+
"""Return Zstandard compressed *data* as bytes.
177189
178190
Refer to ZstdCompressor's docstring for a description of the
179191
optional arguments *level*, *options*, and *zstd_dict*.

0 commit comments

Comments
 (0)