8000 gh-132983: Style improvements for `compression.zstd` by emmatyping · Pull Request #133547 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132983: Style improvements for compression.zstd #133547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 14, 2025
Prev Previous commit
Next Next commit
Use single quotes over double quotes
This better matches CPython's style
  • Loading branch information
emmatyping committed May 13, 2025
commit 3f39ea4263a0f0ff2352c26d593c2e1b5fc5f13c
64 changes: 32 additions & 32 deletions Lib/compression/zstd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

__all__ = (
# compression.zstd
"COMPRESSION_LEVEL_DEFAULT",
"compress",
"CompressionParameter",
"decompress",
"DecompressionParameter",
"finalize_dict",
"get_frame_info",
"Strategy",
"train_dict",
'COMPRESSION_LEVEL_DEFAULT',
'compress',
'CompressionParameter',
'decompress',
'DecompressionParameter',
'finalize_dict',
'get_frame_info',
'Strategy',
'train_dict',

# compression.zstd._zstdfile
"open",
"ZstdFile",
'open',
'ZstdFile',

# _zstd
"get_frame_size",
"zstd_version",
"zstd_version_info",
"ZstdCompressor",
"ZstdDecompressor",
"ZstdDict",
"ZstdError",
'get_frame_size',
'zstd_version',
'zstd_version_info',
'ZstdCompressor',
'ZstdDecompressor',
'ZstdDict',
'ZstdError',
)

import _zstd
Expand All @@ -44,15 +44,15 @@
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 @@ -85,10 +85,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 @@ -116,14 +116,14 @@ 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 "
Expand Down Expand Up @@ -164,12 +164,12 @@ def decompress(data, zstd_dict=None, options=None):
decomp = ZstdDecompressor(options=options, zstd_dict=zstd_dict)
results.append(decomp.decompress(data))
if not decomp.eof:
raise ZstdError("Compressed data ended before the "
"end-of-stream marker was reached")
raise ZstdError('Compressed data ended before the '
'end-of-stream marker was reached')
data = decomp.unused_data
if not data:
break
return b"".join(results)
return b''.join(results)


class CompressionParameter(enum.IntEnum):
Expand Down
72 changes: 36 additions & 36 deletions Lib/compression/zstd/_zstdfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ZSTD_DStreamOutSize)
from compression._common import _streams

__all__ = ("ZstdFile", "open")
__all__ = ('ZstdFile', 'open')

_MODE_CLOSED = 0
_MODE_READ = 1
Expand All @@ -31,15 +31,15 @@ class ZstdFile(_streams.BaseStream):
FLUSH_BLOCK = ZstdCompressor.FLUSH_BLOCK
FLUSH_FRAME = ZstdCompressor.FLUSH_FRAME

def __init__(self, file, /, mode="r", *,
def __init__(self, file, /, mode='r', *,
level=None, options=None, zstd_dict=None):
"""Open a Zstandard compressed file in binary mode.

*file* can be either an file-like object, or a file name to open.

*mode* can be "r" for reading (default), "w" for (over)writing, "x" for
creating exclusively, or "a" for appending. These can equivalently be
given as "rb", "wb", "xb" and "ab" respectively.
*mode* can be 'r' for reading (default), 'w' for (over)writing, 'x' for
creating exclusively, or 'a' for appending. These can equivalently be
given as 'rb', 'wb', 'xb' and 'ab' respectively.

*level* is an optional int specifying the compression level to use,
or COMPRESSION_LEVEL_DEFAULT if not given.
Expand All @@ -57,33 +57,33 @@ def __init__(self, file, /, mode="r", *,
self._buffer = None

if not isinstance(mode, str):
raise ValueError("mode must be a str")
raise ValueError('mode must be a str')
if options is not None and not isinstance(options, dict):
raise TypeError("options must be a dict or None")
mode = mode.removesuffix("b") # handle rb, wb, xb, ab
if mode == "r":
raise TypeError('options must be a dict or None')
mode = mode.removesuffix('b') # handle rb, wb, xb, ab
if mode == 'r':
if level is not None:
raise TypeError("level is illegal in read mode")
raise TypeError('level is illegal in read mode')
self._mode = _MODE_READ
elif mode in {"w", "a", "x"}:
elif mode in {'w', 'a', 'x'}:
if level is not None and not isinstance(level, int):
raise TypeError("level must be int or None")
raise TypeError('level must be int or None')
self._mode = _MODE_WRITE
self._compressor = ZstdCompressor(level=level, options=options,
zstd_dict=zstd_dict)
self._pos = 0
else:
raise ValueError(f"Invalid mode: {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 "
"or a str, bytes, or PathLike object")
raise TypeError('file must be a file-like object '
'or a str, bytes, or PathLike object')

if self._mode == _MODE_READ:
raw = _streams.DecompressReader(
Expand All @@ -104,7 +104,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 @@ -151,22 +151,22 @@ def flush(self, mode=FLUSH_BLOCK):
return
self._check_not_closed()
if mode not in {self.FLUSH_BLOCK, self.FLUSH_FRAME}:
raise ValueError("Invalid mode argument, expected either "
"ZstdFile.FLUSH_FRAME or "
"ZstdFile.FLUSH_BLOCK")
raise ValueError('Invalid mode argument, expected either '
'ZstdFile.FLUSH_FRAME or '
'ZstdFile.FLUSH_BLOCK')
if self._compressor.last_mode == mode:
return
# Flush zstd block/frame, and write.
data = self._compressor.flush(mode)
self._fp.write(data)
if hasattr(self._fp, "flush"):
if hasattr(self._fp, 'flush'):
self._fp.flush()

def read(self, size=-1):
"""Read up to size uncompressed bytes from the file.

If size is negative or omitted, read until EOF is reached.
Returns b"" if the file is already at EOF.
Returns b'' if the file is already at EOF.
"""
if size is None:
size = -1
Expand All @@ -178,7 +178,7 @@ def read1(self, size=-1):
making multiple reads from the underlying stream. Reads up to a
buffer's worth of data if size is negative.

Returns b"" if the file is at EOF.
Returns b'' if the file is at EOF.
"""
self._check_can_read()
if size < 0:
Expand Down Expand Up @@ -271,7 +271,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 All @@ -293,16 +293,16 @@ def writable(self):
return self._mode == _MODE_WRITE


def open(file, /, mode="rb", *, level=None, options=None, zstd_dict=None,
def open(file, /, mode='rb', *, level=None, options=None, zstd_dict=None,
encoding=None, errors=None, newline=None):
"""Open a Zstandard compressed file in binary or text mode.

file can be either a file name (given as a str, bytes, or PathLike object),
in which case the named file is opened, or it can be an existing file object
to read from or write to.

The mode parameter can be "r", "rb" (default), "w", "wb", "x", "xb", "a",
"ab" for binary mode, or "rt", "wt", "xt", "at" for text mode.
The mode parameter can be 'r', 'rb' (default), 'w', 'wb', 'x', 'xb', 'a',
'ab' for binary mode, or 'rt', 'wt', 'xt', 'at' for text mode.

The level, options, and zstd_dict parameters specify the settings the same
as ZstdFile.
Expand All @@ -323,19 +323,19 @@ def open(file, /, mode="rb", *, level=None, options=None, zstd_dict=None,
behavior, and line ending(s).
"""

text_mode = "t" in mode
mode = mode.replace("t", "")
text_mode = 't' in mode
mode = mode.replace('t', '')

if text_mode:
if "b" in mode:
raise ValueError(f"Invalid mode: {mode!r}")
if 'b' in mode:
raise ValueError(f'Invalid mode: {mode!r}')
else:
if encoding is not None:
raise ValueError("Argument 'encoding' not supported in binary mode")
raise ValueError('Argument "encoding" not supported in binary mode')
if errors is not None:
raise ValueError("Argument 'errors' not supported in binary mode")
raise ValueError('Argument "errors" not supported in binary mode')
if newline is not None:
raise ValueError("Argument 'newline' not supported in binary mode")
raise ValueError('Argument "newline" not supported in binary mode')

binary_file = ZstdFile(file, mode, level=level, options=options,
zstd_dict=zstd_dict)
Expand Down
0