Skip to content

Commit 215aece

Browse files
delate code type arg and other
1 parent 7205ee7 commit 215aece

File tree

3 files changed

+42
-62
lines changed

3 files changed

+42
-62
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Removed:
7171

7272
### Added
7373

74-
- `climada.entity.impact_funcs.trop_cyclone.ImpfSetTropCyclone.get_countries_per_region` function [#1034](https://github.com/CLIMADA-project/climada_python/pull/1034)
74+
- `climada.entity.impact_funcs.trop_cyclone.ImpfSetTropCyclone.get_impf_id_regions_per_countries` function [#1034](https://github.com/CLIMADA-project/climada_python/pull/1034)
7575
- `climada.hazard.tc_tracks.TCTracks.subset_years` function [#1023](https://github.com/CLIMADA-project/climada_python/pull/1023)
7676
- `climada.hazard.tc_tracks.TCTracks.from_FAST` function, add Australia basin (AU) [#993](https://github.com/CLIMADA-project/climada_python/pull/993)
7777
- Add `osm-flex` package to CLIMADA core [#981](https://github.com/CLIMADA-project/climada_python/pull/981)

climada/entity/impact_funcs/test/test_tc.py

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -168,46 +168,28 @@ def test_get_countries_per_region(self):
168168
self.assertListEqual(out[2], [124, 840])
169169
self.assertListEqual(out[3], ["CAN", "USA"])
170170

171-
def test_get_region_per_countries(self):
171+
def test_get_imf_id_regions_per_countries(self):
172172
"""Test get_impf_id_regions_per_countries()"""
173173
ifs = ImpfSetTropCyclone()
174-
out = ifs.get_impf_id_regions_per_countries(
175-
countries=["CHE"], code_type="ISO3A"
176-
)
177-
self.assertEqual(out[0][0], 10)
178-
self.assertEqual(out[1][0], "ROW")
179-
self.assertEqual(out[2][0], "Rest of The World")
180-
out = ifs.get_impf_id_regions_per_countries(countries=[756], code_type="ISO3N")
181-
self.assertEqual(out[0][0], 10)
182-
self.assertEqual(out[1][0], "ROW")
183-
self.assertEqual(out[2][0], "Rest of The World")
184-
with self.assertRaises(ValueError) as context:
185-
ImpfSetTropCyclone.get_impf_id_regions_per_countries(
186-
countries=["SWE"], code_type="invalid_code"
187-
)
188-
self.assertEqual(
189-
str(context.exception), "code_type must be either 'iso3a' or 'iso3n'"
190-
)
191-
with self.assertRaises(ValueError) as context:
192-
ImpfSetTropCyclone.get_impf_id_regions_per_countries(
193-
countries=["SWE", 840], code_type="ISO3A"
194-
)
195-
self.assertEqual(
196-
str(context.exception), "All elements in the list must be of the same type."
197-
)
198-
with self.assertRaises(ValueError) as context:
199-
ImpfSetTropCyclone.get_impf_id_regions_per_countries(
200-
countries=[840, 124], code_type="ISO3A"
201-
)
202-
self.assertEqual(
203-
str(context.exception), "ISO3A code type cannot have integer values."
174+
impf_id_reg_id_reg_name = ifs.get_impf_id_regions_per_countries(
175+
countries=["CHE"]
204176
)
177+
178+
# the first element of impf_id_reg_id_reg_name [0] is the impact function id,
179+
# the second [1] is the region id, the third [2] is the region name.
180+
self.assertEqual(impf_id_reg_id_reg_name[0][0], 10)
181+
self.assertEqual(impf_id_reg_id_reg_name[1][0], "ROW")
182+
self.assertEqual(impf_id_reg_id_reg_name[2][0], "Rest of The World")
183+
out = ifs.get_impf_id_regions_per_countries(countries=[756])
184+
self.assertEqual(impf_id_reg_id_reg_name[0][0], 10)
185+
self.assertEqual(impf_id_reg_id_reg_name[1][0], "ROW")
186+
self.assertEqual(impf_id_reg_id_reg_name[2][0], "Rest of The World")
187+
205188
with self.assertRaises(ValueError) as context:
206-
ImpfSetTropCyclone.get_impf_id_regions_per_countries(
207-
countries=["MEX"], code_type="ISO3N"
208-
)
189+
ImpfSetTropCyclone.get_impf_id_regions_per_countries(countries=[3.22])
209190
self.assertEqual(
210-
str(context.exception), "ISO3N code type cannot have string values."
191+
str(context.exception),
192+
"The element 3.22 is neither in ISO3A nor ISO3N format",
211193
)
212194

213195

climada/entity/impact_funcs/trop_cyclone.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
class CountryCode(Enum):
3939
"""
40-
Enum class that links ISO country codes (both iso3a and iso3n) to specific regions and
40+
Enum class that links ISO country codes (both ISO3A and ISO3N) to specific regions and
4141
associated impact function IDs.
4242
4343
Attributes
@@ -458,22 +458,18 @@ def get_countries_per_region(region=None):
458458
)
459459

460460
@staticmethod
461-
def get_impf_id_regions_per_countries(
462-
countries: list = None, code_type: str = "ISO3A"
463-
) -> tuple:
461+
def get_impf_id_regions_per_countries(countries: list = None) -> tuple:
464462
"""Return the impact function id and the region corresponding to a list of countries,
465463
or a single country.
466464
467465
Parameters:
468466
-----------
469467
countries : list
470468
List containing the ISO codes of the country, which should be either
471-
in string format if the code is "ISO3A" or an integer if "ISO3N", see code_type below.
472-
code_type : str
473-
Either "ISO3A" or "ISO3N". "ISO3A" stands for "ISO 3166-1 alpha-3" which is a
474-
three-letter country code, "ISO3N" stands for "ISO 3166-1 numeric" which is a
475-
three-digit country code, the numeric version of "ISO3A". For example, for Switzerland:
476-
the "ISO3A" code is "CHE" and the "ISO3N" is 756.
469+
in string format if the code is "ISO 3166-1 alpha-3" or an integer if the code is
470+
"ISO 3166-1 numeric", which is a three-digit country code, the numeric version of
471+
"ISO 3166-1 alpha-3". For example, for Switzerland: the "ISO 3166-1 alpha-3" code is
472+
"CHE" and the "ISO 3166-1 numeric" is 756.
477473
478474
Returns:
479475
--------
@@ -487,23 +483,25 @@ def get_impf_id_regions_per_countries(
487483
List of the regions names. Example: "Caribbean and Mexico", "USA and Canada", ...
488484
"""
489485

490-
if code_type not in {"ISO3A", "ISO3N"}:
491-
raise ValueError("code_type must be either 'iso3a' or 'iso3n'")
492-
elif not all(isinstance(country, type(countries[0])) for country in countries):
493-
raise ValueError("All elements in the list must be of the same type.")
494-
elif code_type == "ISO3A" and isinstance((countries[0]), int):
495-
raise ValueError("ISO3A code type cannot have integer values.")
496-
elif code_type == "ISO3N" and isinstance((countries[0]), str):
497-
raise ValueError("ISO3N code type cannot have string values.")
498-
499-
region_country_dict = getattr(CountryCode, code_type).value
500486
# Find region
501-
regions_ids = [
502-
region_id
503-
for country in countries
504-
for region_id, countr_in_region_id in region_country_dict.items()
505-
if country in countr_in_region_id
506-
]
487+
regions_ids = []
488+
for country in countries:
489+
490+
if isinstance(country, str):
491+
code_type = "ISO3A"
492+
elif isinstance(country, int):
493+
code_type = "ISO3N"
494+
else:
495+
raise ValueError(
496+
f"The element {country} is neither in ISO3A nor ISO3N format"
497+
)
498+
499+
region_country_dict = getattr(CountryCode, code_type).value
500+
501+
for region_id, countr_in_region_id in region_country_dict.items():
502+
if country in countr_in_region_id:
503+
regions_ids.append(region_id)
504+
507505
# Find impact function id
508506
impf_ids = [CountryCode.IMPF_ID.value[region] for region in regions_ids]
509507
regions_name = [CountryCode.REGION_NAME.value[region] for region in regions_ids]

0 commit comments

Comments
 (0)