Skip to content

Commit

Permalink
states: Don't require a name
Browse files Browse the repository at this point in the history
simplifies the API
  • Loading branch information
EwoutH committed Dec 13, 2024
1 parent badc3a3 commit 102dd70
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions mesa/experimental/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
class State:

Check warning on line 22 in mesa/experimental/states.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/states.py#L22

Added line #L22 was not covered by tests
"""Base class for all states."""

def __init__(self, name: str, initial_value: Any):
def __init__(self, initial_value: Any):

Check warning on line 25 in mesa/experimental/states.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/states.py#L25

Added line #L25 was not covered by tests
"""Create a new state."""
self.name = name
self._value = initial_value
self._last_update_time = 0
self.model = None # Set when state is added to agent

Check warning on line 29 in mesa/experimental/states.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/states.py#L27-L29

Added lines #L27 - L29 were not covered by tests
Expand Down Expand Up @@ -61,12 +60,11 @@ class ContinuousState(State):

def __init__(

Check warning on line 61 in mesa/experimental/states.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/states.py#L61

Added line #L61 was not covered by tests
self,
name: str,
initial_value: float,
rate_function: Callable[[float, float], float],
):
"""Create a new continuous state."""
super().__init__(name, initial_value)
super().__init__(initial_value)
self.rate_function = rate_function

Check warning on line 68 in mesa/experimental/states.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/states.py#L67-L68

Added lines #L67 - L68 were not covered by tests

@property
Expand All @@ -89,14 +87,13 @@ class CompositeState(State):

def __init__(

Check warning on line 88 in mesa/experimental/states.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/states.py#L88

Added line #L88 was not covered by tests
self,
name: str,
dependent_states: list[State],
computation_function: Callable[..., Any],
):
"""Create a new composite state."""
self.dependent_states = dependent_states
self.computation_function = computation_function
super().__init__(name, None) # Value computed on first access
super().__init__(None) # Value computed on first access

Check warning on line 96 in mesa/experimental/states.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/states.py#L94-L96

Added lines #L94 - L96 were not covered by tests

@property
def value(self) -> Any:

Check warning on line 99 in mesa/experimental/states.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/states.py#L98-L99

Added lines #L98 - L99 were not covered by tests
Expand Down Expand Up @@ -144,11 +141,6 @@ def __setattr__(self, name: str, value: Any) -> None:
states = object.__getattribute__(self, "states")

Check warning on line 141 in mesa/experimental/states.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/states.py#L141

Added line #L141 was not covered by tests
# If setting a State object, add or update the states dictionary
if isinstance(value, State):
# The state's name should match the attribute name
if value.name != name:
raise ValueError(
f"State name '{value.name}' does not match attribute name '{name}'"
)
states[name] = value
value.model = self.model

Check warning on line 145 in mesa/experimental/states.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/states.py#L144-L145

Added lines #L144 - L145 were not covered by tests
else:
Expand Down

0 comments on commit 102dd70

Please sign in to comment.