From 2ab20c5feadafa055bb92c7b21a27cacd90693ed Mon Sep 17 00:00:00 2001 From: ~Jhellico Date: Fri, 25 Aug 2023 19:43:34 +0300 Subject: [PATCH] Add categories support to subdivision holidays methods (#1446) --- holidays/holiday_base.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/holidays/holiday_base.py b/holidays/holiday_base.py index b6e58a169..473a87b6e 100644 --- a/holidays/holiday_base.py +++ b/holidays/holiday_base.py @@ -648,14 +648,19 @@ def _add_holiday(self, name: str, *args) -> Optional[date]: self[dt] = self.tr(name) return dt - def _add_subdiv_holidays(self): - """Populate subdivision holidays.""" + def _add_subdiv_category_holidays(self, category: str = None): + """Populate subdivision holidays by category.""" if self.subdiv is not None: subdiv = self.subdiv.replace("-", "_").replace(" ", "_").lower() - add_subdiv_holidays = getattr(self, f"_add_subdiv_{subdiv}_holidays", None) + method_name = f"_add_subdiv_{subdiv}{f'_{category}' if category else ''}_holidays" + add_subdiv_holidays = getattr(self, method_name, None) if add_subdiv_holidays and callable(add_subdiv_holidays): add_subdiv_holidays() + def _add_subdiv_holidays(self): + """Populate subdivision holidays.""" + self._add_subdiv_category_holidays() + def _add_substituted_holidays(self): """Populate substituted holidays.""" if len(self.substituted_holidays) == 0: @@ -758,6 +763,9 @@ def _populate_categories(self): if populate_category_holidays and callable(populate_category_holidays): populate_category_holidays() + # Populate subdivision holidays for all categories. + self._add_subdiv_category_holidays(category) + def append(self, *args: Union[Dict[DateLike, str], List[DateLike], DateLike]) -> None: """Alias for :meth:`update` to mimic list type.""" return self.update(*args)