diff options
author | Nao Pross <np@0hm.ch> | 2024-03-02 21:53:07 +0100 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2024-03-02 21:53:07 +0100 |
commit | 93621f28e140a36cc2e2fd8cc7e615564c03e795 (patch) | |
tree | e17f6d49cba01651dbdc6285782f1938c4edfcc1 | |
parent | Remove IndexError (already exists in standard library) (diff) | |
download | mdpoly-93621f28e140a36cc2e2fd8cc7e615564c03e795.tar.gz mdpoly-93621f28e140a36cc2e2fd8cc7e615564c03e795.zip |
Make state.State a dataclass
Also, the members were (incorrectly) not instance variables
-rw-r--r-- | mdpoly/state.py | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/mdpoly/state.py b/mdpoly/state.py index 1ba44fe..739ec7a 100644 --- a/mdpoly/state.py +++ b/mdpoly/state.py @@ -1,5 +1,6 @@ from __future__ import annotations from typing import TYPE_CHECKING +from dataclasses import dataclass, field if TYPE_CHECKING: from .types import Number @@ -9,9 +10,10 @@ if TYPE_CHECKING: Index = int +@dataclass class State: - variables: dict[Var, Index] = {} - parameters: dict[Param, Number] = {} + variables: dict[Var, Index] = field(default_factory=dict) + parameters: dict[Param, Number] = field(default_factory=dict) _last_index: Index = -1 def _make_index(self) -> Index: @@ -28,6 +30,9 @@ class State: return self.variables[var] - - + def from_index(self, index: Index) -> Var: + for var, idx in self.variables.items(): + if idx == index: + return var + raise IndexError(f"There is no variable with index {index}.") |