From 3077dcd9d61b1ce47bd3c4fabb2aa97d91642cca Mon Sep 17 00:00:00 2001 From: ~Jhellico Date: Wed, 29 Nov 2023 00:06:04 +0200 Subject: [PATCH] Add subdivisions support --- holidays/holiday_base.py | 25 +++++++++++-------------- holidays/observed_holiday_base.py | 4 ++-- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/holidays/holiday_base.py b/holidays/holiday_base.py index ab680959e..f19f4b517 100644 --- a/holidays/holiday_base.py +++ b/holidays/holiday_base.py @@ -676,13 +676,10 @@ def _add_substituted_holidays(self): substituted_label = self.tr(self.substituted_label) substituted_date_format = self.tr(self.substituted_date_format) - substituted_holidays_mapping_names = ["substituted_holidays"] - # Check category specific special observed holidays. - for category in sorted(self.categories): - substituted_holidays_mapping_names.append(f"substituted_{category}_holidays") - - for mapping_name in substituted_holidays_mapping_names: - for hol in _normalize_tuple(getattr(self, mapping_name, {}).get(self._year, ())): + for mapping_name in self._get_static_holiday_mapping_names(): + for hol in _normalize_tuple( + getattr(self, f"substituted_{mapping_name}", {}).get(self._year, ()) + ): from_year = hol[0] if len(hol) == 5 else self._year from_month, from_day, to_month, to_day = hol[-4:] from_date = date(from_year, from_month, from_day).strftime(substituted_date_format) @@ -758,20 +755,20 @@ def _populate(self, year: int) -> None: # Populate substituted holidays. self._add_substituted_holidays() - def _get_special_holiday_mapping_names(self): + def _get_static_holiday_mapping_names(self): # Check for general special holidays. - mapping_names = ["special_holidays"] + mapping_names = ["holidays"] # Check subdivision specific special holidays. if self.subdiv is not None: subdiv = self.subdiv.replace("-", "_").replace(" ", "_").lower() - mapping_names.append(f"special_{subdiv}_holidays") + mapping_names.append(f"{subdiv}_holidays") # Check category specific special holidays (both general and per subdivision). for category in sorted(self.categories): - mapping_names.append(f"special_{category}_holidays") + mapping_names.append(f"{category}_holidays") if self.subdiv is not None: - mapping_names.append(f"special_{subdiv}_{category}_holidays") + mapping_names.append(f"{subdiv}_{category}_holidays") return mapping_names @@ -779,9 +776,9 @@ def _add_special_holidays(self): if not hasattr(self, "_has_special"): return None - for mapping_name in self._get_special_holiday_mapping_names(): + for mapping_name in self._get_static_holiday_mapping_names(): for month, day, name in _normalize_tuple( - getattr(self, mapping_name, {}).get(self._year, ()) + getattr(self, f"special_{mapping_name}", {}).get(self._year, ()) ): self._add_holiday(name, date(self._year, month, day)) diff --git a/holidays/observed_holiday_base.py b/holidays/observed_holiday_base.py index 80cf9ae12..3b8c28c25 100644 --- a/holidays/observed_holiday_base.py +++ b/holidays/observed_holiday_base.py @@ -169,9 +169,9 @@ def _add_special_holidays(self): if not self.observed: return None - for mapping_name in self._get_special_holiday_mapping_names(): + for mapping_name in self._get_static_holiday_mapping_names(): for month, day, name in _normalize_tuple( - getattr(self, f"{mapping_name}_observed", {}).get(self._year, ()) + getattr(self, f"special_{mapping_name}_observed", {}).get(self._year, ()) ): self._add_holiday( self.tr(self.observed_label) % self.tr(name), date(self._year, month, day)