From a0857ca4445d50bdcf67c276afe0d0d9edf47df0 Mon Sep 17 00:00:00 2001 From: Andrea Albino Date: Mon, 2 Dec 2024 17:42:07 +0100 Subject: [PATCH 1/4] allow merge_section to merge children classes --- src/nomad_measurements/utils.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/nomad_measurements/utils.py b/src/nomad_measurements/utils.py index 58389fca..bd5675f6 100644 --- a/src/nomad_measurements/utils.py +++ b/src/nomad_measurements/utils.py @@ -100,19 +100,19 @@ def merge_sections( # noqa: PLR0912 logger.warning(warning) else: print(warning) - for name, sub_section_def in update.m_def.all_sub_sections.items(): - count = section.m_sub_section_count(sub_section_def) + for name, _ in update.m_def.all_sub_sections.items(): + count = section.m_sub_section_count(name) if count == 0: - for update_sub_section in update.m_get_sub_sections(sub_section_def): - section.m_add_sub_section(sub_section_def, update_sub_section) - elif count == update.m_sub_section_count(sub_section_def): + for update_sub_section in update.m_get_sub_sections(name): + section.m_add_sub_section(name, update_sub_section) + elif count == update.m_sub_section_count(name): for i in range(count): merge_sections( - section.m_get_sub_section(sub_section_def, i), - update.m_get_sub_section(sub_section_def, i), + section.m_get_sub_section(name, i), + update.m_get_sub_section(name, i), logger, ) - elif update.m_sub_section_count(sub_section_def) > 0: + elif update.m_sub_section_count(name) > 0: warning = ( f'Merging sections with different number of "{name}" sub sections.' ) From 6da204640a34bba3175dd978abc5238a05dd818e Mon Sep 17 00:00:00 2001 From: Andrea Albino Date: Mon, 2 Dec 2024 18:28:49 +0100 Subject: [PATCH 2/4] check for same class among all_base_sections --- src/nomad_measurements/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nomad_measurements/utils.py b/src/nomad_measurements/utils.py index bd5675f6..6267e8f8 100644 --- a/src/nomad_measurements/utils.py +++ b/src/nomad_measurements/utils.py @@ -84,7 +84,7 @@ def merge_sections( # noqa: PLR0912 if section is None: section = update.m_copy() return - if not isinstance(section, type(update)): + if update.m_def not in section.m_def.all_base_sections: raise TypeError( 'Cannot merge sections of different types: ' f'{type(section)} and {type(update)}' From 3ee7944661b1f6bc72c297f1fa2cdc421d0a0128 Mon Sep 17 00:00:00 2001 From: Andrea Albino <95371554+aalbino2@users.noreply.github.com> Date: Tue, 3 Dec 2024 11:37:45 +0100 Subject: [PATCH 3/4] Update src/nomad_measurements/utils.py Co-authored-by: Sarthak Kapoor <57119427+ka-sarthak@users.noreply.github.com> --- src/nomad_measurements/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nomad_measurements/utils.py b/src/nomad_measurements/utils.py index 6267e8f8..9448ea56 100644 --- a/src/nomad_measurements/utils.py +++ b/src/nomad_measurements/utils.py @@ -84,7 +84,7 @@ def merge_sections( # noqa: PLR0912 if section is None: section = update.m_copy() return - if update.m_def not in section.m_def.all_base_sections: + if update.m_def in [section.m_def, *section.m_def.all_base_sections]: raise TypeError( 'Cannot merge sections of different types: ' f'{type(section)} and {type(update)}' From 56ea767c6ccac4bd0c6beae090536f656ed7f98a Mon Sep 17 00:00:00 2001 From: Andrea Albino <95371554+aalbino2@users.noreply.github.com> Date: Tue, 3 Dec 2024 11:51:40 +0100 Subject: [PATCH 4/4] added not Co-authored-by: Sarthak Kapoor <57119427+ka-sarthak@users.noreply.github.com> --- src/nomad_measurements/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nomad_measurements/utils.py b/src/nomad_measurements/utils.py index 9448ea56..849f90ec 100644 --- a/src/nomad_measurements/utils.py +++ b/src/nomad_measurements/utils.py @@ -84,7 +84,7 @@ def merge_sections( # noqa: PLR0912 if section is None: section = update.m_copy() return - if update.m_def in [section.m_def, *section.m_def.all_base_sections]: + if update.m_def not in [section.m_def, *section.m_def.all_base_sections]: raise TypeError( 'Cannot merge sections of different types: ' f'{type(section)} and {type(update)}'