Skip to content

Commit

Permalink
A round of ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriPapadopoulos committed Sep 27, 2024
1 parent ff73770 commit 0bec280
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 178 deletions.
75 changes: 46 additions & 29 deletions src/blosc2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ def compress(
return blosc2_ext.compress(src, typesize, clevel, filter, codec)


def decompress(src: object, dst: object | bytearray = None,
as_bytearray: bool = False) -> str | bytes | bytearray | None:
def decompress(
src: object, dst: object | bytearray = None, as_bytearray: bool = False
) -> str | bytes | bytearray | None:
"""Decompresses a bytes-like compressed object.
Parameters
Expand Down Expand Up @@ -202,8 +203,12 @@ def decompress(src: object, dst: object | bytearray = None,
return blosc2_ext.decompress(src, dst, as_bytearray)


def pack(obj: object, clevel: int = 9, filter: blosc2.Filter = blosc2.Filter.SHUFFLE,
codec: blosc2.Codec = blosc2.Codec.BLOSCLZ) -> str | bytes:
def pack(
obj: object,
clevel: int = 9,
filter: blosc2.Filter = blosc2.Filter.SHUFFLE,
codec: blosc2.Codec = blosc2.Codec.BLOSCLZ,
) -> str | bytes:
"""Pack (compress) a Python object.
Parameters
Expand Down Expand Up @@ -321,8 +326,12 @@ def unpack(packed_object: str | bytes, **kwargs: dict) -> object:
return obj


def pack_array(arr: np.ndarray, clevel: int = 9, filter: blosc2.Filter = blosc2.Filter.SHUFFLE,
codec: blosc2.Codec = blosc2.Codec.BLOSCLZ) -> str | bytes:
def pack_array(
arr: np.ndarray,
clevel: int = 9,
filter: blosc2.Filter = blosc2.Filter.SHUFFLE,
codec: blosc2.Codec = blosc2.Codec.BLOSCLZ,
) -> str | bytes:
"""Pack (compress) a NumPy array. It is equivalent to the pack function.
Parameters
Expand Down Expand Up @@ -591,8 +600,9 @@ def load_array(urlpath: str, dparams: dict = None) -> np.ndarray:
return load_tensor(urlpath, dparams=dparams)


def pack_tensor(tensor: tensorflow.Tensor | torch.Tensor | np.ndarray, chunksize: int = None,
**kwargs: dict) -> bytes | int:
def pack_tensor(
tensor: tensorflow.Tensor | torch.Tensor | np.ndarray, chunksize: int = None, **kwargs: dict
) -> bytes | int:
"""Pack (compress) a TensorFlow or PyTorch tensor or a NumPy array.
Parameters
Expand Down Expand Up @@ -719,8 +729,12 @@ def unpack_tensor(cframe: bytes) -> tensorflow.Tensor | torch.Tensor | np.ndarra
return _unpack_tensor(schunk)


def save_tensor(tensor: tensorflow.Tensor | torch.Tensor | np.ndarray, urlpath: str, chunksize: int = None,
**kwargs: dict) -> int:
def save_tensor(
tensor: tensorflow.Tensor | torch.Tensor | np.ndarray,
urlpath: str,
chunksize: int = None,
**kwargs: dict,
) -> int:
"""Save a serialized PyTorch or TensorFlow tensor or NumPy array in `urlpath`.
Parameters
Expand Down Expand Up @@ -1271,8 +1285,11 @@ def compute_partition(nitems, maxshape, minpart=None):


def compute_chunks_blocks(
shape: tuple[int] | list, chunks: tuple | list | None = None, blocks: tuple | list | None = None,
dtype: np.dtype = np.uint8, **kwargs: dict
shape: tuple[int] | list,
chunks: tuple | list | None = None,
blocks: tuple | list | None = None,
dtype: np.dtype = np.uint8,
**kwargs: dict,
) -> tuple[(int, int)]:
"""
Compute educated guesses for chunks and blocks of a :ref:`NDArray`.
Expand Down Expand Up @@ -1421,13 +1438,13 @@ def compress2(src: object, **kwargs: dict) -> str | bytes:
If an internal error occurred, probably because some
parameter is not a valid parameter.
"""
if kwargs is not None and 'cparams' in kwargs:
if kwargs is not None and "cparams" in kwargs:
if len(kwargs) > 1:
raise AttributeError("Cannot pass both cparams and other kwargs already included in CParams")
if isinstance(kwargs.get('cparams'), blosc2.CParams):
kwargs = asdict(kwargs.get('cparams'))
if isinstance(kwargs.get("cparams"), blosc2.CParams):
kwargs = asdict(kwargs.get("cparams"))
else:
kwargs = kwargs.get('cparams')
kwargs = kwargs.get("cparams")

return blosc2_ext.compress2(src, **kwargs)

Expand Down Expand Up @@ -1481,13 +1498,13 @@ def decompress2(src: object, dst: object | bytearray = None, **kwargs: dict) ->
If the length of :paramref:`src` is smaller than the minimum.
If :paramref:`dst` is not None and its length is 0.
"""
if kwargs is not None and 'dparams' in kwargs:
if kwargs is not None and "dparams" in kwargs:
if len(kwargs) > 1:
raise AttributeError("Cannot pass both dparams and other kwargs already included in DParams")
if isinstance(kwargs.get('dparams'), blosc2.DParams):
kwargs = asdict(kwargs.get('dparams'))
if isinstance(kwargs.get("dparams"), blosc2.DParams):
kwargs = asdict(kwargs.get("dparams"))
else:
kwargs = kwargs.get('dparams')
kwargs = kwargs.get("dparams")

return blosc2_ext.decompress2(src, dst, **kwargs)

Expand Down Expand Up @@ -1582,11 +1599,11 @@ def ndarray_from_cframe(cframe: bytes | str, copy: bool = False) -> blosc2.NDArr


def register_codec(
codec_name: str,
id: int,
encoder: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], int] = None,
decoder: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], int] = None,
version: int = 1
codec_name: str,
id: int,
encoder: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], int] = None,
decoder: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], int] = None,
version: int = 1,
) -> None:
"""Register a user defined codec.
Expand Down Expand Up @@ -1664,10 +1681,10 @@ def decoder1(input, output, meta, schunk):


def register_filter(
id: int,
forward: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], None] = None,
backward: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], None] = None,
name: str = None
id: int,
forward: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], None] = None,
backward: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], None] = None,
name: str = None,
) -> None:
"""Register an user defined filter.
Expand Down
38 changes: 30 additions & 8 deletions src/blosc2/lazyexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,10 @@ def fill_chunk_operands(


def fast_eval(
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None], operands: dict, getitem: bool, **kwargs
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None],
operands: dict,
getitem: bool,
**kwargs,
) -> blosc2.NDArray | np.ndarray:
"""Evaluate the expression in chunks of operands using a fast path.
Expand Down Expand Up @@ -721,7 +724,11 @@ def fast_eval(


def slices_eval(
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None], operands: dict, getitem: bool, _slice=None, **kwargs
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None],
operands: dict,
getitem: bool,
_slice=None,
**kwargs,
) -> blosc2.NDArray | np.ndarray:
"""Evaluate the expression in chunks of operands.
Expand Down Expand Up @@ -896,7 +903,11 @@ def slices_eval(


def reduce_slices(
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None], operands: dict, reduce_args, _slice=None, **kwargs
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None],
operands: dict,
reduce_args,
_slice=None,
**kwargs,
) -> blosc2.NDArray | np.ndarray:
"""Evaluate the expression in chunks of operands.
Expand Down Expand Up @@ -1131,7 +1142,9 @@ def convert_none_out(dtype, reduce_op, reduced_shape):
return out


def chunked_eval(expression: str | Callable[[tuple, np.ndarray, tuple[int]], None], operands: dict, item=None, **kwargs):
def chunked_eval(
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None], operands: dict, item=None, **kwargs
):
"""
Evaluate the expression in chunks of operands.
Expand Down Expand Up @@ -1942,8 +1955,13 @@ def _open_lazyarray(array):
return expr


def lazyudf(func: Callable[[tuple, np.ndarray, tuple[int]], None], inputs: tuple | list,
dtype: np.dtype, chunked_eval: bool = True, **kwargs: dict) -> LazyUDF:
def lazyudf(
func: Callable[[tuple, np.ndarray, tuple[int]], None],
inputs: tuple | list,
dtype: np.dtype,
chunked_eval: bool = True,
**kwargs: dict,
) -> LazyUDF:
"""
Get a LazyUDF from a python user-defined function.
Expand Down Expand Up @@ -2002,8 +2020,12 @@ def lazyudf(func: Callable[[tuple, np.ndarray, tuple[int]], None], inputs: tuple
return LazyUDF(func, inputs, dtype, chunked_eval, **kwargs)


def lazyexpr(expression: str | bytes | LazyExpr, operands: dict = None,
out: blosc2.NDArray | np.ndarray = None, where: tuple | list = None) -> LazyExpr:
def lazyexpr(
expression: str | bytes | LazyExpr,
operands: dict = None,
out: blosc2.NDArray | np.ndarray = None,
where: tuple | list = None,
) -> LazyExpr:
"""
Get a LazyExpr from an expression.
Expand Down
Loading

0 comments on commit 0bec280

Please sign in to comment.