From f3afffc59abd920f012fd6ac8dbe06b9b3d481d6 Mon Sep 17 00:00:00 2001 From: VsevolodX <79542055+VsevolodX@users.noreply.github.com> Date: Wed, 30 Oct 2024 18:39:00 -0700 Subject: [PATCH 1/3] update: add get by name and cat --- src/py/mat3ra/standata/base.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/py/mat3ra/standata/base.py b/src/py/mat3ra/standata/base.py index 6402b8e..fce4b6e 100644 --- a/src/py/mat3ra/standata/base.py +++ b/src/py/mat3ra/standata/base.py @@ -220,3 +220,25 @@ def get_by_categories(cls, *tags: str) -> List[dict]: categories = cls.data.standataConfig.convert_tags_to_categories_list(*tags) matching_filenames = cls.data.standataConfig.get_filenames_by_categories(*categories) return cls.data.filesMapByName.get_objects_by_filenames(matching_filenames) + + @classmethod + def get_by_name_and_categories(cls, name: str, *tags: str) -> dict: + """ + Returns the first entity that matches both the name regex and all categories. + + Args: + name: Name to match with regex + *tags: Category tags to match + + Returns: + First matching entity + """ + categories = cls.data.standataConfig.convert_tags_to_categories_list(*tags) + category_matches = cls.data.standataConfig.get_filenames_by_categories(*categories) + name_matches = cls.data.standataConfig.get_filenames_by_regex(name) + matching_filenames = [f for f in name_matches if f in category_matches] + + if not matching_filenames: + raise ValueError(f"No matches found for name '{name}' and categories {tags}") + + return cls.data.filesMapByName.get_objects_by_filenames(matching_filenames)[0] From 6d734c8b9c81ade4fa891e58aade27d44d6271ea Mon Sep 17 00:00:00 2001 From: VsevolodX <79542055+VsevolodX@users.noreply.github.com> Date: Wed, 30 Oct 2024 18:41:19 -0700 Subject: [PATCH 2/3] chore: add a test --- tests/py/unit/test_materials_data.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/py/unit/test_materials_data.py b/tests/py/unit/test_materials_data.py index 351ebd3..9eed5c2 100644 --- a/tests/py/unit/test_materials_data.py +++ b/tests/py/unit/test_materials_data.py @@ -38,3 +38,11 @@ def test_get_material_data(): assert type(material) == dict assert material["name"] == "C, Graphene, HEX (P6/mmm) 2D (Monolayer), 2dm-3993" assert material["isNonPeriodic"] is False + + +def test_get_by_name_and_categories(): + """Assert correct information if found about a material.""" + material = Materials.get_by_name_and_categories("MoS2", "2D") + assert type(material) == dict + assert material["name"] == "MoS2, Molybdenum Disulfide, HEX (P-6m2) 2D (Monolayer), 2dm-3150" + assert material["isNonPeriodic"] is False From ca9e72cd37255db079be8b5c6e9b567a75991a70 Mon Sep 17 00:00:00 2001 From: Timur Bazhirov Date: Fri, 1 Nov 2024 18:22:06 -0700 Subject: [PATCH 3/3] Update test_materials_data.py --- tests/py/unit/test_materials_data.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/py/unit/test_materials_data.py b/tests/py/unit/test_materials_data.py index 9eed5c2..46296a5 100644 --- a/tests/py/unit/test_materials_data.py +++ b/tests/py/unit/test_materials_data.py @@ -18,7 +18,6 @@ def test_categories_data(): def test_get_by_name(): - """Assert correct information if found about a material.""" material = Materials.get_by_name_first_match("Graphene") assert type(material) == dict assert material["name"] == "C, Graphene, HEX (P6/mmm) 2D (Monolayer), 2dm-3993" @@ -26,14 +25,12 @@ def test_get_by_name(): def test_get_by_categories(): - """Assert correct information if found about a material.""" material = Materials.get_by_categories("2D") assert isinstance(material, list) assert material[0]["name"] == "C, Graphene, HEX (P6/mmm) 2D (Monolayer), 2dm-3993" def test_get_material_data(): - """Assert correct information if found about a material.""" material = materials_data["filesMapByName"]["C-[Graphene]-HEX_[P6%2Fmmm]_2D_[Monolayer]-[2dm-3993].json"] assert type(material) == dict assert material["name"] == "C, Graphene, HEX (P6/mmm) 2D (Monolayer), 2dm-3993" @@ -41,7 +38,6 @@ def test_get_material_data(): def test_get_by_name_and_categories(): - """Assert correct information if found about a material.""" material = Materials.get_by_name_and_categories("MoS2", "2D") assert type(material) == dict assert material["name"] == "MoS2, Molybdenum Disulfide, HEX (P-6m2) 2D (Monolayer), 2dm-3150"