Skip to content

Commit 476f6da

Browse files
committed
Version 1.0.11: automatically cleanup if needed
1 parent 4cdaa3f commit 476f6da

8 files changed

Lines changed: 32 additions & 7 deletions

File tree

pathfinding/core/graph.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def __init__(
1010
self.edges = edges if edges else []
1111
self.nodes = nodes if nodes else {}
1212
self.bi_directional = bi_directional
13+
# we will call cleanup automatically if dirty is True
14+
self.dirty = False
1315
self.edge_node_items()
1416
if not nodes:
1517
self.generate_nodes()

pathfinding/core/grid.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def __init__(
4646
"""
4747
self.width = width
4848
self.height = height
49+
# we will call cleanup automatically if dirty is True
50+
self.dirty = False
4951
self.passable_left_right_border = False
5052
self.passable_up_down_border = False
5153
if isinstance(matrix, (tuple, list)) or (

pathfinding/core/world.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@
99
class World:
1010
def __init__(self, grids: Dict[int, Grid]):
1111
self.grids = grids
12+
self.dirty = False
1213

13-
def neighbors(self, node: Node, diagonal_movement: DiagonalMovement) -> List[Node]:
14-
return self.grids[node.grid_id].neighbors(node, diagonal_movement=diagonal_movement)
14+
def cleanup(self):
15+
for grid in self.grids:
16+
grid.cleanup()
17+
18+
def neighbors(
19+
self, node: Node,
20+
diagonal_movement: DiagonalMovement) -> List[Node]:
21+
return self.grids[node.grid_id].neighbors(
22+
node, diagonal_movement=diagonal_movement)
1523

1624
def calc_cost(self, node_a, node_b, weighted=False):
1725
# TODO: if node_a.grid_id != node_b.grid_id calculate distance between
1826
# grids as well, for now we ignore switching grids
19-
return self.grids[node_a.grid_id].calc_cost(node_a, node_b, weighted=weighted)
27+
return self.grids[node_a.grid_id].calc_cost(
28+
node_a, node_b, weighted=weighted)

pathfinding/finder/bi_a_star.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def find_path(self, start, end, grid):
4343
:param grid: grid that stores all possible steps/tiles as 2D-list
4444
:return:
4545
"""
46+
self.clean_grid(grid)
47+
4648
self.start_time = time.time() # execution time limitation
4749
self.runs = 0 # count number of iterations
4850

pathfinding/finder/finder.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ def check_neighbors(self, start, end, graph, open_list,
138138
raise NotImplementedError(
139139
'Please implement check_neighbors in your finder')
140140

141+
def clean_grid(self, grid):
142+
"""clean the map if needed."""
143+
if grid.dirty:
144+
grid.cleanup()
145+
grid.dirty = True
146+
141147
def find_path(self, start, end, grid):
142148
"""
143149
find a path from start to end node on grid by iterating over
@@ -148,6 +154,8 @@ def find_path(self, start, end, grid):
148154
(can be a list of grids)
149155
:return:
150156
"""
157+
self.clean_grid(grid)
158+
151159
self.start_time = time.time() # execution time limitation
152160
self.runs = 0 # count number of iterations
153161
start.opened = True

pathfinding/finder/ida_star.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ def search(self, node, g, cutoff, path, depth, end, grid):
9797
return min_t
9898

9999
def find_path(self, start, end, grid):
100+
self.clean_grid(grid)
101+
100102
self.start_time = time.time() # execution time limitation
101103
self.runs = 0 # count number of iterations
102104

pathfinding/finder/msp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import heapq
21
import time
32
from collections import deque, namedtuple
43
from ..core import heuristic
@@ -49,6 +48,8 @@ def itertree(self, grid, start):
4948
grid, neighbor, node, end, open_list, open_value=True)
5049

5150
def find_path(self, start, end, grid):
51+
self.clean_grid(grid)
52+
5253
self.start_time = time.time() # execution time limitation
5354
self.runs = 0 # count number of iterations
5455

@@ -61,5 +62,4 @@ def find_path(self, start, end, grid):
6162
step = step.parent
6263
path.appendleft(step)
6364
return path, self.runs
64-
else:
65-
return [], self.runs
65+
return [], self.runs

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
long_description=long_description,
1212
long_description_content_type="text/markdown",
1313
url="https://github.com/brean/python-pathfinding",
14-
version="1.0.10",
14+
version="1.0.11",
1515
license="MIT",
1616
author="Andreas Bresser",
1717
packages=find_packages(),

0 commit comments

Comments
 (0)