Skip to content

Commit e3b917c

Browse files
authored
Merge pull request silx-kit#347 from t20100/lint
Added standard formatting/linting
2 parents 1344a5c + db99a0b commit e3b917c

File tree

13 files changed

+1024
-756
lines changed

13 files changed

+1024
-756
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[flake8]
22
max-line-length = 88
33
extend-ignore = E203,E501,E701
4+
extend-exclude =
5+
build,
6+
lib

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ jobs:
1717
with:
1818
python-version: '3.12'
1919
cache: 'pip'
20-
- run: python -m pip install --upgrade pip bandit build twine
20+
- run: python -m pip install --upgrade pip bandit black build flake8 isort twine
2121
- run: bandit -c pyproject.toml -r .
22+
- run: black --check .
23+
- run: flake8
24+
- run: isort --check .
2225
- run: python -m build --sdist
2326
- run: python -m twine check dist/*
2427
# Build documentation

doc/conf.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
# -- Project information -----------------------------------------------------
1818

1919
project = "hdf5plugin"
20-
copyright = (
21-
"2016-2024, European Synchrotron Radiation Facility, Grenoble"
22-
)
20+
copyright = "2016-2025, European Synchrotron Radiation Facility, Grenoble"
2321
author = "ESRF"
2422

2523
# -- General configuration ---------------------------------------------------

doc/contribute.rst

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,26 @@ To also run tests relying on actual HDF5 files, run from the source directory::
2424

2525
This tests the installed version of `hdf5plugin`.
2626

27-
Linting
28-
=======
27+
Formatting/Linting
28+
==================
29+
30+
From the source directory:
31+
32+
* Format code with `black <https://black.readthedocs.io/>`_::
33+
34+
black .
35+
36+
* Sort imports with `isort <https://pycqa.github.io/isort/>`_::
37+
38+
isort .
39+
40+
* Check code with `flake8 <https://flake8.pycqa.org/>`_::
41+
42+
flake8
2943

30-
Run `bandit <https://github.com/PyCQA/bandit/>_` security linter from the source directory::
44+
* Check code with `bandit <https://bandit.readthedocs.io/>`_ security linter::
3145

32-
bandit -c pyproject.toml -r .
46+
bandit -c pyproject.toml -r .
3347

3448
Building documentation
3549
======================

doc/hdf5plugin_EuropeanHUG2023/benchmark.py

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
import sys
88
import tempfile
99
import time
10-
from typing import Optional, NamedTuple
10+
from typing import NamedTuple, Optional
1111

1212
import h5py
1313
import numpy
1414

15-
1615
# Set affinity and env. var. before importing hdf5plugin
1716

1817
if len(sys.argv) >= 2:
@@ -28,13 +27,14 @@
2827
# Directory where to run read/write benchmarks
2928
DIRECTORY = "/dev/shm"
3029

30+
3131
# Function to get data to use for tests
3232
def get_data():
3333
with h5py.File("/dev/shm/kevlar.h5") as h5file:
3434
return h5file["/entry/data/data"][::10] # Take 100 frames
3535

3636

37-
import hdf5plugin
37+
import hdf5plugin # noqa: E402
3838

3939

4040
class Result(NamedTuple):
@@ -46,21 +46,18 @@ class Result(NamedTuple):
4646
read_duration: float
4747
chunks: tuple[int]
4848

49-
compression_rate = property(
50-
lambda self: self.raw_nbytes / self.compressed_nbytes)
49+
compression_rate = property(lambda self: self.raw_nbytes / self.compressed_nbytes)
5150
write_speed = property(
5251
lambda self: (self.raw_nbytes / 1024**2) / self.write_duration,
53-
doc="Unit: MB/sec")
52+
doc="Unit: MB/sec",
53+
)
5454
read_speed = property(
5555
lambda self: (self.raw_nbytes / 1024**2) / self.read_duration,
56-
doc="Unit: MB/sec")
57-
58-
59-
def benchmark(
60-
data: numpy.ndarray,
61-
directory: Optional[str] = None,
62-
**kwargs
63-
) -> Result:
56+
doc="Unit: MB/sec",
57+
)
58+
59+
60+
def benchmark(data: numpy.ndarray, directory: Optional[str] = None, **kwargs) -> Result:
6461
"""Run benchmark for given conditions
6562
6663
:param data: Dataset to use
@@ -73,15 +70,16 @@ def benchmark(
7370
else:
7471
dirname = directory
7572

76-
filename = os.path.join(dirname, 'hdf5plugin_benchmark.h5')
73+
filename = os.path.join(dirname, "hdf5plugin_benchmark.h5")
7774

7875
if os.path.exists(filename):
7976
os.remove(filename)
8077

8178
# Compression
8279
with h5py.File(filename, "w") as h5file:
8380
dataset = h5file.create_dataset(
84-
"data", shape=data.shape, dtype=data.dtype, **kwargs)
81+
"data", shape=data.shape, dtype=data.dtype, **kwargs
82+
)
8583
start_write_time = time.perf_counter()
8684
dataset[:] = data
8785
dataset.flush()
@@ -91,16 +89,18 @@ def benchmark(
9189
with h5py.File(filename, "r") as h5file:
9290
dataset = h5file["data"]
9391
start_time = time.perf_counter()
94-
read_data = dataset[()]
92+
dataset[()]
9593
read_duration = time.perf_counter() - start_time
9694
storage_size = dataset.id.get_storage_size()
9795
chunks = dataset.chunks
98-
96+
9997
os.remove(filename)
10098

101-
return Result(data.nbytes, storage_size, write_duration, read_duration, chunks=chunks)
99+
return Result(
100+
data.nbytes, storage_size, write_duration, read_duration, chunks=chunks
101+
)
102+
102103

103-
104104
DEFAULT_FILTERS = { # Filters available with h5py/libhdf5
105105
"Raw": None,
106106
"GZip": "gzip",
@@ -114,28 +114,34 @@ def benchmark(
114114
}
115115

116116
BITSHUFFLE_FILTERS = {
117-
"Bitshuffle-lz4": hdf5plugin.Bitshuffle(cname='lz4'),
118-
"Bitshuffle-zstd": hdf5plugin.Bitshuffle(cname='zstd'),
117+
"Bitshuffle-lz4": hdf5plugin.Bitshuffle(cname="lz4"),
118+
"Bitshuffle-zstd": hdf5plugin.Bitshuffle(cname="zstd"),
119119
}
120-
120+
121121
BLOSC_FILTERS = {}
122-
for cname in ('lz4', 'blosclz', 'lz4', 'lz4hc', 'snappy', 'zlib', 'zstd'):
123-
for shuffle_name, shuffle in [('NoShuffle', hdf5plugin.Blosc.NOSHUFFLE),
124-
('Shuffle', hdf5plugin.Blosc.SHUFFLE),
125-
('BitShuffle', hdf5plugin.Blosc.BITSHUFFLE)]:
126-
for clevel in [5]: #(1, 3, 5, 9):
122+
for cname in ("lz4", "blosclz", "lz4", "lz4hc", "snappy", "zlib", "zstd"):
123+
for shuffle_name, shuffle in [
124+
("NoShuffle", hdf5plugin.Blosc.NOSHUFFLE),
125+
("Shuffle", hdf5plugin.Blosc.SHUFFLE),
126+
("BitShuffle", hdf5plugin.Blosc.BITSHUFFLE),
127+
]:
128+
for clevel in [5]: # (1, 3, 5, 9):
127129
BLOSC_FILTERS[f"Blosc-{cname}-{shuffle_name}-{clevel}"] = hdf5plugin.Blosc(
128-
cname=cname, clevel=clevel, shuffle=shuffle)
130+
cname=cname, clevel=clevel, shuffle=shuffle
131+
)
129132

130133

131134
BLOSC2_FILTERS = {}
132-
for cname in ('lz4', 'blosclz', 'lz4', 'lz4hc', 'zlib', 'zstd'):
133-
for filters_name, filters in [('NoFilter', hdf5plugin.Blosc2.NOFILTER),
134-
('Shuffle', hdf5plugin.Blosc2.SHUFFLE),
135-
('BitShuffle', hdf5plugin.Blosc2.BITSHUFFLE)]:
136-
for clevel in [5]: # (1, 3, 5, 9):
137-
BLOSC2_FILTERS[f"Blosc2-{cname}-{filters_name}-{clevel}"] = hdf5plugin.Blosc2(
138-
cname=cname, clevel=clevel, filters=filters)
135+
for cname in ("lz4", "blosclz", "lz4", "lz4hc", "zlib", "zstd"):
136+
for filters_name, filters in [
137+
("NoFilter", hdf5plugin.Blosc2.NOFILTER),
138+
("Shuffle", hdf5plugin.Blosc2.SHUFFLE),
139+
("BitShuffle", hdf5plugin.Blosc2.BITSHUFFLE),
140+
]:
141+
for clevel in [5]: # (1, 3, 5, 9):
142+
BLOSC2_FILTERS[f"Blosc2-{cname}-{filters_name}-{clevel}"] = (
143+
hdf5plugin.Blosc2(cname=cname, clevel=clevel, filters=filters)
144+
)
139145

140146

141147
FILTERS = {

pyproject.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ test = [
4545
"blosc2>=2.5.1",
4646
"blosc2-grok>=0.2.2",
4747
]
48+
dev = [
49+
"hdf5plugin[doc,test]",
50+
"bandit",
51+
"black",
52+
"flake8",
53+
"isort",
54+
]
4855

4956
[tool.setuptools]
5057
packages = ["hdf5plugin"]
@@ -62,3 +69,15 @@ exclude_dirs = [
6269

6370
[tool.bandit.assert_used]
6471
skips = ["*test.py"]
72+
73+
[tool.black]
74+
extend-exclude = '''
75+
(
76+
lib
77+
| .*\.ipynb
78+
)
79+
'''
80+
81+
[tool.isort]
82+
profile = "black"
83+
skip_glob = ["lib"]

0 commit comments

Comments
 (0)