Skip to content

Commit

Permalink
mob next [ci-skip] [ci skip] [skip ci]
Browse files Browse the repository at this point in the history
lastFile:src/skore/storage/non_persistent_storage.py
  • Loading branch information
thomass-dev committed Sep 9, 2024
1 parent f10e5c6 commit 29e3ba4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
18 changes: 16 additions & 2 deletions src/skore/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,27 @@ def put(self, key: str, value: Any):
self.put_item(key, i)

def put_item(self, key: str, item: Item):
self.storage.setitem(key, (item.raw_class_name, item.serialized))
self.storage.setitem(
key,
(
item.raw_class_name,
item.serialized,
item.media_type,
)
)

def get(self, key: str) -> Any:
return self.get_item(key).raw

def get_item(self, key: str) -> Item:
return deserialize(*self.storage.getitem(key))

raw_class_name, serialized, media_type = *self.storage.getitem(key)

return deserialize(
raw_class_name,
serialized,
media_type,
)

def list_keys(self) -> List[str]:
"""List all keys in the project."""
Expand Down
17 changes: 8 additions & 9 deletions src/skore/storage/non_persistent_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
from skore.storage.storage import Storage

if TYPE_CHECKING:
from typing import Generator
from typing import Generator, Any

from skore.item import Item
from skore.storage.storage import URI


Expand All @@ -19,15 +18,15 @@ class NonPersistentStorage(Storage):
def __init__(self, *, content: dict = None):
self.content = content or {}

def __contains__(self, key: URI) -> bool:
def __contains__(self, key: str) -> bool:
"""Return True if the storage has the specified key, else False."""
return key in self.content

def __iter__(self) -> Generator[URI, None, None]:
def __iter__(self) -> Generator[str, None, None]:
"""Yield the keys."""
yield from self.content.keys()

def getitem(self, key: URI) -> Item:
def getitem(self, key: str) -> Any:
"""Return the item for te specified key.
Raises
Expand All @@ -37,11 +36,11 @@ def getitem(self, key: URI) -> Item:
"""
return self.content[key]

def setitem(self, key: URI, item: Item):
def setitem(self, key: str, item: Any):
"""Set the item for the specified key."""
self.content[key] = item

def delitem(self, key: URI):
def delitem(self, key: str):
"""Delete the specified key and its item.
Raises
Expand All @@ -51,10 +50,10 @@ def delitem(self, key: URI):
"""
del self.content[key]

def keys(self) -> Generator[URI, None, None]:
def keys(self) -> Generator[str, None, None]:
"""Yield the keys."""
yield from self

def items(self) -> Generator[tuple[URI, Item], None, None]:
def items(self) -> Generator[tuple[str, Any], None, None]:
"""Yield the pairs (key, item)."""
yield from self.content.items()
9 changes: 8 additions & 1 deletion tests/unit/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def savefig(*args, **kwargs):
)


def test_project(monkeypatch):
def test_project_here(monkeypatch):
def savefig(*args, **kwargs):
return ""

Expand Down Expand Up @@ -192,3 +192,10 @@ def savefig(*args, **kwargs):
"mpl_figure": ("matplotlib.figure.Figure", ""),
"rf_model": ("sklearn.base.BaseEstimator", '{"skops": "", "html": ""}'),
}

assert project.get("string_item") == "Hello, World!"
assert project.get("int_item") == 42
assert project.get("float_item") == 3.14
assert project.get("bool_item") == True
assert project.get("list_item") == [1, 2, 3]
assert project.get("dict_item") == {"key": "value"}

0 comments on commit 29e3ba4

Please sign in to comment.