Skip to content

Commit f22d6b0

Browse files
committed
Refactor: some methods to properties at Cell & Grid
1 parent a54f2e0 commit f22d6b0

File tree

10 files changed

+71
-69
lines changed

10 files changed

+71
-69
lines changed

algorithms/aldous_broder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def on(grid: Grid) -> Grid:
1717
unvisited_count = grid.size - 1
1818

1919
while unvisited_count > 0:
20-
neighbor = choice(current_cell.neighbors())
20+
neighbor = choice(current_cell.neighbors)
2121
if neighbor is None:
2222
raise ValueError("Aldous-Broder algorithm needs all cells to have at least one neighbor")
2323
if len(neighbor.links) == 0:

base/cell.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ def column(self) -> int:
1717
def links(self) -> List["Cell"]:
1818
return list(self._links.keys())
1919

20+
@property
21+
def neighbors(self) -> List[Optional["Cell"]]:
22+
neighbors_list = [] # type: List[Optional[Cell]]
23+
if self.north:
24+
neighbors_list.append(self.north)
25+
if self.south:
26+
neighbors_list.append(self.south)
27+
if self.east:
28+
neighbors_list.append(self.east)
29+
if self.west:
30+
neighbors_list.append(self.west)
31+
return neighbors_list
32+
2033
def __init__(self, row: int, column: int) -> None:
2134
if row is None or row < 0:
2235
raise ValueError("Row must be a positive integer")
@@ -46,18 +59,7 @@ def unlink(self, cell: "Cell", bidirectional: bool = True) -> "Cell":
4659
def linked_to(self, cell: "Cell") -> bool:
4760
return cell in self._links
4861

49-
def neighbors(self) -> List[Optional["Cell"]]:
50-
neighbors_list = [] # type: List[Optional[Cell]]
51-
if self.north:
52-
neighbors_list.append(self.north)
53-
if self.south:
54-
neighbors_list.append(self.south)
55-
if self.east:
56-
neighbors_list.append(self.east)
57-
if self.west:
58-
neighbors_list.append(self.west)
59-
return neighbors_list
60-
62+
@property
6163
def distances(self) -> Distances:
6264
distances = Distances(self)
6365
frontier = [self] # type: List[Cell]

base/grid.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, rows: int, columns: int) -> None:
2929
self._grid = self.prepare_grid()
3030
self.configure_cells()
3131

32-
def get_cell(self, row: int, column: int) -> Optional[Cell]:
32+
def cell_at(self, row: int, column: int) -> Optional[Cell]:
3333
if not (0 <= row < self.rows):
3434
return None
3535
if not (0 <= column < self.columns):
@@ -41,15 +41,15 @@ def prepare_grid(self) -> List[List[Cell]]:
4141

4242
def configure_cells(self) -> None:
4343
for cell in self.each_cell():
44-
cell.north = self.get_cell(cell.row - 1, cell.column)
45-
cell.south = self.get_cell(cell.row + 1, cell.column)
46-
cell.east = self.get_cell(cell.row, cell.column + 1)
47-
cell.west = self.get_cell(cell.row, cell.column - 1)
44+
cell.north = self.cell_at(cell.row - 1, cell.column)
45+
cell.south = self.cell_at(cell.row + 1, cell.column)
46+
cell.east = self.cell_at(cell.row, cell.column + 1)
47+
cell.west = self.cell_at(cell.row, cell.column - 1)
4848

4949
def random_cell(self) -> Cell:
5050
column = randint(0, self.columns - 1)
5151
row = randint(0, self.rows - 1)
52-
return cast(Cell, self.get_cell(row, column))
52+
return cast(Cell, self.cell_at(row, column))
5353

5454
def each_row(self) -> Generator:
5555
for row in range(self.rows):
@@ -58,7 +58,7 @@ def each_row(self) -> Generator:
5858
def each_cell(self) -> Generator:
5959
for row in range(self.rows):
6060
for column in range(self.columns):
61-
yield self.get_cell(row, column)
61+
yield self.cell_at(row, column)
6262

6363
def contents_of(self, cell: Cell) -> str:
6464
return " "

demos/image_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ def get_coloring() -> bool:
6767
start_row = round(grid.rows / 2)
6868
start_column = round(grid.columns / 2)
6969
print("Drawing colored maze with start row {} column {}".format(start_row, start_column))
70-
start_cell = grid.get_cell(start_row, start_column)
70+
start_cell = grid.cell_at(start_row, start_column)
7171
if start_cell is None:
7272
raise IndexError("Invalid start cell row {} column {}".format(start_row, start_column))
73-
grid.distances = start_cell.distances() # type: ignore
73+
grid.distances = start_cell.distances # type: ignore
7474

7575
filename = strftime("%Y%m%d%H%M%S", gmtime())
7676

pathfinders/dijkstra.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
def calculate_distances(grid: DistanceGrid, start_row: int, start_column: int, end_row: int,
55
end_column: int) -> DistanceGrid:
6-
start_cell = grid.get_cell(start_row, start_column)
6+
start_cell = grid.cell_at(start_row, start_column)
77
if start_cell is None:
88
raise IndexError("Invalid start cell row {} column {}".format(start_row, start_column))
9-
destination_cell = grid.get_cell(end_row, end_column)
9+
destination_cell = grid.cell_at(end_row, end_column)
1010
if destination_cell is None:
1111
raise IndexError("Invalid destination cell row {} column {}".format(end_row, end_row))
12-
distances = start_cell.distances()
12+
distances = start_cell.distances
1313
grid.distances = distances.path_to(destination_cell)
1414

1515
return grid

pathfinders/longest_path.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010

1111
def calculate(grid: DistanceGrid) -> Tuple[int, int, int, int]:
12-
start_cell = grid.get_cell(0, 0)
12+
start_cell = grid.cell_at(0, 0)
1313
if start_cell is None:
1414
raise IndexError("Invalid start cell row {} column {}".format(0, 0))
1515

16-
new_start_cell, distance = start_cell.distances().max
17-
goal_cell, distance = new_start_cell.distances().max
16+
new_start_cell, distance = start_cell.distances.max
17+
goal_cell, distance = new_start_cell.distances.max
1818

1919
return new_start_cell.row, new_start_cell.column, goal_cell.row, goal_cell.column

renderers/unicode_renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def render(grid: Grid, **kwargs: Any) -> None:
2727

2828
output = JUNCTIONS[12]
2929
for x in range(grid.columns - 1):
30-
output += (horizontal_wall * 3 + get_topmost_junction(cast(Cell, grid.get_cell(row=0, column=x))))
30+
output += (horizontal_wall * 3 + get_topmost_junction(cast(Cell, grid.cell_at(row=0, column=x))))
3131
output += horizontal_wall * 3 + JUNCTIONS[10] + "\n"
3232

3333
for row in grid.each_row():

test/test_cell.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_links_listing() -> None:
6060

6161

6262
def test_has_no_neighbors() -> None:
63-
assert Cell(1, 1).neighbors() == []
63+
assert Cell(1, 1).neighbors == []
6464

6565

6666
def test_has_neighbors() -> None:
@@ -73,15 +73,15 @@ def test_has_neighbors() -> None:
7373
yet_another_cell.east = another_cell
7474
yet_another_cell.west = a_cell
7575

76-
assert another_cell in a_cell.neighbors()
77-
assert yet_another_cell not in a_cell.neighbors()
78-
assert len(a_cell.neighbors()) == 1
79-
assert a_cell not in another_cell.neighbors()
80-
assert yet_another_cell in another_cell.neighbors()
81-
assert len(another_cell.neighbors()) == 1
82-
assert a_cell in yet_another_cell.neighbors()
83-
assert another_cell in yet_another_cell.neighbors()
84-
assert len(yet_another_cell.neighbors()) == 2
76+
assert another_cell in a_cell.neighbors
77+
assert yet_another_cell not in a_cell.neighbors
78+
assert len(a_cell.neighbors) == 1
79+
assert a_cell not in another_cell.neighbors
80+
assert yet_another_cell in another_cell.neighbors
81+
assert len(another_cell.neighbors) == 1
82+
assert a_cell in yet_another_cell.neighbors
83+
assert another_cell in yet_another_cell.neighbors
84+
assert len(yet_another_cell.neighbors) == 2
8585

8686

8787
def test_distances() -> None:
@@ -93,7 +93,7 @@ def test_distances() -> None:
9393
a_cell.link(another_cell)
9494
another_cell.east = yet_another_cell
9595
another_cell.link(yet_another_cell)
96-
distances = a_cell.distances()
96+
distances = a_cell.distances
9797
assert distances.cells == [yet_another_cell, another_cell, a_cell]
9898
assert distances[a_cell] == 0
9999
assert distances[another_cell] == 1

test/test_grid.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,39 @@ def test_constructor() -> None:
1616
def test_cell_access() -> None:
1717
grid = Grid(2, 2)
1818

19-
assert grid.get_cell(0, 0) == Cell(0, 0) # type: ignore
20-
assert grid.get_cell(0, 1) == Cell(0, 1) # type: ignore
21-
assert grid.get_cell(1, 0) == Cell(1, 0) # type: ignore
22-
assert grid.get_cell(1, 1) == Cell(1, 1) # type: ignore
19+
assert grid.cell_at(0, 0) == Cell(0, 0) # type: ignore
20+
assert grid.cell_at(0, 1) == Cell(0, 1) # type: ignore
21+
assert grid.cell_at(1, 0) == Cell(1, 0) # type: ignore
22+
assert grid.cell_at(1, 1) == Cell(1, 1) # type: ignore
2323

24-
assert grid.get_cell(-1, 0) is None
25-
assert grid.get_cell(0, -1) is None
26-
assert grid.get_cell(4, 0) is None
27-
assert grid.get_cell(0, 4) is None
24+
assert grid.cell_at(-1, 0) is None
25+
assert grid.cell_at(0, -1) is None
26+
assert grid.cell_at(4, 0) is None
27+
assert grid.cell_at(0, 4) is None
2828

2929

3030
def test_neighbors_setup_when_grid_is_created() -> None:
3131
grid = Grid(2, 2)
3232

33-
assert grid.get_cell(0, 0).north is None # type: ignore
34-
assert grid.get_cell(0, 0).south == Cell(1, 0) # type: ignore
35-
assert grid.get_cell(0, 0).east == Cell(0, 1) # type: ignore
36-
assert grid.get_cell(0, 0).west is None # type: ignore
37-
38-
assert grid.get_cell(0, 1).north is None # type: ignore
39-
assert grid.get_cell(0, 1).south == Cell(1, 1) # type: ignore
40-
assert grid.get_cell(0, 1).east is None # type: ignore
41-
assert grid.get_cell(0, 1).west == Cell(0, 0) # type: ignore
42-
43-
assert grid.get_cell(1, 0).north == Cell(0, 0) # type: ignore
44-
assert grid.get_cell(1, 0).south is None # type: ignore
45-
assert grid.get_cell(1, 0).east == Cell(1, 1) # type: ignore
46-
assert grid.get_cell(1, 0).west is None # type: ignore
47-
48-
assert grid.get_cell(1, 1).north == Cell(0, 1) # type: ignore
49-
assert grid.get_cell(1, 1).south is None # type: ignore
50-
assert grid.get_cell(1, 1).east is None # type: ignore
51-
assert grid.get_cell(1, 1).west == Cell(1, 0) # type: ignore
33+
assert grid.cell_at(0, 0).north is None # type: ignore
34+
assert grid.cell_at(0, 0).south == Cell(1, 0) # type: ignore
35+
assert grid.cell_at(0, 0).east == Cell(0, 1) # type: ignore
36+
assert grid.cell_at(0, 0).west is None # type: ignore
37+
38+
assert grid.cell_at(0, 1).north is None # type: ignore
39+
assert grid.cell_at(0, 1).south == Cell(1, 1) # type: ignore
40+
assert grid.cell_at(0, 1).east is None # type: ignore
41+
assert grid.cell_at(0, 1).west == Cell(0, 0) # type: ignore
42+
43+
assert grid.cell_at(1, 0).north == Cell(0, 0) # type: ignore
44+
assert grid.cell_at(1, 0).south is None # type: ignore
45+
assert grid.cell_at(1, 0).east == Cell(1, 1) # type: ignore
46+
assert grid.cell_at(1, 0).west is None # type: ignore
47+
48+
assert grid.cell_at(1, 1).north == Cell(0, 1) # type: ignore
49+
assert grid.cell_at(1, 1).south is None # type: ignore
50+
assert grid.cell_at(1, 1).east is None # type: ignore
51+
assert grid.cell_at(1, 1).west == Cell(1, 0) # type: ignore
5252

5353

5454
def test_random_cell() -> None:

utils/rotator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def on(grid: Union[Grid, "DistanceGrid", "ColoredGrid"]) -> Union[Grid, "Distanc
2323

2424
for old_cell in grid.each_cell():
2525
row, column = Rotator._rotated_coordinates(old_cell, rotated_grid)
26-
new_cell = rotated_grid.get_cell(row, column)
26+
new_cell = rotated_grid.cell_at(row, column)
2727
if new_cell is None:
2828
raise IndexError("Cell not found at row {} column {}".format(row, column))
2929
Rotator._rotate_cell_neighbors(new_cell, old_cell, rotated_grid)
@@ -40,7 +40,7 @@ def _rotated_coordinates(cell: Cell, grid: Grid) -> Tuple[int, int]:
4040
def _rotate_cell_neighbors(new_cell: Cell, old_cell: Cell, grid: Grid) -> None:
4141
for link in old_cell.links:
4242
row, column = Rotator._rotated_coordinates(link, grid)
43-
neighbor = grid.get_cell(row, column)
43+
neighbor = grid.cell_at(row, column)
4444
if neighbor is None:
4545
raise IndexError("Cell not found at row {} column {}".format(row, column))
4646
new_cell.link(neighbor)

0 commit comments

Comments
 (0)