Skip to content

Commit

Permalink
feat: recursive state casting (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
rilshok committed Jun 24, 2024
2 parents a5f06c2 + 30865e6 commit ca5637d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/iokit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"save_file",
"save_temp",
]
__version__ = "0.1.1"
__version__ = "0.1.2"

from .extensions import Gzip, Json, Jsonl, Tar, Txt
from .state import State, filter_states, find_state
Expand Down
19 changes: 12 additions & 7 deletions src/iokit/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"find_state",
]

from contextlib import suppress
from datetime import datetime
from fnmatch import fnmatch
from io import BytesIO
from typing import Any, Generator, Iterable
from typing import Any, Generator, Iterable, Type

from humanize import naturalsize
from typing_extensions import Self
Expand Down Expand Up @@ -126,13 +127,17 @@ def __repr__(self) -> str:
size = naturalsize(self.size, gnu=True)
return f"{self.name} ({size})"

@classmethod
def by_suffix(cls, suffix: str) -> Type[Self]:
if suffix in cls._suffixes:
return cls
for klass in cls.__subclasses__():
with suppress(ValueError):
return klass.by_suffix(suffix)
raise ValueError(f"Unknown state suffix {suffix}")

def cast(self) -> "State":
suffix = self.name.suffix
for klass in State.__subclasses__():
if suffix in getattr(klass, "_suffixes"):
break
else:
raise ValueError(f"Unknown state suffix {suffix}")
klass = self.by_suffix(self.name.suffix)
state = klass.__new__(klass)
setattr(state, "_data", self.data)
setattr(state, "_name", self.name)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_state_inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from iokit import Json, State


class MyJson(Json, suffix="myjson"):
pass


def test_state_inheritance_json() -> None:
assert getattr(MyJson, "_suffix") == "myjson"
assert getattr(MyJson, "_suffixes") == ("myjson",)
myjson = MyJson({"a": 1}, name="test")
assert myjson.name == "test.myjson"
loaded = State(data=myjson.data, name="test.myjson")
assert loaded.load() == myjson.load() == {"a": 1}

3 comments on commit ca5637d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/iokit
   state.py1192182%32–33, 40, 51, 56–59, 83–85, 91, 94, 105–107, 115, 127–128, 149, 162
src/iokit/extensions
   tar.py28293%27, 30
   txt.py9189%9
src/iokit/storage
   local.py29486%32–33, 35–36
TOTAL2542889% 

Tests Skipped Failures Errors Time
21 0 💤 0 ❌ 0 🔥 0.414s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/iokit
   state.py1192182%32–33, 40, 51, 56–59, 83–85, 91, 94, 105–107, 115, 127–128, 149, 162
src/iokit/extensions
   tar.py28293%27, 30
   txt.py9189%9
src/iokit/storage
   local.py29486%32–33, 35–36
TOTAL2542889% 

Tests Skipped Failures Errors Time
21 0 💤 0 ❌ 0 🔥 0.266s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/iokit
   state.py1192182%32–33, 40, 51, 56–59, 83–85, 91, 94, 105–107, 115, 127–128, 149, 162
src/iokit/extensions
   tar.py28293%27, 30
   txt.py9189%9
src/iokit/storage
   local.py29486%32–33, 35–36
TOTAL2542889% 

Tests Skipped Failures Errors Time
21 0 💤 0 ❌ 0 🔥 0.281s ⏱️

Please sign in to comment.