Skip to content

Commit

Permalink
Refactor NetworkGrid docstrings and iter/get_cell_list_contents (proj…
Browse files Browse the repository at this point in the history
…ectmesa#1498)

* Refactor NetworkGrid docstrings and iter/get_cell_list_contents

* Update space.py

* Update space.py
  • Loading branch information
Tortar authored Nov 4, 2022
1 parent 8759f7b commit 3267522
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,14 +963,12 @@ def default_val() -> list:
return []

def place_agent(self, agent: Agent, node_id: int) -> None:
"""Place a agent in a node."""

"""Place an agent in a node."""
self.G.nodes[node_id]["agent"].append(agent)
agent.pos = node_id

def get_neighbors(self, node_id: int, include_center: bool = False) -> list[int]:
"""Get all adjacent nodes"""

neighbors = list(self.G.neighbors(node_id))
if include_center:
neighbors.append(node_id)
Expand All @@ -979,7 +977,6 @@ def get_neighbors(self, node_id: int, include_center: bool = False) -> list[int]

def move_agent(self, agent: Agent, node_id: int) -> None:
"""Move an agent from its current node to a new node."""

self.remove_agent(agent)
self.place_agent(agent, node_id)

Expand All @@ -994,22 +991,22 @@ def is_cell_empty(self, node_id: int) -> bool:
return self.G.nodes[node_id]["agent"] == self.default_val()

def get_cell_list_contents(self, cell_list: list[int]) -> list[GridContent]:
"""Returns the contents of a list of cells ((x,y) tuples)
Note: this method returns a list of `Agent`'s; `None` contents are excluded.
"""Returns a list of the agents contained in the nodes identified
in `cell_list`; nodes with empty content are excluded.
"""
return list(self.iter_cell_list_contents(cell_list))

def get_all_cell_contents(self) -> list[GridContent]:
"""Returns a list of the contents of the cells
identified in cell_list."""
return list(self.iter_cell_list_contents(self.G))

def iter_cell_list_contents(self, cell_list: list[int]) -> list[GridContent]:
"""Returns an iterator of the contents of the cells
identified in cell_list."""
list_of_lists = [
self.G.nodes[node_id]["agent"]
for node_id in cell_list
if not self.is_cell_empty(node_id)
]
return [item for sublist in list_of_lists for item in sublist]

def get_all_cell_contents(self) -> list[GridContent]:
"""Returns a list of all the agents in the network."""
return self.get_cell_list_contents(self.G)

def iter_cell_list_contents(self, cell_list: list[int]) -> Iterator[GridContent]:
"""Returns an iterator of the agents contained in the nodes identified
in `cell_list`; nodes with empty content are excluded.
"""
yield from self.get_cell_list_contents(cell_list)

0 comments on commit 3267522

Please sign in to comment.