From 4faa89c611c94ba3ef62c6ffd562b058adbcbd74 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 22 Mar 2025 09:43:08 +0100 Subject: [PATCH 1/2] ruff v0.11.2 and rules C4,PERF,UP032,UP034 --- benchmarks.py | 4 ++-- mazelib/generate/Kruskal.py | 16 +++++++++------- mazelib/mazelib.py | 4 +--- mazelib/solve/ShortestPath.py | 2 +- mazelib/solve/ShortestPaths.py | 2 +- pyproject.toml | 9 +++++---- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/benchmarks.py b/benchmarks.py index 17c4e61..2422915 100644 --- a/benchmarks.py +++ b/benchmarks.py @@ -78,8 +78,8 @@ def print_benchmarks(times): """ print("\nmazelib benchmarking") print(datetime.now().strftime("%Y-%m-%d %H:%M")) - print("Python version: {0}".format(get_python_version())) - print("mazelib version: {0}".format(version)) + print(f"Python version: {get_python_version()}") + print(f"mazelib version: {version}") print( "\nTotal Time (seconds): %.5f\n" % sum([sum(times_row) for times_row in times]) ) diff --git a/mazelib/generate/Kruskal.py b/mazelib/generate/Kruskal.py index b145035..ac84316 100644 --- a/mazelib/generate/Kruskal.py +++ b/mazelib/generate/Kruskal.py @@ -31,13 +31,15 @@ def generate(self): forest.append([(row, col)]) grid[row][col] = 0 - edges = [] - for row in range(2, self.H - 1, 2): - for col in range(1, self.W - 1, 2): - edges.append((row, col)) - for row in range(1, self.H - 1, 2): - for col in range(2, self.W - 1, 2): - edges.append((row, col)) + edges = [ + (row, col) + for row in range(2, self.H - 1, 2) + for col in range(1, self.W - 1, 2) + ] + [ + (row, col) + for row in range(1, self.H - 1, 2) + for col in range(2, self.W - 1, 2) + ] shuffle(edges) diff --git a/mazelib/mazelib.py b/mazelib/mazelib.py index 7e6871d..0ab71ff 100644 --- a/mazelib/mazelib.py +++ b/mazelib/mazelib.py @@ -218,9 +218,7 @@ def tostring(self, entrances=False, solutions=False): return "" # build the walls of the grid - txt = [] - for row in self.grid: - txt.append("".join(["#" if cell else " " for cell in row])) + txt = ["".join("#" if cell else " " for cell in row) for row in self.grid] # insert the start and end points if entrances and self.start and self.end: diff --git a/mazelib/solve/ShortestPath.py b/mazelib/solve/ShortestPath.py index 587120f..1c72b9e 100644 --- a/mazelib/solve/ShortestPath.py +++ b/mazelib/solve/ShortestPath.py @@ -91,7 +91,7 @@ def _solve(self): # 3) a solution reaches the end or a dead end when we mark it by appending a None. num_unfinished = sum( - map(lambda sol: 0 if sol[-1] is None else 1, solutions) + 0 if sol[-1] is None else 1 for sol in solutions ) # 4) clean-up solutions diff --git a/mazelib/solve/ShortestPaths.py b/mazelib/solve/ShortestPaths.py index ba9f0ab..4cbf9ea 100644 --- a/mazelib/solve/ShortestPaths.py +++ b/mazelib/solve/ShortestPaths.py @@ -88,7 +88,7 @@ def _solve(self): # 3) a solution reaches the end or a dead end when we mark it by appending a None. num_unfinished = sum( - map(lambda sol: 0 if sol[-1] is None else 1, solutions) + 0 if sol[-1] is None else 1 for sol in solutions ) # 4) clean-up solutions diff --git a/pyproject.toml b/pyproject.toml index d5b1940..59efed2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,7 +38,7 @@ classifiers = [ [project.optional-dependencies] dev = [ "black == 24.10.0", - "ruff == 0.7.4", + "ruff == 0.11.2", "toml == 0.10.2", "twine == 4.0.2", ] @@ -62,7 +62,7 @@ readme = {file = ["README.md"]} ####################################################################### [tool.ruff] # This is the exact version of Ruff we use. -required-version = "0.7.4" +required-version = "0.11.2" # Assume Python 3.13 target-version = "py313" @@ -98,11 +98,13 @@ exclude = [ [tool.ruff.lint] # Enable pycodestyle (E) and Pyflakes (F) codes by default. +# C4 - flake8-comprehensions # D - NumPy docstring rules # N801 - Class name should use CapWords convention +# PERF - Perflint # SIM - code simplification rules # TID - tidy imports -select = ["E", "F", "D", "N801", "SIM", "TID"] +select = ["E", "F", "C4", "D", "N801", "PERF", "SIM", "TID"] # Ruff rules we ignore (for now) because they are not 100% automatable # @@ -142,4 +144,3 @@ ban-relative-imports = "all" [tool.ruff.lint.pydocstyle] convention = "numpy" - From 599eb6e923c1422ac3a03129a98ecbf36e02140b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 22 Mar 2025 09:52:52 +0100 Subject: [PATCH 2/2] Placate psf/black --- mazelib/solve/ShortestPath.py | 4 +--- mazelib/solve/ShortestPaths.py | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/mazelib/solve/ShortestPath.py b/mazelib/solve/ShortestPath.py index 1c72b9e..af6eabb 100644 --- a/mazelib/solve/ShortestPath.py +++ b/mazelib/solve/ShortestPath.py @@ -90,9 +90,7 @@ def _solve(self): solutions[s].append(ns[0]) # 3) a solution reaches the end or a dead end when we mark it by appending a None. - num_unfinished = sum( - 0 if sol[-1] is None else 1 for sol in solutions - ) + num_unfinished = sum(0 if sol[-1] is None else 1 for sol in solutions) # 4) clean-up solutions return self._clean_up(solutions) diff --git a/mazelib/solve/ShortestPaths.py b/mazelib/solve/ShortestPaths.py index 4cbf9ea..0d618e4 100644 --- a/mazelib/solve/ShortestPaths.py +++ b/mazelib/solve/ShortestPaths.py @@ -87,9 +87,7 @@ def _solve(self): solutions[s].append(ns[0]) # 3) a solution reaches the end or a dead end when we mark it by appending a None. - num_unfinished = sum( - 0 if sol[-1] is None else 1 for sol in solutions - ) + num_unfinished = sum(0 if sol[-1] is None else 1 for sol in solutions) # 4) clean-up solutions solutions = self._clean_up(solutions)