Create a data class instance from a dictionary.
:param data_class: a data class type
:param data: a dictionary of a input data
:param config: a configuration of the creation process
:return: an instance of a data class
Source code in wt_ml/output/artifacts/_utils.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 | def dataclass_from_dict(data_class: type[D], data: Data) -> D:
"""Create a data class instance from a dictionary.
:param data_class: a data class type
:param data: a dictionary of a input data
:param config: a configuration of the creation process
:return: an instance of a data class
"""
init_values = {}
for field in get_fields(data_class):
if field.name in data:
field_data = data[field.name]
value = _create_field_value(field, field_data)
else:
value = get_default_value_for_field(field)
init_values[field.name] = value
instance = data_class(**init_values)
return instance
|