Skip to content

Commit e4e0fba

Browse files
committed
Use ruff to format and check code; remove isort
1 parent b28c153 commit e4e0fba

File tree

11 files changed

+50
-27
lines changed

11 files changed

+50
-27
lines changed

examples/benchmark.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
1bc8711 130.93s MacBook Pro (Retina, 15-inch, Mid 2014) 2.2GHz i7, 16GB RAM
1616
2277962 80.68s MacBook Pro (Retina, 15-inch, Mid 2014) 2.2GHz i7, 16GB RAM
1717
"""
18+
1819
import time
1920

2021
from rasterstats import zonal_stats

pyproject.toml

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ dependencies = [
4242
progress = [
4343
"tqdm"
4444
]
45+
docs = [
46+
"numpydoc",
47+
"sphinx",
48+
"sphinx-rtd-theme",
49+
]
4550
test = [
4651
"coverage",
4752
"geopandas",
@@ -52,7 +57,7 @@ test = [
5257
]
5358
dev = [
5459
"rasterstats[test]",
55-
"numpydoc",
60+
"ruff",
5661
"twine",
5762
]
5863

@@ -81,6 +86,18 @@ version = {attr = "rasterstats._version.__version__"}
8186
[tool.hatch.version]
8287
path = "src/rasterstats/_version.py"
8388

84-
[tool.isort]
85-
profile = "black"
86-
known_first_party = ["rasterstats"]
89+
[tool.ruff.lint]
90+
select = [
91+
"E", # pycodestyle
92+
"F", # Pyflakes
93+
"I", # isort
94+
"RUF", # Ruff-specific rules
95+
"UP", # pyupgrade
96+
]
97+
ignore = [
98+
"RUF005", # Consider iterable unpacking instead of concatenation
99+
]
100+
101+
[tool.ruff]
102+
# TODO: files in docs/notebooks/ use old versions and are incompatible with modern tools
103+
extend-exclude = ["*.ipynb"]

src/rasterstats/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
__all__ = [
88
"__version__",
9-
"gen_zonal_stats",
9+
"cli",
1010
"gen_point_query",
11+
"gen_zonal_stats",
12+
"point_query",
1113
"raster_stats",
1214
"zonal_stats",
13-
"point_query",
14-
"cli",
1515
]

src/rasterstats/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ def zonalstats(
4343
4444
The input arguments to zonalstats should be valid GeoJSON Features. (see cligj)
4545
46-
The output GeoJSON will be mostly unchanged but have additional properties per feature
47-
describing the summary statistics (min, max, mean, etc.) of the underlying raster dataset.
46+
The output GeoJSON will be mostly unchanged but have additional properties per
47+
featuredescribing the summary statistics (min, max, mean, etc.) of the underlying
48+
raster dataset.
4849
4950
The raster is specified by the required -r/--raster argument.
5051

src/rasterstats/io.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def parse_feature(obj):
8686
except (AssertionError, TypeError):
8787
pass
8888

89-
raise ValueError("Can't parse %s as a geojson Feature object" % obj)
89+
raise ValueError(f"Can't parse {obj} as a geojson Feature object")
9090

9191

9292
def read_features(obj, layer=0):
@@ -302,7 +302,7 @@ def read(self, bounds=None, window=None, masked=False, boundless=True):
302302
masked: boolean
303303
return a masked numpy array, default: False
304304
boundless: boolean
305-
allow window/bounds that extend beyond the datasets extent, default: True
305+
allow window/bounds that extend beyond the dataset's extent, default: True
306306
partially or completely filled arrays will be returned as appropriate.
307307
308308
Returns

src/rasterstats/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
def raster_stats(*args, **kwargs):
2626
"""Deprecated. Use zonal_stats instead."""
2727
warnings.warn(
28-
"'raster_stats' is an alias to 'zonal_stats'" " and will disappear in 1.0",
28+
"'raster_stats' is an alias to 'zonal_stats' and will disappear in 1.0",
2929
DeprecationWarning,
3030
)
3131
return zonal_stats(*args, **kwargs)
@@ -43,7 +43,9 @@ def zonal_stats(*args, **kwargs):
4343
if progress:
4444
if tqdm is None:
4545
raise ValueError(
46-
"You specified progress=True, but tqdm is not installed in the environment. You can do pip install rasterstats[progress] to install tqdm!"
46+
"You specified progress=True, but tqdm is not installed in "
47+
"the environment. "
48+
"You can do pip install rasterstats[progress] to install tqdm!"
4749
)
4850
stats = gen_zonal_stats(*args, **kwargs)
4951
total = len(args[0])
@@ -140,7 +142,7 @@ def gen_zonal_stats(
140142
Use with `prefix` to ensure unique and meaningful property names.
141143
142144
boundless: boolean
143-
Allow features that extend beyond the raster datasets extent, default: True
145+
Allow features that extend beyond the raster dataset's extent, default: True
144146
Cells outside dataset extents are treated as nodata.
145147
146148
Returns

src/rasterstats/point.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def point_window_unitxy(x, y, affine):
1515
((row1, row2), (col1, col2)), (unitx, unity)
1616
"""
1717
fcol, frow = ~affine * (x, y)
18-
r, c = int(round(frow)), int(round(fcol))
18+
r, c = round(frow), round(fcol)
1919

2020
# The new source window for our 2x2 array
2121
new_win = ((r - 1, r + 1), (c - 1, c + 1))
@@ -50,7 +50,7 @@ def bilinear(arr, x, y):
5050
if hasattr(arr, "count") and arr.count() != 4:
5151
# a masked array with at least one nodata
5252
# fall back to nearest neighbor
53-
val = arr[int(round(1 - y)), int(round(x))]
53+
val = arr[round(1 - y), round(x)]
5454
if val is masked:
5555
return None
5656
else:
@@ -158,7 +158,7 @@ def gen_point_query(
158158
point query values appended as additional properties.
159159
160160
boundless: boolean
161-
Allow features that extend beyond the raster datasets extent, default: True
161+
Allow features that extend beyond the raster dataset's extent, default: True
162162
Cells outside dataset extents are treated as nodata.
163163
164164
Returns

src/rasterstats/utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ def check_stats(stats, categorical):
9393
if x.startswith("percentile_"):
9494
get_percentile(x)
9595
elif x not in VALID_STATS:
96-
raise ValueError(
97-
"Stat `%s` not valid; " "must be one of \n %r" % (x, VALID_STATS)
98-
)
96+
raise ValueError(f"Stat {x!r} not valid; must be one of \n {VALID_STATS}")
9997

10098
run_count = False
10199
if categorical or "majority" in stats or "minority" in stats or "unique" in stats:

tests/test_cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
data_dir = Path(__file__).parent / "data"
1313

14+
1415
def test_cli_feature():
1516
raster = str(data_dir / "slope.tif")
1617
vector = str(data_dir / "feature.geojson")

tests/test_io.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from shapely.geometry import shape
99

1010
from rasterstats.io import ( # todo parse_feature
11-
fiona_generator,
1211
Raster,
1312
boundless_array,
1413
bounds_window,
14+
fiona_generator,
1515
read_featurecollection,
1616
read_features,
1717
rowcol,
@@ -47,7 +47,7 @@ def _test_read_features(indata):
4747

4848
def _test_read_features_single(indata):
4949
# single (first target geom)
50-
geom = shape(list(read_features(indata))[0]["geometry"])
50+
geom = shape(next(iter(read_features(indata)))["geometry"])
5151
assert geom.equals_exact(target_geoms[0], eps)
5252

5353

@@ -280,9 +280,9 @@ def test_Raster():
280280
r2 = Raster(arr, affine, nodata, band=1).read(bounds)
281281

282282
with pytest.raises(ValueError):
283-
r3 = Raster(arr, affine, nodata, band=1).read()
283+
Raster(arr, affine, nodata, band=1).read()
284284
with pytest.raises(ValueError):
285-
r4 = Raster(arr, affine, nodata, band=1).read(bounds=1, window=1)
285+
Raster(arr, affine, nodata, band=1).read(bounds=1, window=1)
286286

287287
# If the abstraction is correct, the arrays are equal
288288
assert np.array_equal(r1.array, r2.array)
@@ -301,7 +301,7 @@ def test_Raster_boundless_disabled():
301301

302302
# rasterio src fails outside extent
303303
with pytest.raises(ValueError):
304-
r1 = Raster(raster, band=1).read(outside_bounds, boundless=False)
304+
Raster(raster, band=1).read(outside_bounds, boundless=False)
305305

306306
# rasterio src works inside extent
307307
r2 = Raster(raster, band=1).read(bounds, boundless=False)
@@ -316,7 +316,7 @@ def test_Raster_boundless_disabled():
316316

317317
# ndarray src fails outside extent
318318
with pytest.raises(ValueError):
319-
r4 = Raster(arr, affine, nodata, band=1).read(outside_bounds, boundless=False)
319+
Raster(arr, affine, nodata, band=1).read(outside_bounds, boundless=False)
320320

321321
# If the abstraction is correct, the arrays are equal
322322
assert np.array_equal(r2.array, r3.array)

0 commit comments

Comments
 (0)