From 017e09f6003507778d99831387d81a24ee636277 Mon Sep 17 00:00:00 2001 From: "Vladislav A. Proskurov" Date: Wed, 4 Sep 2024 20:53:31 +0400 Subject: [PATCH 1/2] perf: load only if there's a loader --- src/iokit/state.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/iokit/state.py b/src/iokit/state.py index 1998784..8a20786 100644 --- a/src/iokit/state.py +++ b/src/iokit/state.py @@ -134,20 +134,26 @@ def _by_suffix(cls, suffix: str) -> Type[Self]: for kls in cls.__subclasses__(): with suppress(ValueError): return kls._by_suffix(suffix) - return cls + raise ValueError(f"Unknown state suffix '{suffix}'") def cast(self) -> "State": - klass = self._by_suffix(self.name.suffix) - state = klass.__new__(klass) - setattr(state, "_data", self.data) - setattr(state, "_name", self.name) - setattr(state, "_time", self.time) - return state + with suppress(ValueError): + klass = self._by_suffix(self.name.suffix) + state = klass.__new__(klass) + setattr(state, "_data", self.data) + setattr(state, "_name", self.name) + setattr(state, "_time", self.time) + return state + return self def load(self) -> Any: if not self.name.suffix: return self.data.getvalue() - return self.cast().load() + state = self.cast() + if type(state) is State: # pylint: disable=unidiomatic-typecheck + msg = f"Cannot load state with suffix '{self.name.suffix}'" + raise NotImplementedError(msg) + return state.load() def filter_states(states: Iterable[State], pattern: str) -> Generator[State, None, None]: From 902087635222a21b0d7bf3a3f777fcee19936ac3 Mon Sep 17 00:00:00 2001 From: "Vladislav A. Proskurov" Date: Wed, 4 Sep 2024 20:55:07 +0400 Subject: [PATCH 2/2] feat: v0.1.6 --- src/iokit/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/iokit/__init__.py b/src/iokit/__init__.py index 30090e6..360245d 100644 --- a/src/iokit/__init__.py +++ b/src/iokit/__init__.py @@ -15,7 +15,7 @@ "save_file", "save_temp", ] -__version__ = "0.1.5" +__version__ = "0.1.6" from .extensions import Dat, Env, Gzip, Json, Jsonl, Tar, Txt, Yaml from .state import State, filter_states, find_state