Skip to content

Commit c1d83f1

Browse files
reyan-singhpre-commit-ci[bot]adamamer20Copilot
authored
Use the -O flag for improved performance in error handling #31 (#141)
* Replacing error code with optimized code Replacing common error code with more optimize code using assert / __debug__ for production ready build along with maintaining error handling capabilities for development * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updating few changes fixing optimization code * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixed build test fixed build test * fixed build code fixed build code * updating agent to fix build updating agent to fix build * Updated assert code along with __debug__ Updated assert code along with __debug__ to make code informtive while in debug mode but optimized when build with -O option * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Instead of assert raising informative errors modifying assert code with raise... to raise informative errors to the user * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update examples/sugarscape_ig/ss_polars/agents.py Co-authored-by: Copilot <[email protected]> * Update examples/sugarscape_ig/ss_polars/agents.py Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Adam Amer <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 836e31a commit c1d83f1

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

mesa_frames/abstract/agents.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -998,10 +998,11 @@ def __iadd__(self, other: DataFrame | Sequence[Any] | dict[str, Any]) -> Self:
998998

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

10061007
@overload
10071008
def __getitem__(self, key: str | tuple[AgentMask, str]) -> Series | DataFrame: ...

mesa_frames/concrete/agents.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,12 @@ def __add__(self, other: AgentSetDF | Iterable[AgentSetDF]) -> Self:
438438
return super().__add__(other)
439439

440440
def __getattr__(self, name: str) -> dict[AgentSetDF, Any]:
441-
if name.startswith("_"): # Avoids infinite recursion of private attributes
442-
raise AttributeError(
443-
f"'{self.__class__.__name__}' object has no attribute '{name}'"
444-
)
441+
# Avoids infinite recursion of private attributes
442+
if __debug__: # Only execute in non-optimized mode
443+
if name.startswith("_"):
444+
raise AttributeError(
445+
f"'{self.__class__.__name__}' object has no attribute '{name}'"
446+
)
445447
return {agentset: getattr(agentset, name) for agentset in self._agentsets}
446448

447449
@overload

mesa_frames/concrete/model.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,17 @@ def agents(self) -> AgentsDF:
175175
try:
176176
return self._agents
177177
except AttributeError:
178-
raise ValueError(
179-
"You haven't called super().__init__() in your model. Make sure to call it in your __init__ method."
180-
)
178+
if __debug__: # Only execute in non-optimized mode
179+
raise RuntimeError(
180+
"You haven't called super().__init__() in your model. Make sure to call it in your __init__ method."
181+
)
181182

182183
@agents.setter
183184
def agents(self, agents: AgentsDF) -> None:
184-
if not isinstance(agents, AgentsDF):
185-
raise TypeError("agents must be an instance of AgentsDF")
185+
if __debug__: # Only execute in non-optimized mode
186+
if not isinstance(agents, AgentsDF):
187+
raise TypeError("agents must be an instance of AgentsDF")
188+
186189
self._agents = agents
187190

188191
@property

0 commit comments

Comments
 (0)