Skip to content
32 changes: 17 additions & 15 deletions Lib/compression/zstd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@

class FrameInfo:
"""Information about a Zstandard frame."""
__slots__ = 'decompressed_size', 'dictionary_id'

__slots__ = "decompressed_size", "dictionary_id"

def __init__(self, decompressed_size, dictionary_id):
super().__setattr__('decompressed_size', decompressed_size)
super().__setattr__('dictionary_id', dictionary_id)
super().__setattr__("decompressed_size", decompressed_size)
super().__setattr__("dictionary_id", dictionary_id)

def __repr__(self):
return (f'FrameInfo(decompressed_size={self.decompressed_size}, '
f'dictionary_id={self.dictionary_id})')
return (f"FrameInfo(decompressed_size={self.decompressed_size}, "
f"dictionary_id={self.dictionary_id})")

def __setattr__(self, name, _):
raise AttributeError(f"can't set attribute {name!r}")
Expand Down Expand Up @@ -78,10 +79,10 @@ def train_dict(samples, dict_size):
"""
if not isinstance(dict_size, int):
ds_cls = type(dict_size).__qualname__
raise TypeError(f'dict_size must be an int object, not {ds_cls!r}.')
raise TypeError(f"dict_size must be an int object, not {ds_cls!r}.")

samples = tuple(samples)
chunks = b''.join(samples)
chunks = b"".join(samples)
chunk_sizes = tuple(_nbytes(sample) for sample in samples)
if not chunks:
raise ValueError("samples contained no data; can't train dictionary.")
Expand Down Expand Up @@ -109,23 +110,23 @@ def finalize_dict(zstd_dict, /, samples, dict_size, level):
"""

if not isinstance(zstd_dict, ZstdDict):
raise TypeError('zstd_dict argument should be a ZstdDict object.')
raise TypeError("zstd_dict argument should be a ZstdDict object.")
if not isinstance(dict_size, int):
raise TypeError('dict_size argument should be an int object.')
raise TypeError("dict_size argument should be an int object.")
if not isinstance(level, int):
raise TypeError('level argument should be an int object.')
raise TypeError("level argument should be an int object.")

samples = tuple(samples)
chunks = b''.join(samples)
chunks = b"".join(samples)
chunk_sizes = tuple(_nbytes(sample) for sample in samples)
if not chunks:
raise ValueError("The samples are empty content, can't finalize the"
raise ValueError("The samples are empty content, can't finalize the "
"dictionary.")
dict_content = _zstd._finalize_dict(zstd_dict.dict_content,
chunks, chunk_sizes,
dict_size, level)
dict_content = _zstd._finalize_dict(zstd_dict.dict_content, chunks,
chunk_sizes, dict_size, level)
return ZstdDict(dict_content)


def compress(data, level=None, options=None, zstd_dict=None):
"""Return Zstandard compressed *data* as bytes.

Expand All @@ -141,6 +142,7 @@ def compress(data, level=None, options=None, zstd_dict=None):
comp = ZstdCompressor(level=level, options=options, zstd_dict=zstd_dict)
return comp.compress(data, mode=ZstdCompressor.FLUSH_FRAME)


def decompress(data, zstd_dict=None, options=None):
"""Decompress one or more frames of Zstandard compressed *data*.

Expand Down
10 changes: 5 additions & 5 deletions Lib/compression/zstd/_zstdfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def __init__(self, file, /, mode="r", *,
raise ValueError(f"Invalid mode: {mode!r}")

if isinstance(file, (str, bytes, PathLike)):
self._fp = io.open(file, f'{mode}b')
self._fp = io.open(file, f"{mode}b")
self._close_fp = True
elif ((mode == 'r' and hasattr(file, "read"))
or (mode != 'r' and hasattr(file, "write"))):
elif ((mode == "r" and hasattr(file, "read"))
or (mode != "r" and hasattr(file, "write"))):
self._fp = file
else:
raise TypeError("file must be a file-like object "
Expand All @@ -107,7 +107,7 @@ def close(self):
return
try:
if self._mode == _MODE_READ:
if getattr(self, '_buffer', None):
if getattr(self, "_buffer", None):
self._buffer.close()
self._buffer = None
elif self._mode == _MODE_WRITE:
Expand Down Expand Up @@ -274,7 +274,7 @@ def name(self):

@property
def mode(self):
return 'wb' if self._mode == _MODE_WRITE else 'rb'
return "wb" if self._mode == _MODE_WRITE else "rb"

@property
def closed(self):
Expand Down
Loading