From 4f1ef103a841cc3cbec9d6203a42e3aab9238c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=20Pfl=C3=BCger?= Date: Wed, 23 Oct 2024 10:48:53 +0200 Subject: [PATCH 1/2] refactor: simplify from_csv --- climate_categories/_conversions.py | 35 +++++++++--------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/climate_categories/_conversions.py b/climate_categories/_conversions.py index 4f5a9db..73b55af 100644 --- a/climate_categories/_conversions.py +++ b/climate_categories/_conversions.py @@ -67,9 +67,9 @@ def hydrate( auxiliary_categories_hydrated = {} for aux_categorization_name, categories in self.auxiliary_categories.items(): aux_categorization = cats[aux_categorization_name] - auxiliary_categories_hydrated[ - aux_categorization - ] = self._hydrate_handle_errors(categories, aux_categorization) + auxiliary_categories_hydrated[aux_categorization] = ( + self._hydrate_handle_errors(categories, aux_categorization) + ) return ConversionRule( factors_categories_a=self._hydrate_handle_errors( @@ -87,14 +87,12 @@ def hydrate( @typing.overload def _hydrate_handle_errors( self, to_hydrate: dict[str, int], categorization: "Categorization" - ) -> dict["Category", int]: - ... + ) -> dict["Category", int]: ... @typing.overload def _hydrate_handle_errors( self, to_hydrate: set[str], categorization: "Categorization" - ) -> set["Category"]: - ... + ) -> set["Category"]: ... def _hydrate_handle_errors( self, @@ -901,29 +899,16 @@ def __init__( @staticmethod def from_csv( - filepath: typing.Union[str, pathlib.Path, typing.TextIO], - cats : typing.Union[dict[str, "Categorization"], None] = None, + filepath: typing.Union[str, pathlib.Path, typing.TextIO], + cats: typing.Union[dict[str, "Categorization"], None] = None, ) -> "Conversion": """Read conversion from comma-separated-values file and add categorizations.""" - conv = ConversionSpec.from_csv(filepath) - def get_cats(cat_names): - import climate_categories - - return { - cat_name: climate_categories.cats[cat_name] for cat_name in cat_names - } - if not cats: - cat_names = [ - conv.categorization_a_name, - conv.categorization_b_name, - ] - - if conv.auxiliary_categorizations_names: - cat_names += conv.auxiliary_categorizations_names + if cats is None: + import climate_categories - cats = get_cats(cat_names) + cats = climate_categories.cats return conv.hydrate(cats=cats) From c545090d03f49b969b7e8c2b1e9b5b389878f778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=20Pfl=C3=BCger?= Date: Wed, 23 Oct 2024 10:50:15 +0200 Subject: [PATCH 2/2] lint: ruff --- .../example_conversion.IPCC1996.IPCC2006.csv | 2 +- .../tests/data/simple_categorisation_a.yaml | 2 +- .../tests/data/simple_categorisation_b.yaml | 2 +- climate_categories/tests/test_conversions.py | 16 ++++++++++------ 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/climate_categories/tests/data/example_conversion.IPCC1996.IPCC2006.csv b/climate_categories/tests/data/example_conversion.IPCC1996.IPCC2006.csv index d68acba..1343a1e 100644 --- a/climate_categories/tests/data/example_conversion.IPCC1996.IPCC2006.csv +++ b/climate_categories/tests/data/example_conversion.IPCC1996.IPCC2006.csv @@ -3,4 +3,4 @@ # last_update: 2021-07-09 IPCC1996,gas,IPCC2006,comment 0,,0, -1,,1, \ No newline at end of file +1,,1, diff --git a/climate_categories/tests/data/simple_categorisation_a.yaml b/climate_categories/tests/data/simple_categorisation_a.yaml index 1656c97..beef553 100644 --- a/climate_categories/tests/data/simple_categorisation_a.yaml +++ b/climate_categories/tests/data/simple_categorisation_a.yaml @@ -32,4 +32,4 @@ categories: - C - CatC unnumbered: - title: The unnumbered category \ No newline at end of file + title: The unnumbered category diff --git a/climate_categories/tests/data/simple_categorisation_b.yaml b/climate_categories/tests/data/simple_categorisation_b.yaml index 35751f9..05e1dc0 100644 --- a/climate_categories/tests/data/simple_categorisation_b.yaml +++ b/climate_categories/tests/data/simple_categorisation_b.yaml @@ -24,4 +24,4 @@ categories: comment: The second category alternative_codes: - B - - CatB \ No newline at end of file + - CatB diff --git a/climate_categories/tests/test_conversions.py b/climate_categories/tests/test_conversions.py index fd88872..0f018ef 100644 --- a/climate_categories/tests/test_conversions.py +++ b/climate_categories/tests/test_conversions.py @@ -433,9 +433,7 @@ def get_test_data_filepath(fname: str): def test_read_csv_in_conversion_class(): - fd = get_test_data_filepath( - "example_conversion.IPCC1996.IPCC2006.csv" - ) + fd = get_test_data_filepath("example_conversion.IPCC1996.IPCC2006.csv") conv_from_conversion_spec = conversions.ConversionSpec.from_csv(fd) conv_from_conversion_spec = conv_from_conversion_spec.hydrate( @@ -448,9 +446,13 @@ def test_read_csv_in_conversion_class(): def test_read_conversion_from_csv_with_custom_categorizations(): - categorisation_a = climate_categories.from_yaml(get_test_data_filepath("simple_categorisation_a.yaml")) + categorisation_a = climate_categories.from_yaml( + get_test_data_filepath("simple_categorisation_a.yaml") + ) - categorisation_b = climate_categories.from_yaml(get_test_data_filepath("simple_categorisation_b.yaml")) + categorisation_b = climate_categories.from_yaml( + get_test_data_filepath("simple_categorisation_b.yaml") + ) cats = {"A": categorisation_a, "B": categorisation_b} @@ -473,7 +475,9 @@ def test_read_conversion_from_csv_with_existing_categorizations(): def test_read_conversion_from_csv_with_existing_categorizations_aux_dims(): conv = climate_categories.Conversion.from_csv( - get_test_data_filepath("test_conversion_with_existing_categorizations_aux_dims.csv") + get_test_data_filepath( + "test_conversion_with_existing_categorizations_aux_dims.csv" + ) ) assert conv.categorization_a_name == "BURDI"