Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/getting_started/plot_skore_getting_started.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@
# directory) using :func:`~skore.Project.put`), with a "universal" key-value convention:

# %%
my_project.put("my_int", 3)
my_project.put("df_cv_report_metrics", df_cv_report_metrics)
my_project.put("roc_plot", roc_plot)
my_project.artifacts.my_int = 3
my_project.artifacts.df_cv_report_metrics = df_cv_report_metrics
my_project.artifacts.roc_plot = roc_plot

# %%
# .. note ::
Expand All @@ -282,10 +282,10 @@
# We can retrieve the value of an item:

# %%
my_project.get("my_int")
my_project.artifacts.my_int

# %%
my_project.get("df_cv_report_metrics")
my_project.artifacts.df_cv_report_metrics

# %%
# .. seealso::
Expand Down
56 changes: 56 additions & 0 deletions skore/src/skore/project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,59 @@ def wrapper(self, *args, **kwargs):
return wrapper


class ArtifactMetadata:
"""Handle metadata for project artifacts."""

def __init__(self, project):
self._project = project

def __getattr__(self, key: str):
"""Get metadata for an item."""
return {
"note": self._project.get_note(key),
"display_as": None,
}

def __setattr__(self, key: str, value: dict):
"""Set metadata for an item."""
if key.startswith("_"):
super().__setattr__(key, value)
return

if isinstance(value, dict):
if "note" in value:
self._project.set_note(key, value["note"])
if "display_as" in value:
# TODO: Implement display_as storage
pass
else:
raise TypeError(
"Metadata must be a dictionary with 'note' and/or 'display_as' keys"
)


class Artifacts:
"""Handle attribute-style access to project artifacts."""

def __init__(self, project):
self._project = project

def __getattr__(self, key: str):
"""Get an artifact value."""
try:
return self._project.get(key)
except KeyError as e:
raise AttributeError(f"No artifact named '{key}'") from e

def __setattr__(self, key: str, value: Any):
"""Set an artifact value."""
if key.startswith("_"):
super().__setattr__(key, value)
return

self._project.put(key, value)


class Project:
"""
A collection of items persisted in a storage.
Expand Down Expand Up @@ -107,6 +160,9 @@ def __init__(
self.path = self.path.resolve()
self.name = self.path.name

self.artifacts = Artifacts(project=self)
self.metadata = ArtifactMetadata(project=self)

if if_exists == "raise" and self.path.exists():
raise FileExistsError(
f"Project '{str(path)}' already exists. Set `if_exists` to 'load' to "
Expand Down
Loading