Skip to content

Use the -O flag for improved performance in error handling #31 #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/sugarscape_ig/ss_polars/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def get_best_moves(self, neighborhood: pl.DataFrame) -> pl.DataFrame:
pl.DataFrame
DataFrame with the best moves for each agent
"""
raise NotImplementedError("Subclasses must implement this method")
if __debug__: # Only execute in non-optimized mode
raise NotImplementedError("Subclasses must implement this method")


class AntPolarsLoopDF(AntPolarsBase):
Expand Down Expand Up @@ -309,7 +310,8 @@ def _prepare_cells(
return occupied_cells, free_cells, target_cells

def _get_best_moves(self):
raise NotImplementedError("Subclasses must implement this method")
if __debug__: # Only execute in non-optimized mode
raise NotImplementedError("Subclasses must implement this method")


class AntPolarsLoopNoVec(AntPolarsLoop):
Expand Down
9 changes: 5 additions & 4 deletions mesa_frames/abstract/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,10 +998,11 @@

@abstractmethod
def __getattr__(self, name: str) -> Any:
if name == "_agents":
raise RuntimeError(
"The _agents attribute is not set. You probably forgot to call super().__init__ in the __init__ method."
)
if __debug__: # Only execute in non-optimized mode
if name == "_agents":
raise AttributeError(

Check warning on line 1003 in mesa_frames/abstract/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/abstract/agents.py#L1003

Added line #L1003 was not covered by tests
"The _agents attribute is not set. You probably forgot to call super().__init__ in the __init__ method."
)

@overload
def __getitem__(self, key: str | tuple[AgentMask, str]) -> Series | DataFrame: ...
Expand Down
10 changes: 6 additions & 4 deletions mesa_frames/concrete/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,12 @@
return super().__add__(other)

def __getattr__(self, name: str) -> dict[AgentSetDF, Any]:
if name.startswith("_"): # Avoids infinite recursion of private attributes
raise AttributeError(
f"'{self.__class__.__name__}' object has no attribute '{name}'"
)
# Avoids infinite recursion of private attributes
if __debug__: # Only execute in non-optimized mode
if name.startswith("_"):
raise AttributeError(

Check warning on line 444 in mesa_frames/concrete/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/concrete/agents.py#L444

Added line #L444 was not covered by tests
f"'{self.__class__.__name__}' object has no attribute '{name}'"
)
return {agentset: getattr(agentset, name) for agentset in self._agentsets}

@overload
Expand Down
13 changes: 8 additions & 5 deletions mesa_frames/concrete/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,17 @@
try:
return self._agents
except AttributeError:
raise ValueError(
"You haven't called super().__init__() in your model. Make sure to call it in your __init__ method."
)
if __debug__: # Only execute in non-optimized mode
raise RuntimeError(

Check warning on line 179 in mesa_frames/concrete/model.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/concrete/model.py#L178-L179

Added lines #L178 - L179 were not covered by tests
"You haven't called super().__init__() in your model. Make sure to call it in your __init__ method."
)

@agents.setter
def agents(self, agents: AgentsDF) -> None:
if not isinstance(agents, AgentsDF):
raise TypeError("agents must be an instance of AgentsDF")
if __debug__: # Only execute in non-optimized mode
if not isinstance(agents, AgentsDF):
raise TypeError("agents must be an instance of AgentsDF")

Check warning on line 187 in mesa_frames/concrete/model.py

View check run for this annotation

Codecov / codecov/patch

mesa_frames/concrete/model.py#L185-L187

Added lines #L185 - L187 were not covered by tests

self._agents = agents

@property
Expand Down