Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 16, 2024
1 parent 2f7920c commit 5f44b59
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
17 changes: 14 additions & 3 deletions pyiron_base/interfaces/has_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
from typing import Any

from pyiron_base.interfaces.has_hdf import HasHDF
from pyiron_base.storage.hdfio import DummyHDFio, _extract_module_class_name, _import_class
from pyiron_base.storage.hdfio import (
DummyHDFio,
_extract_module_class_name,
_import_class,
)

__author__ = "Jan Janssen"
__copyright__ = (
Expand All @@ -32,6 +36,7 @@
__status__ = "production"
__date__ = "Dec 20, 2023"


def create_from_dict(obj_dict):
"""
Create and restores an object previously written as a dictionary.
Expand All @@ -43,7 +48,9 @@ def create_from_dict(obj_dict):
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!")
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)
Expand All @@ -52,6 +59,7 @@ def create_from_dict(obj_dict):
obj.from_dict(obj_dict, version)
return obj


class HasDict(ABC):
__dict_version__ = "0.1.0"

Expand All @@ -63,9 +71,12 @@ 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")):
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
Expand Down
4 changes: 1 addition & 3 deletions pyiron_base/jobs/job/extension/executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ def _from_dict(self, obj_dict, version=None):
]
executable_class_dict = {}
# Backwards compatibility; dict state used to be nested one level deeper
if "executable" in obj_dict.keys() and isinstance(
obj_dict["executable"], dict
):
if "executable" in obj_dict.keys() and isinstance(obj_dict["executable"], dict):
obj_dict = obj_dict["executable"]
for key in data_container_keys:
executable_class_dict[key] = obj_dict.get(key, None)
Expand Down
2 changes: 1 addition & 1 deletion pyiron_base/storage/datacontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ def to(v):
def _to_dict(self):
return {"data": self.to_builtin(), "READ_ONLY": self.read_only}

def _from_dict(self, obj_dict, version = None):
def _from_dict(self, obj_dict, version=None):
with self.unlocked():
self.clear()
self.update(obj_dict["data"], wrap=True)
Expand Down

0 comments on commit 5f44b59

Please sign in to comment.