Skip to content

Commit

Permalink
simplify read_csv method
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Busch committed Oct 21, 2024
1 parent 3634389 commit 0bda928
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
35 changes: 18 additions & 17 deletions climate_categories/_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,24 +901,25 @@ def __init__(

@classmethod
def from_csv(cls, filepath):
conv = ConversionSpec.from_csv(filepath)
return 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
# }
#
# cat_names = [
# conv.categorization_a_name,
# conv.categorization_b_name,
# *conv.auxiliary_categorizations_names,
# ]
#
# cats = get_cats(cat_names)
#
# return conv.hydrate(cats=cats)

def get_cats(cat_names):
import climate_categories

return {
cat_name: climate_categories.cats[cat_name] for cat_name in cat_names
}

cat_names = [
conv.categorization_a_name,
conv.categorization_b_name,
*conv.auxiliary_categorizations_names,
]

cats = get_cats(cat_names)

return conv.hydrate(cats=cats)

def reversed(self) -> "Conversion":
"""Returns the Conversion with categorization_a and categorization_b swapped."""
Expand Down
12 changes: 4 additions & 8 deletions climate_categories/tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,13 +429,9 @@ def test_relevant_rules():


def test_read_csv_in_conversion_class():
filepath = importlib.resources.files("climate_categories.data").joinpath(
"conversion.IPCC1996.IPCC2006.csv"
)
conv = conversions.Conversion.from_csv(filepath)

assert conv.categorization_a_name == "IPCC1996"
assert conv.categorization_b_name == "IPCC2006"
conv_from_conversion = conversions.Conversion.from_csv("data/good_conversion.csv")

conv_from_conversion_spec = conversions.ConversionSpec.from_csv("data/good_conversion.csv")

# make sure hydrate() added all the rules
assert len(conv.rules) == 153
assert vars(conv_from_conversion_spec) == vars(conv_from_conversion)

0 comments on commit 0bda928

Please sign in to comment.