From 2126a55638a256203e6c82359bb6f074a08bc395 Mon Sep 17 00:00:00 2001 From: VsevolodX <79542055+VsevolodX@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:47:15 -0700 Subject: [PATCH 1/2] update: remove need for initialization --- src/py/mat3ra/standata/base.py | 47 ++++++++++++++++------------- src/py/mat3ra/standata/materials.py | 9 ++---- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/py/mat3ra/standata/base.py b/src/py/mat3ra/standata/base.py index a66a989..c0d86b1 100644 --- a/src/py/mat3ra/standata/base.py +++ b/src/py/mat3ra/standata/base.py @@ -156,15 +156,13 @@ class Config: filesMapByName: StandataFilesMapByName = StandataFilesMapByName() standataConfig: StandataConfig = StandataConfig() - def __init__(self, data: Dict, **kwargs): + def __init__(self, data: Dict): """ Initializes StandataData from raw data. Args: data: Dictionary with keys for filesMapByName and standataConfig. """ - super().__init__( - filesMapByName=self._initialize_files_map(data), standataConfig=self._initialize_config(data), **kwargs - ) + super().__init__(filesMapByName=self._initialize_files_map(data), standataConfig=self._initialize_config(data)) @staticmethod def _initialize_files_map(data: Dict) -> StandataFilesMapByName: @@ -188,37 +186,44 @@ def _initialize_config(data: Dict) -> StandataConfig: ) -class Standata(BaseModel): - data: StandataData +class Standata: + data_dict: Dict = {} + data: StandataData = StandataData(data_dict) + + @classmethod + def get_as_list(cls) -> List[dict]: + return list(cls.data.filesMapByName.dictionary.values()) - def __init__(self, data_dict: Dict, **kwargs): + @classmethod + def get_by_name(cls, name: str) -> List[dict]: """ - Initialize the Standata class with StandataData. + Returns entities by name regex. + Args: - data: Dictionary containing the data to initialize StandataData. + name: Name of the entity. """ - super().__init__(data=StandataData(data_dict), **kwargs) - - def get_as_list(self) -> List[dict]: - return list(self.data.filesMapByName.dictionary.values()) + matching_filenames = cls.data.standataConfig.get_filenames_by_regex(name) + return cls.data.filesMapByName.get_objects_by_filenames(matching_filenames) - def get_by_name(self, name: str) -> List[dict]: + @classmethod + def get_by_name_first_match(cls, name: str) -> dict: """ - Returns entities by name regex. + Returns the first entity that matches the name regex. Args: name: Name of the entity. """ - matching_filenames = self.data.standataConfig.get_filenames_by_regex(name) - return self.data.filesMapByName.get_objects_by_filenames(matching_filenames) + matching_filenames = cls.data.standataConfig.get_filenames_by_regex(name) + return cls.data.filesMapByName.get_objects_by_filenames(matching_filenames)[0] - def get_by_categories(self, *tags: str) -> List[dict]: + @classmethod + def get_by_categories(cls, *tags: str) -> List[dict]: """ Finds entities that match all specified category tags. Args: *tags: Category tags for the entity query. """ - categories = self.data.standataConfig.convert_tags_to_categories_list(*tags) - matching_filenames = self.data.standataConfig.get_filenames_by_categories(*categories) - return self.data.filesMapByName.get_objects_by_filenames(matching_filenames) + 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) diff --git a/src/py/mat3ra/standata/materials.py b/src/py/mat3ra/standata/materials.py index d3c642c..8331fff 100644 --- a/src/py/mat3ra/standata/materials.py +++ b/src/py/mat3ra/standata/materials.py @@ -1,12 +1,9 @@ from typing import Dict -from .base import Standata +from .base import Standata, StandataData from .data.materials import materials_data class Materials(Standata): - def __init__(self, data: Dict = materials_data): - """ - Initialize the Materials class with specific materials data. - """ - super().__init__(data_dict=data) + data_dict: Dict = materials_data + data: StandataData = StandataData(data_dict) From 7ae619a75c32363076cf3c2059ec717fea2dc3db Mon Sep 17 00:00:00 2001 From: VsevolodX <79542055+VsevolodX@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:47:31 -0700 Subject: [PATCH 2/2] chore: adjust tsts --- tests/py/unit/materials/test_standata_init.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/tests/py/unit/materials/test_standata_init.py b/tests/py/unit/materials/test_standata_init.py index a1c60b4..d426e54 100644 --- a/tests/py/unit/materials/test_standata_init.py +++ b/tests/py/unit/materials/test_standata_init.py @@ -3,24 +3,22 @@ def test_standata_materials_init(): """Materials class is initialized with data based on materials_data""" - std_materials = Materials() - assert std_materials.data.standataConfig.entities is not None - assert len(std_materials.data.standataConfig.entities) >= 1 - assert isinstance(std_materials.data.filesMapByName.dictionary, dict) - assert len(std_materials.data.filesMapByName.dictionary) >= 1 + assert Materials.data.standataConfig.entities is not None + assert len(Materials.data.standataConfig.entities) >= 1 + assert isinstance(Materials.data.filesMapByName.dictionary, dict) + assert len(Materials.data.filesMapByName.dictionary) >= 1 def test_categories_data(): """Category map has at least one group of tags.""" - std_materials = Materials() - assert std_materials.data.standataConfig.categories is not None + std_materials = Materials + assert Materials.data.standataConfig.categories is not None assert len(std_materials.data.standataConfig.categories) >= 1 def test_get_by_name(): """Assert correct information if found about a material.""" - std_materials = Materials() - material = std_materials.get_by_name("Graphene")[0] + material = Materials.get_by_name_first_match("Graphene") assert type(material) == dict assert material["name"] == "C, Graphene, HEX (P6/mmm) 2D (Monolayer), 2dm-3993" assert material["isNonPeriodic"] is False @@ -28,7 +26,6 @@ def test_get_by_name(): def test_get_by_categories(): """Assert correct information if found about a material.""" - std_materials = Materials() - material = std_materials.get_by_categories("2D") + material = Materials.get_by_categories("2D") assert isinstance(material, list) assert material[0]["name"] == "C, Graphene, HEX (P6/mmm) 2D (Monolayer), 2dm-3993"