Commit 410d8d97 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added include_derived option to m_to_dict in metainfo.

parent 8ec6944e
Pipeline #90925 canceled with stages
in 7 minutes and 58 seconds
...@@ -1152,7 +1152,9 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas ...@@ -1152,7 +1152,9 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
return self.m_def == definition or definition in self.m_def.all_base_sections return self.m_def == definition or definition in self.m_def.all_base_sections
def m_to_dict( def m_to_dict(
self, with_meta: bool = False, include_defaults: bool = False, self, with_meta: bool = False,
include_defaults: bool = False,
include_derived: bool = False,
categories: List[Union['Category', Type['MCategory']]] = None, categories: List[Union['Category', Type['MCategory']]] = None,
partial: TypingCallable[['Definition', 'MSection'], bool] = None) -> Dict[str, Any]: partial: TypingCallable[['Definition', 'MSection'], bool] = None) -> Dict[str, Any]:
''' '''
...@@ -1162,6 +1164,7 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas ...@@ -1162,6 +1164,7 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
with_meta: Include information about the section definition and the sections with_meta: Include information about the section definition and the sections
position in its parent. position in its parent.
include_defaults: Include default values of unset quantities. include_defaults: Include default values of unset quantities.
include_derived: Include values of derived quantities.
categories: A list of category classes or category definitions that is used categories: A list of category classes or category definitions that is used
to filter the included quantities and sub sections. Only applied to to filter the included quantities and sub sections. Only applied to
properties of this section, not on sub-sections. Is overwritten properties of this section, not on sub-sections. Is overwritten
...@@ -1208,7 +1211,7 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas ...@@ -1208,7 +1211,7 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
for category in category_defs) for category in category_defs)
child_partial = lambda *args, **kwargs: True child_partial = lambda *args, **kwargs: True
def serialize_quantity(quantity, is_set): def serialize_quantity(quantity, is_set, is_derived):
quantity_type = quantity.type quantity_type = quantity.type
serialize: TypingCallable[[Any], Any] = str serialize: TypingCallable[[Any], Any] = str
...@@ -1274,6 +1277,8 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas ...@@ -1274,6 +1277,8 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
if is_set: if is_set:
value = self.__dict__[quantity.name] value = self.__dict__[quantity.name]
elif is_derived:
value = quantity.derived(self)
else: else:
value = quantity.default value = quantity.default
...@@ -1300,7 +1305,10 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas ...@@ -1300,7 +1305,10 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
if not partial(quantity, self): if not partial(quantity, self):
continue continue
try:
if quantity.virtual: if quantity.virtual:
if include_derived and quantity.derived is not None:
yield name, serialize_quantity(quantity, False, True)
continue continue
is_set = self.m_is_set(quantity) is_set = self.m_is_set(quantity)
...@@ -1308,8 +1316,7 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas ...@@ -1308,8 +1316,7 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
if not include_defaults or not quantity.m_is_set(Quantity.default): if not include_defaults or not quantity.m_is_set(Quantity.default):
continue continue
try: yield name, serialize_quantity(quantity, is_set, False)
yield name, serialize_quantity(quantity, is_set)
except ValueError as e: except ValueError as e:
import traceback import traceback
...@@ -1325,14 +1332,18 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas ...@@ -1325,14 +1332,18 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
if self.m_sub_section_count(sub_section_def) > 0: if self.m_sub_section_count(sub_section_def) > 0:
yield name, [ yield name, [
None if item is None else item.m_to_dict( None if item is None else item.m_to_dict(
with_meta=with_meta, include_defaults=include_defaults, with_meta=with_meta,
include_defaults=include_defaults,
include_derived=include_derived,
partial=child_partial) partial=child_partial)
for item in self.m_get_sub_sections(sub_section_def)] for item in self.m_get_sub_sections(sub_section_def)]
else: else:
sub_section = self.m_get_sub_section(sub_section_def, -1) sub_section = self.m_get_sub_section(sub_section_def, -1)
if sub_section is not None: if sub_section is not None:
yield name, sub_section.m_to_dict( yield name, sub_section.m_to_dict(
with_meta=with_meta, include_defaults=include_defaults, with_meta=with_meta,
include_defaults=include_defaults,
include_derived=include_derived,
partial=child_partial) partial=child_partial)
return {key: value for key, value in items()} return {key: value for key, value in items()}
......
...@@ -492,6 +492,9 @@ class TestM1: ...@@ -492,6 +492,9 @@ class TestM1:
system.atom_labels = ['H', 'H', 'O'] system.atom_labels = ['H', 'H', 'O']
assert system.n_atoms == 3 assert system.n_atoms == 3
assert 'n_atoms' not in system.m_to_dict()
assert 'n_atoms' in system.m_to_dict(include_derived=True)
pass pass
def test_derived_cached(self): def test_derived_cached(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment