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/project.py
  • Loading branch information
rouk1 committed Sep 9, 2024
1 parent f627423 commit c648fdb
Showing 1 changed file with 47 additions and 90 deletions.
137 changes: 47 additions & 90 deletions src/skore/project.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,53 @@
""""""

import abc
import json
from abc import abstractmethod
from pathlib import Path
from typing import Any, List, Protocol

import pandas
import pandas as pd


def get_filepath_from_project_name(project_name) -> Path:
return None


class Item(abc.ABC):
"""An item in the project."""

@property
@abstractmethod
def value(self) -> Any:
"""Get the value of the item."""
pass


class JSONItem(Item):
"""An item that is a JSON serializable Python object."""

def __init__(self, value):
self.__raw = value
self.transformed = json.dumps(value)

@property
def value(self):
return self.__raw


class DataFrameItem(Item):
"""An item that is a pandas or polars data frame."""

def __init__(self, value):
self.__raw = None
self.transformed = value.to_json(orient="split")

@property
def value(self):
return pandas.read_json(self.transformed, orient="split")


class Storable(Protocol):
item: Item
class_name: str


def to_storable(value) -> Item:
"""Transform a value into an Item."""
# Get the class of the value as a string
value_class = type(value).__name__

# Map the class name to the corresponding Item subclass
item_class_map = {
"dict": JSONItem,
"list": JSONItem,
"tuple": JSONItem,
"int": JSONItem,
"float": JSONItem,
"str": JSONItem,
"bool": JSONItem,
"DataFrame": DataFrameItem,
# TODO: Add
# polars.DataFrame DataFrameItem
# numpy.ndarray NumpyArrayItem
# matplotlib.figure.Figure MediaItem (as SVG)
# PIL.Image.Image MediaItem (as PNG)
# altair.Chart MediaItem (as Vega-Lite JSON)
# bytes + media type MediaItem
# str + media type MediaItem
# Scikit-learn model ScikitLearnModelItem
}

# Check if the value's class is in our mapping
if value_class in item_class_map:
return Storable(
item=item_class_map[value_class](value),
class_name=type(value_class).__name__,
)
else:
return Storable(item=Item(value), class_name="Item")


def from_storable(storable: Storable) -> Item:
return storable.class_name #FIXME eval ?? : )
from dataclasses import dataclass
from typing import Any, List


@dataclass(frozen=True)
class Item:
"""An item is a value that is stored in the project."""

raw: Any = None
raw_class_name: str | None = None
transformed: Any = None


def get_fully_qualified_class_name(obj):
"""
Get the fully qualified class name of an object.
This function returns the fully qualified class name of the given object,
including the module name if available.
Parameters
----------
obj : object
Any Python object.
Returns
-------
str
The fully qualified class name as a string.
"""
module = obj.__class__.__module__
if module is None or module == str.__class__.__module__:
return obj.__class__.__name__
return module + "." + obj.__class__.__name__


def transform(o: Any) -> Item:
"""Transform an object into an item."""
try:
serialized = json.dumps(o)
except AttributeError:
value_class_name = None
match value_class_name:
case "DataFrame":
return Item(raw=o, raw_class_name=value_class_name, transformed=o.to_dict())
case _:
return Item(raw=o, raw_class_name=value_class_name)


class Project:
Expand Down

0 comments on commit c648fdb

Please sign in to comment.