Automation Parameter serialization type name

This commit is contained in:
allegroai 2020-06-19 19:09:27 +03:00
parent 0626d807a7
commit 9f9452ecf4

View File

@ -37,6 +37,7 @@ class Parameter(RandomSeed):
""" """
The base hyper-parameter optimization object. The base hyper-parameter optimization object.
""" """
_class_type_serialize_name = 'type'
def __init__(self, name): def __init__(self, name):
# type: (Optional[str]) -> () # type: (Optional[str]) -> ()
@ -79,7 +80,7 @@ class Parameter(RandomSeed):
:return: dict representation of the object (serialization). :return: dict representation of the object (serialization).
""" """
serialize = {'type.': str(self.__class__).split('.')[-1][:-2]} serialize = {self._class_type_serialize_name: str(self.__class__).split('.')[-1][:-2]}
# noinspection PyCallingNonCallable # noinspection PyCallingNonCallable
serialize.update(dict(((k, v.to_dict() if hasattr(v, 'to_dict') else v) for k, v in self.__dict__.items()))) serialize.update(dict(((k, v.to_dict() if hasattr(v, 'to_dict') else v) for k, v in self.__dict__.items())))
return serialize return serialize
@ -93,7 +94,7 @@ class Parameter(RandomSeed):
:return: The Parameter object. :return: The Parameter object.
""" """
a_dict = a_dict.copy() a_dict = a_dict.copy()
a_cls = a_dict.pop('type.', None) a_cls = a_dict.pop(cls._class_type_serialize_name, None)
if not a_cls: if not a_cls:
return None return None
try: try:
@ -101,8 +102,9 @@ class Parameter(RandomSeed):
except AttributeError: except AttributeError:
return None return None
instance = a_cls.__new__(a_cls) instance = a_cls.__new__(a_cls)
instance.__dict__ = dict((k, cls.from_dict(v) if isinstance(v, dict) and 'type.' in v else v) instance.__dict__ = dict(
for k, v in a_dict.items()) (k, cls.from_dict(v) if isinstance(v, dict) and cls._class_type_serialize_name in v else v)
for k, v in a_dict.items())
return instance return instance