Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit edf4f06

Browse files
authored
Add ruff as pre-commit hook (#46)
1 parent 3e74938 commit edf4f06

File tree

21 files changed

+213
-150
lines changed

21 files changed

+213
-150
lines changed

‎.flake8

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@ extend-ignore =
1212
# E203 whitespace before ':' (to be compatible with black)
1313
per-file-ignores =
1414
__init__.py:F401,F403, # allow unused and star imports
15-
test_*.py:F401,F403,
1615
graphblas_algorithms/nxapi/exception.py:F401,
17-
graphblas_algorithms/**/__init__.py:F401,F403

‎.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ repos:
4747
rev: 23.1.0
4848
hooks:
4949
- id: black
50+
# - id: black-jupyter
5051
- repo: https://github.com/PyCQA/flake8
5152
rev: 6.0.0
5253
hooks:
@@ -69,6 +70,10 @@ repos:
6970
types_or: [python, rst, markdown]
7071
additional_dependencies: [tomli]
7172
files: ^(graphblas_algorithms|docs)/
73+
- repo: https://github.com/charliermarsh/ruff-pre-commit
74+
rev: v0.0.249
75+
hooks:
76+
- id: ruff
7277
- repo: https://github.com/pre-commit/pre-commit-hooks
7378
rev: v4.4.0
7479
hooks:

‎graphblas_algorithms/algorithms/cluster.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,19 @@ def average_clustering(G, *, count_zeros=True, weighted=False, mask=None):
178178
val = c.reduce().get(0)
179179
if not count_zeros:
180180
return val / c.nvals
181-
elif mask is not None:
181+
if mask is not None:
182182
return val / mask.parent.nvals
183-
else:
184-
return val / c.size
183+
return val / c.size
185184

186185

187186
def average_clustering_directed(G, *, count_zeros=True, weighted=False, mask=None):
188187
c = clustering_directed(G, weighted=weighted, mask=mask)
189188
val = c.reduce().get(0)
190189
if not count_zeros:
191190
return val / c.nvals
192-
elif mask is not None:
191+
if mask is not None:
193192
return val / mask.parent.nvals
194-
else:
195-
return val / c.size
193+
return val / c.size
196194

197195

198196
def single_square_clustering(G, idx):

‎graphblas_algorithms/algorithms/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def k_truss(G: Graph, k) -> Graph:
2121
C = Matrix("int32", S.nrows, S.ncols)
2222
while True:
2323
C(S.S, replace) << plus_pair(S @ S.T)
24-
C << select.value(C>=k - 2)
24+
C << select.value(k - 2<=C)
2525
if C.nvals == nvals_last:
2626
break
2727
nvals_last = C.nvals

‎graphblas_algorithms/algorithms/regular.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ def is_regular(G):
1010
return False
1111
d = degrees.get(0)
1212
return (degrees == d).reduce(monoid.land).get(True)
13-
else:
14-
row_degrees = G.get_property("row_degrees+")
15-
if row_degrees.nvals != row_degrees.size:
16-
return False
17-
column_degrees = G.get_property("column_degrees+")
18-
if column_degrees.nvals != column_degrees.size:
19-
return False
20-
d = row_degrees.get(0)
21-
if not (row_degrees == d).reduce(monoid.land):
22-
return False
23-
d = column_degrees.get(0)
24-
return (column_degrees == d).reduce(monoid.land).get(True)
13+
row_degrees = G.get_property("row_degrees+")
14+
if row_degrees.nvals != row_degrees.size:
15+
return False
16+
column_degrees = G.get_property("column_degrees+")
17+
if column_degrees.nvals != column_degrees.size:
18+
return False
19+
d = row_degrees.get(0)
20+
if not (row_degrees == d).reduce(monoid.land):
21+
return False
22+
d = column_degrees.get(0)
23+
return (column_degrees == d).reduce(monoid.land).get(True)
2524

2625

2726
def is_k_regular(G, k):

‎graphblas_algorithms/algorithms/shortest_paths/weighted.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def single_source_bellman_ford_path_length(G, source):
7575

7676

7777
def bellman_ford_path_lengths(G, nodes=None, *, expand_output=False):
78-
"""
78+
"""Extra parameter: expand_output
7979
8080
Parameters
8181
----------

‎graphblas_algorithms/classes/_caching.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ def get_reduction(G, mask=None):
1515
if mask is not None:
1616
if key in cache:
1717
return cache[key].dup(mask=mask)
18-
elif cache.get("has_self_edges") is False and f"{keybase}+" in cache:
18+
if cache.get("has_self_edges") is False and f"{keybase}+" in cache:
1919
cache[key] = cache[f"{keybase}+"]
2020
return cache[key].dup(mask=mask)
21-
elif "offdiag" in cache:
21+
if "offdiag" in cache:
2222
return getattr(cache["offdiag"], methodname)(op_).new(mask=mask, name=key)
23-
elif (
23+
if (
2424
"L-" in cache
2525
and "U-" in cache
2626
and opclass in {"BinaryOp", "Monoid"}
@@ -30,12 +30,9 @@ def get_reduction(G, mask=None):
3030
getattr(cache["L-"], methodname)(op_).new(mask=mask)
3131
| getattr(cache["U-"], methodname)(op_).new(mask=mask)
3232
).new(name=key)
33-
elif not G.get_property("has_self_edges"):
33+
if not G.get_property("has_self_edges"):
3434
return G.get_property(f"{keybase}+", mask=mask)
35-
else:
36-
return getattr(G.get_property("offdiag"), methodname)(op_).new(
37-
mask=mask, name=key
38-
)
35+
return getattr(G.get_property("offdiag"), methodname)(op_).new(mask=mask, name=key)
3936
if key not in cache:
4037
if cache.get("has_self_edges") is False and f"{keybase}+" in cache:
4138
cache[key] = cache[f"{keybase}+"]
@@ -73,13 +70,12 @@ def get_reduction(G, mask=None):
7370
if mask is not None:
7471
if key in cache:
7572
return cache[key].dup(mask=mask)
76-
elif cache.get("has_self_edges") is False and f"{keybase}-" in cache:
73+
if cache.get("has_self_edges") is False and f"{keybase}-" in cache:
7774
cache[key] = cache[f"{keybase}-"]
7875
return cache[key].dup(mask=mask)
79-
elif methodname == "reduce_columnwise" and "AT" in cache:
76+
if methodname == "reduce_columnwise" and "AT" in cache:
8077
return cache["AT"].reduce_rowwise(op_).new(mask=mask, name=key)
81-
else:
82-
return getattr(A, methodname)(op_).new(mask=mask, name=key)
78+
return getattr(A, methodname)(op_).new(mask=mask, name=key)
8379
if key not in cache:
8480
if cache.get("has_self_edges") is False and f"{keybase}-" in cache:
8581
cache[key] = cache[f"{keybase}-"]
@@ -185,5 +181,5 @@ def get_reduction(G, mask=None):
185181
return cache[key]
186182

187183
else: # pragma: no cover (sanity)
188-
raise RuntimeError()
184+
raise RuntimeError
189185
return get_reduction

‎graphblas_algorithms/classes/_utils.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,25 +193,24 @@ def matrix_to_dicts(self, A, *, use_row_index=False, use_column_index=False, val
193193
return {
194194
row: dict(zip(col_indices[start:stop], values[start:stop])) for row, (start, stop) in it
195195
}
196-
elif use_row_index:
196+
if use_row_index:
197197
return {
198198
row: {
199199
id_to_key[col]: val for col, val in zip(col_indices[start:stop], values[start:stop])
200200
}
201201
for row, (start, stop) in it
202202
}
203-
elif use_column_index:
203+
if use_column_index:
204204
return {
205205
id_to_key[row]: dict(zip(col_indices[start:stop], values[start:stop]))
206206
for row, (start, stop) in it
207207
}
208-
else:
209-
return {
210-
id_to_key[row]: {
211-
id_to_key[col]: val for col, val in zip(col_indices[start:stop], values[start:stop])
212-
}
213-
for row, (start, stop) in it
208+
return {
209+
id_to_key[row]: {
210+
id_to_key[col]: val for col, val in zip(col_indices[start:stop], values[start:stop])
214211
}
212+
for row, (start, stop) in it
213+
}
215214

216215

217216
def to_networkx(self, edge_attribute="weight"):

‎graphblas_algorithms/classes/digraph.py

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,20 @@ def get_recip_degreesp(G, mask=None):
119119
"""pair(A & A.T).reduce_rowwise()"""
120120
A = G._A
121121
cache = G._cache
122-
if "AT" in cache:
123-
AT = cache["AT"]
124-
else:
125-
AT = A.T
122+
AT = cache.get("AT", A.T)
126123
if mask is not None:
127124
if "recip_degrees+" in cache:
128125
return cache["recip_degrees+"].dup(mask=mask)
129-
elif cache.get("has_self_edges") is False and "recip_degrees-" in cache:
126+
if cache.get("has_self_edges") is False and "recip_degrees-" in cache:
130127
cache["recip_degrees+"] = cache["recip_degrees-"]
131128
return cache["recip_degrees-"].dup(mask=mask)
132-
elif "recip_degrees-" in cache and "diag" in cache:
129+
if "recip_degrees-" in cache and "diag" in cache:
133130
return (unary.one(cache["diag"]) + cache["recip_degrees-"]).new(
134131
mask=mask, name="recip_degrees+"
135132
)
136-
elif "recip_degrees-" in cache and not G.get_property("has_self_edges"):
133+
if "recip_degrees-" in cache and not G.get_property("has_self_edges"):
137134
return cache["recip_degrees-"].dup(mask=mask)
138-
else:
139-
return binary.pair(A & AT).reduce_rowwise().new(mask=mask, name="recip_degrees+")
135+
return binary.pair(A & AT).reduce_rowwise().new(mask=mask, name="recip_degrees+")
140136
if "recip_degrees+" not in cache:
141137
if cache.get("has_self_edges") is False and "recip_degrees-" in cache:
142138
cache["recip_degrees+"] = cache["recip_degrees-"]
@@ -174,34 +170,33 @@ def get_recip_degreesm(G, mask=None):
174170
if mask is not None:
175171
if "recip_degrees-" in cache:
176172
return cache["recip_degrees-"].dup(mask=mask)
177-
elif cache.get("has_self_edges") is False and "recip_degrees+" in cache:
173+
if cache.get("has_self_edges") is False and "recip_degrees+" in cache:
178174
cache["recip_degrees-"] = cache["recip_degrees+"]
179175
return cache["recip_degrees-"].dup(mask=mask)
180-
elif "recip_degrees+" in cache and "diag" in cache:
176+
if "recip_degrees+" in cache and "diag" in cache:
181177
rv = binary.minus(cache["recip_degrees+"] | unary.one(cache["diag"])).new(
182178
mask=mask, name="recip_degrees-"
183179
)
184180
rv(rv.V, replace) << rv # drop 0s
185181
return rv
186-
elif not G.get_property("has_self_edges"):
182+
if not G.get_property("has_self_edges"):
187183
return G.get_property("recip_degrees+", mask=mask)
188-
elif "offdiag" in cache:
184+
if "offdiag" in cache:
189185
return (
190186
binary.pair(cache["offdiag"] & AT)
191187
.reduce_rowwise()
192188
.new(mask=mask, name="recip_degrees-")
193189
)
194-
elif "L-" in cache and "U-" in cache:
190+
if "L-" in cache and "U-" in cache:
195191
return (
196192
binary.pair(cache["L-"] & AT).reduce_rowwise().new(mask=mask)
197193
+ binary.pair(cache["U-"] & AT).reduce_rowwise().new(mask=mask)
198194
).new(name="recip_degrees-")
199-
else:
200-
diag = G.get_property("diag", mask=mask)
201-
overlap = binary.pair(A & AT).reduce_rowwise().new(mask=mask)
202-
rv = binary.minus(overlap | unary.one(diag)).new(name="recip_degrees-")
203-
rv(rv.V, replace) << rv # drop 0s
204-
return rv
195+
diag = G.get_property("diag", mask=mask)
196+
overlap = binary.pair(A & AT).reduce_rowwise().new(mask=mask)
197+
rv = binary.minus(overlap | unary.one(diag)).new(name="recip_degrees-")
198+
rv(rv.V, replace) << rv # drop 0s
199+
return rv
205200
if "recip_degrees-" not in cache:
206201
if cache.get("has_self_edges") is False and "recip_degrees+" in cache:
207202
cache["recip_degrees-"] = cache["recip_degrees+"]
@@ -245,14 +240,12 @@ def get_total_degreesp(G, mask=None):
245240
if mask is not None:
246241
if "total_degrees+" in cache:
247242
return cache["total_degrees+"].dup(mask=mask)
248-
elif cache.get("has_self_edges") is False and "total_degrees-" in cache:
243+
if cache.get("has_self_edges") is False and "total_degrees-" in cache:
249244
cache["total_degrees+"] = cache["total_degrees-"]
250245
return cache["total_degrees+"].dup(mask=mask)
251-
else:
252-
return (
253-
G.get_property("row_degrees+", mask=mask)
254-
+ G.get_property("column_degrees+", mask=mask)
255-
).new(name="total_degrees+")
246+
return (
247+
G.get_property("row_degrees+", mask=mask) + G.get_property("column_degrees+", mask=mask)
248+
).new(name="total_degrees+")
256249
if "total_degrees+" not in cache:
257250
if cache.get("has_self_edges") is False and "total_degrees-" in cache:
258251
cache["total_degrees+"] = cache["total_degrees-"]
@@ -277,14 +270,12 @@ def get_total_degreesm(G, mask=None):
277270
if mask is not None:
278271
if "total_degrees-" in cache:
279272
return cache["total_degrees-"].dup(mask=mask)
280-
elif cache.get("has_self_edges") is False and "total_degrees+" in cache:
273+
if cache.get("has_self_edges") is False and "total_degrees+" in cache:
281274
cache["total_degrees-"] = cache["total_degrees+"]
282275
return cache["total_degrees-"].dup(mask=mask)
283-
else:
284-
return (
285-
G.get_property("row_degrees-", mask=mask)
286-
+ G.get_property("column_degrees-", mask=mask)
287-
).new(name="total_degrees-")
276+
return (
277+
G.get_property("row_degrees-", mask=mask) + G.get_property("column_degrees-", mask=mask)
278+
).new(name="total_degrees-")
288279
if "total_degrees-" not in cache:
289280
if cache.get("has_self_edges") is False and "total_degrees+" in cache:
290281
cache["total_degrees-"] = cache["total_degrees+"]
@@ -313,10 +304,7 @@ def get_total_recipp(G, mask=None):
313304
elif "recip_degrees+" in cache:
314305
cache["total_recip+"] = cache["recip_degrees+"].reduce().get(0)
315306
else:
316-
if "AT" in cache:
317-
AT = cache["AT"]
318-
else:
319-
AT = A.T
307+
AT = cache.get("AT", A.T)
320308
cache["total_recip+"] = binary.pair(A & AT).reduce_scalar().get(0)
321309
if "has_self_edges" not in cache and "total_recip-" in cache:
322310
cache["has_self_edges"] = cache["total_recip+"] > cache["total_recip-"]
@@ -398,8 +386,7 @@ def to_directed_graph(G, weight=None, dtype=None):
398386
return DiGraph.from_networkx(G, weight=weight, dtype=dtype)
399387
except ImportError:
400388
pass
401-
402-
raise TypeError()
389+
raise TypeError
403390

404391

405392
def to_graph(G, weight=None, dtype=None):
@@ -420,8 +407,7 @@ def to_graph(G, weight=None, dtype=None):
420407
return ga.Graph.from_networkx(G, weight=weight, dtype=dtype)
421408
except ImportError:
422409
pass
423-
424-
raise TypeError()
410+
raise TypeError
425411

426412

427413
class AutoDict(dict):

‎graphblas_algorithms/classes/graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def to_undirected_graph(G, weight=None, dtype=None):
266266
except ImportError:
267267
pass
268268

269-
raise TypeError()
269+
raise TypeError
270270

271271

272272
class AutoDict(dict):

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /