From a510b342ddd52b18b133e86ba6462b36ac086ed1 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Tue, 20 Feb 2024 09:41:22 +0100 Subject: [PATCH 1/7] new feature: AgentSet.get can retrieve one or more then one attribute --- mesa/agent.py | 16 ++++++++++------ tests/test_agent.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/mesa/agent.py b/mesa/agent.py index 243a187337b..42707a06fc0 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -17,7 +17,7 @@ from random import Random # mypy -from typing import TYPE_CHECKING, Any, Callable +from typing import TYPE_CHECKING, Any, Callable, List if TYPE_CHECKING: # We ensure that these are not imported during runtime to prevent cyclic @@ -264,17 +264,21 @@ def do( return res if return_results else self - def get(self, attr_name: str) -> list[Any]: + def get(self, attr_names: str | List[str]) -> list[Any]: """ - Retrieve a specified attribute from each agent in the AgentSet. + Retrieve the specified attribute(s) from each agent in the AgentSet. Args: - attr_name (str): The name of the attribute to retrieve from each agent. + attr_names (str | List[str]): The name(s) of the attribute(s) to retrieve from each agent. Returns: - list[Any]: A list of attribute values from each agent in the set. + list[Any]: A list of attribute values for each agent in the set. """ - return [getattr(agent, attr_name) for agent in self._agents] + + if isinstance(attr_names, str): + return [getattr(agent, attr_names) for agent in self._agents] + else: + return [[getattr(agent, attr_name) for attr_name in attr_names] for agent in self._agents] def __getitem__(self, item: int | slice) -> Agent: """ diff --git a/tests/test_agent.py b/tests/test_agent.py index 7ad538eba27..c47d8aafbd5 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -222,6 +222,23 @@ def test_agentset_get_attribute(): agentset.get("non_existing_attribute") + model = Model() + agents = [] + for i in range(10): + agent = TestAgent(model.next_id(), model) + agent.i = i**2 + agents.append(agent) + agentset = AgentSet(agents, model) + + values = agentset.get(["unique_id", "i"]) + + for value, agent in zip(values, agents): + unique_id, i, = value + assert agent.unique_id == unique_id + assert agent.i == i + + + class OtherAgentType(Agent): def get_unique_identifier(self): return self.unique_id From cb7852325e234179c648b5183deab330a0087f03 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:42:56 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/agent.py | 7 +++++-- tests/test_agent.py | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/mesa/agent.py b/mesa/agent.py index 42707a06fc0..fb19917b240 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -264,7 +264,7 @@ def do( return res if return_results else self - def get(self, attr_names: str | List[str]) -> list[Any]: + def get(self, attr_names: str | list[str]) -> list[Any]: """ Retrieve the specified attribute(s) from each agent in the AgentSet. @@ -278,7 +278,10 @@ def get(self, attr_names: str | List[str]) -> list[Any]: if isinstance(attr_names, str): return [getattr(agent, attr_names) for agent in self._agents] else: - return [[getattr(agent, attr_name) for attr_name in attr_names] for agent in self._agents] + return [ + [getattr(agent, attr_name) for attr_name in attr_names] + for agent in self._agents + ] def __getitem__(self, item: int | slice) -> Agent: """ diff --git a/tests/test_agent.py b/tests/test_agent.py index c47d8aafbd5..0cd211123e1 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -221,7 +221,6 @@ def test_agentset_get_attribute(): with pytest.raises(AttributeError): agentset.get("non_existing_attribute") - model = Model() agents = [] for i in range(10): @@ -233,12 +232,14 @@ def test_agentset_get_attribute(): values = agentset.get(["unique_id", "i"]) for value, agent in zip(values, agents): - unique_id, i, = value + ( + unique_id, + i, + ) = value assert agent.unique_id == unique_id assert agent.i == i - class OtherAgentType(Agent): def get_unique_identifier(self): return self.unique_id From 22cd1046a22c465da6911d86576e6b490d1adeb2 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Tue, 20 Feb 2024 11:23:40 +0100 Subject: [PATCH 3/7] minor docstring update --- mesa/agent.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mesa/agent.py b/mesa/agent.py index fb19917b240..4dcefa46f7a 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -272,7 +272,8 @@ def get(self, attr_names: str | list[str]) -> list[Any]: attr_names (str | List[str]): The name(s) of the attribute(s) to retrieve from each agent. Returns: - list[Any]: A list of attribute values for each agent in the set. + list[Any]: A list with the attribute value for each agent in the set if attr_names is a str + List[List[Any]]: A list with a list of attribute values for each agent in the set if attr_names is a list of str """ if isinstance(attr_names, str): From 5f9622732725e990bac60e1218750a79f51d4319 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 20 Feb 2024 10:23:59 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa/agent.py b/mesa/agent.py index 4dcefa46f7a..d68b7e8ff81 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -17,7 +17,7 @@ from random import Random # mypy -from typing import TYPE_CHECKING, Any, Callable, List +from typing import TYPE_CHECKING, Any, Callable if TYPE_CHECKING: # We ensure that these are not imported during runtime to prevent cyclic From 76bb633047127920ccc60d659f6a9ed0b918a11f Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 21 Feb 2024 07:42:17 +0100 Subject: [PATCH 5/7] add AttributeError to docstring and fixes capital L in List --- mesa/agent.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mesa/agent.py b/mesa/agent.py index d68b7e8ff81..59ea2de9dfc 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -274,6 +274,10 @@ def get(self, attr_names: str | list[str]) -> list[Any]: Returns: list[Any]: A list with the attribute value for each agent in the set if attr_names is a str List[List[Any]]: A list with a list of attribute values for each agent in the set if attr_names is a list of str + + Raises: + AttributeError if an agent does not have the specified attribute(s) + """ if isinstance(attr_names, str): From c0660a4108b9c000e8277e53a2f168e398c48437 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 21 Feb 2024 07:59:16 +0100 Subject: [PATCH 6/7] Update agent.py --- mesa/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa/agent.py b/mesa/agent.py index 59ea2de9dfc..d5916eaefb2 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -273,7 +273,7 @@ def get(self, attr_names: str | list[str]) -> list[Any]: Returns: list[Any]: A list with the attribute value for each agent in the set if attr_names is a str - List[List[Any]]: A list with a list of attribute values for each agent in the set if attr_names is a list of str + list[list[Any]]: A list with a list of attribute values for each agent in the set if attr_names is a list of str Raises: AttributeError if an agent does not have the specified attribute(s) From c441910dbcdc48f7343510fba41c613d8b1c8770 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 21 Feb 2024 08:02:31 +0100 Subject: [PATCH 7/7] Update agent.py --- mesa/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa/agent.py b/mesa/agent.py index d5916eaefb2..57edf1a032a 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -269,7 +269,7 @@ def get(self, attr_names: str | list[str]) -> list[Any]: Retrieve the specified attribute(s) from each agent in the AgentSet. Args: - attr_names (str | List[str]): The name(s) of the attribute(s) to retrieve from each agent. + attr_names (str | list[str]): The name(s) of the attribute(s) to retrieve from each agent. Returns: list[Any]: A list with the attribute value for each agent in the set if attr_names is a str