Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify from_csv #165

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions climate_categories/_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nice one!


return conv.hydrate(cats=cats)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# last_update: 2021-07-09
IPCC1996,gas,IPCC2006,comment
0,,0,
1,,1,
1,,1,
2 changes: 1 addition & 1 deletion climate_categories/tests/data/simple_categorisation_a.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ categories:
- C
- CatC
unnumbered:
title: The unnumbered category
title: The unnumbered category
2 changes: 1 addition & 1 deletion climate_categories/tests/data/simple_categorisation_b.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ categories:
comment: The second category
alternative_codes:
- B
- CatB
- CatB
16 changes: 10 additions & 6 deletions climate_categories/tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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}

Expand All @@ -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"
Expand Down
Loading