-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[minor] Recursive to/from_dict #1602
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,11 @@ | |
from typing import Any | ||
|
||
from pyiron_base.interfaces.has_hdf import HasHDF | ||
from pyiron_base.storage.hdfio import DummyHDFio | ||
from pyiron_base.storage.hdfio import ( | ||
DummyHDFio, | ||
_extract_module_class_name, | ||
_import_class, | ||
) | ||
|
||
__author__ = "Jan Janssen" | ||
__copyright__ = ( | ||
|
@@ -33,11 +37,50 @@ | |
__date__ = "Dec 20, 2023" | ||
|
||
|
||
def create_from_dict(obj_dict): | ||
""" | ||
Create and restores an object previously written as a dictionary. | ||
|
||
Args: | ||
obj_dict (dict): must be the output of HasDict.to_dict() | ||
|
||
Returns: | ||
object: restored object | ||
""" | ||
if "TYPE" not in obj_dict: | ||
raise ValueError( | ||
"invalid obj_dict! must contain type information and be the output of HasDict.to_dict!" | ||
) | ||
type_field = obj_dict["TYPE"] | ||
module_path, class_name = _extract_module_class_name(type_field) | ||
class_object = _import_class(module_path, class_name) | ||
version = obj_dict.get("VERSION", None) | ||
obj = class_object.instantiate(obj_dict, version) | ||
obj.from_dict(obj_dict, version) | ||
return obj | ||
|
||
|
||
class HasDict(ABC): | ||
__dict_version__ = "0.1.0" | ||
|
||
@abstractmethod | ||
@classmethod | ||
def instantiate(cls, obj_dict: dict, version: str = None) -> "Self": | ||
return cls() | ||
|
||
def from_dict(self, obj_dict: dict, version: str = None): | ||
def load(inner_dict): | ||
if not isinstance(inner_dict, dict): | ||
return inner_dict | ||
if not all( | ||
k in inner_dict for k in ("NAME", "TYPE", "OBJECT", "DICT_VERSION") | ||
): | ||
return {k: load(v) for k, v in inner_dict.items()} | ||
return create_from_dict(inner_dict) | ||
|
||
self._from_dict({k: load(v) for k, v in obj_dict.items()}, version) | ||
|
||
@abstractmethod | ||
def _from_dict(self, obj_dict: dict, version: str = None): | ||
pass | ||
|
||
@abstractmethod | ||
|
@@ -66,7 +109,17 @@ def _type_to_dict(self): | |
return type_dict | ||
|
||
def to_dict(self): | ||
return self._to_dict() | self._type_to_dict() | ||
type_dict = self._type_to_dict() | ||
data_dict = {} | ||
child_dict = {} | ||
for k, v in self._to_dict().items(): | ||
if isinstance(v, HasDict): | ||
child_dict[k] = v.to_dict() | ||
elif isinstance(v, HasHDF): | ||
child_dict[k] = HasDictfromHDF.to_dict(v) | ||
else: | ||
data_dict[k] = v | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to clarify this, this part currently does not work for the |
||
return data_dict | self._join_children_dict(child_dict) | type_dict | ||
|
||
@staticmethod | ||
def _join_children_dict(children: dict[str, dict[str, Any]]) -> dict[str, Any]: | ||
|
@@ -114,12 +167,21 @@ class HasDictfromHDF(HasDict, HasHDF): | |
their children to implmement it. | ||
""" | ||
|
||
def from_dict(self, obj_dict: dict, version: str = None): | ||
@classmethod | ||
def instantiate(cls, obj_dict: dict, version: str = None) -> "Self": | ||
hdf = DummyHDFio(None, "/", obj_dict) | ||
return cls(**cls.from_hdf_args(hdf)) | ||
|
||
def _from_dict(self, obj_dict: dict, version: str = None): | ||
# DummyHDFio(project=None) looks a bit weird, but it was added there | ||
# only to support saving/loading jobs which already use the HasDict | ||
# interface | ||
hdf = DummyHDFio(None, "/", obj_dict) | ||
self.from_hdf(hdf, group_name=self._get_hdf_group_name()) | ||
group_name = self._get_hdf_group_name() | ||
if group_name is not None: | ||
hdf = DummyHDFio(None, "/", {group_name: obj_dict}) | ||
else: | ||
hdf = DummyHDFio(None, "/", obj_dict) | ||
self.from_hdf(hdf) | ||
|
||
def _to_dict(self): | ||
hdf = DummyHDFio(None, "/") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1174,14 +1174,14 @@ def _to_dict(self): | |
data_dict["files_to_compress"] = self._files_to_remove | ||
return data_dict | ||
|
||
def from_dict(self, job_dict): | ||
self._type_from_dict(type_dict=job_dict) | ||
if "import_directory" in job_dict.keys(): | ||
self._import_directory = job_dict["import_directory"] | ||
self._server.from_dict(server_dict=job_dict["server"]) | ||
if "executable" in job_dict.keys() and job_dict["executable"] is not None: | ||
self.executable.from_dict(job_dict["executable"]) | ||
input_dict = job_dict["input"] | ||
def _from_dict(self, obj_dict, version=None): | ||
self._type_from_dict(type_dict=obj_dict) | ||
if "import_directory" in obj_dict.keys(): | ||
self._import_directory = obj_dict["import_directory"] | ||
self._server = obj_dict["server"] | ||
if "executable" in obj_dict.keys() and obj_dict["executable"] is not None: | ||
self._executable = obj_dict["executable"] | ||
Comment on lines
+1182
to
+1183
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a bit surprised about this part, when is the dictionary of the executable converted to the executable object? Is this already happening when reading the executable from the HDF5 file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this new setup HasDict.from_dict converts nested objects back from their dictionary form before the implementations HasDict._from_dict is called. So by the time GenericJob._from_dict runs and looks for the executable Executable._from_dict already ran. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In principle the reverse also works, i.e. it's not always necessary anymore to call to_dict on ones children that are returned in the obj_dict. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it makes sense to document this at least in the |
||
input_dict = obj_dict["input"] | ||
if "generic_dict" in input_dict.keys(): | ||
generic_dict = input_dict["generic_dict"] | ||
self._restart_file_list = generic_dict["restart_file_list"] | ||
|
@@ -1242,7 +1242,7 @@ def from_hdf(self, hdf=None, group_name=None): | |
exe_dict = self._hdf5["executable/executable"].to_object().to_builtin() | ||
exe_dict["READ_ONLY"] = self._hdf5["executable/executable/READ_ONLY"] | ||
job_dict["executable"] = {"executable": exe_dict} | ||
self.from_dict(job_dict=job_dict) | ||
self.from_dict(obj_dict=job_dict) | ||
|
||
def save(self): | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I understand that the conversion is handled here, still I am wondering if this is what we want. I thought the intention was to get towards using
__getstate__()
and__setstate__()
. Now with the executable and the server object using datacalsses for data storage internally, I was hoping that we could use a simple dictionary representation there where the class is stored by storing the attached dataclass.