From 265117384bf324252d66deca509ea52be0037017 Mon Sep 17 00:00:00 2001 From: Leonardo Schwarz Date: Tue, 12 Nov 2024 14:28:25 +0100 Subject: [PATCH] code to load packaged files --- .../panel/standardize_input_panel.py | 9 +++++++++ .../unit/to_be_migrated/test_standardize_input_panel.py | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/src/depiction_targeted_preproc/panel/standardize_input_panel.py b/src/depiction_targeted_preproc/panel/standardize_input_panel.py index 3dd2e73..80e9307 100644 --- a/src/depiction_targeted_preproc/panel/standardize_input_panel.py +++ b/src/depiction_targeted_preproc/panel/standardize_input_panel.py @@ -1,4 +1,8 @@ +from __future__ import annotations + import polars as pl +import yaml +from pathlib import Path from pydantic import BaseModel @@ -9,6 +13,11 @@ class StandardizeConfig(BaseModel): select_columns: list[str] default_values: dict[str, str] + @classmethod + def load_packaged(cls, name: str) -> StandardizeConfig: + path = Path(__file__).parent / "standardize" / name + return cls.model_validate(yaml.safe_load(path.read_text())) + def _identify_column_correspondence(config: StandardizeConfig, raw_df: pl.DataFrame) -> dict[str, str]: """Identifies the correspondence between the columns in the raw dataframe and the standardized columns, diff --git a/tests/unit/to_be_migrated/test_standardize_input_panel.py b/tests/unit/to_be_migrated/test_standardize_input_panel.py index f8ba904..fd5d3a7 100644 --- a/tests/unit/to_be_migrated/test_standardize_input_panel.py +++ b/tests/unit/to_be_migrated/test_standardize_input_panel.py @@ -21,3 +21,8 @@ def test_standardize(config: StandardizeConfig) -> None: {"mass": [1, 2, 3], "label": ["a", "b", "c"], "type": ["something", "something", "something"]} ) pl.testing.assert_frame_equal(result, expected_df) + + +def test_config_load_packaged() -> None: + config = StandardizeConfig.load_packaged("standardize_main_config.yml") + assert config.select_columns == ["mass", "label", "type"]