Skip to content

declearn.utils.ObjectConfig

Dataclass to wrap a JSON-serializable object configuration.

Attributes:

Name Type Description
name str

Key to retrieve the object's type constructor, typically from registered types (see declearn.utils.access_registered).

group str or None

Optional name of the group under which the object's type is registered (see declearn.utils.access_registered).

config dict[str, any]

JSON-serializable dict containing the object's config, that enables recreating it using type(obj).from_config(config).

Source code in declearn/utils/_serialize.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@dataclasses.dataclass
class ObjectConfig:
    """Dataclass to wrap a JSON-serializable object configuration.

    Attributes
    ----------
    name: str
        Key to retrieve the object's type constructor, typically from
        registered types (see `declearn.utils.access_registered`).
    group: str or None
        Optional name of the group under which the object's type is
        registered (see `declearn.utils.access_registered`).
    config: dict[str, any]
        JSON-serializable dict containing the object's config, that
        enables recreating it using `type(obj).from_config(config)`.
    """

    name: str
    group: Optional[str]
    config: Dict[str, Any]

    def to_dict(self) -> ObjectConfigDict:
        """Return a dict representation of this ObjectConfig."""
        return dataclasses.asdict(self)  # type: ignore

    def to_json(self, path: str) -> None:
        """Save this ObjectConfig to a JSON file."""
        json_dump(self.to_dict(), path, indent=2)

    @classmethod
    def from_json(cls, path: str) -> Self:
        """Restore an ObjectConfig from a JSON file."""
        config = json_load(path)
        return cls(**config)

from_json(path) classmethod

Restore an ObjectConfig from a JSON file.

Source code in declearn/utils/_serialize.py
75
76
77
78
79
@classmethod
def from_json(cls, path: str) -> Self:
    """Restore an ObjectConfig from a JSON file."""
    config = json_load(path)
    return cls(**config)

to_dict()

Return a dict representation of this ObjectConfig.

Source code in declearn/utils/_serialize.py
67
68
69
def to_dict(self) -> ObjectConfigDict:
    """Return a dict representation of this ObjectConfig."""
    return dataclasses.asdict(self)  # type: ignore

to_json(path)

Save this ObjectConfig to a JSON file.

Source code in declearn/utils/_serialize.py
71
72
73
def to_json(self, path: str) -> None:
    """Save this ObjectConfig to a JSON file."""
    json_dump(self.to_dict(), path, indent=2)