From 33359c165ed1089fd92df297c0cbf23a9a5c9166 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 15 Dec 2023 12:01:50 +0000 Subject: [PATCH] Combine more holidays Closes: #84 --- agenda/data.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/agenda/data.py b/agenda/data.py index c8f7acd..5b4a394 100644 --- a/agenda/data.py +++ b/agenda/data.py @@ -136,6 +136,17 @@ async def bristol_waste_collection_events( def combine_holidays(holidays: list[Holiday]) -> list[Event]: """Combine UK and US holidays with the same date and title.""" + + all_countries = {h.country for h in holidays} + + standard_name = { + (1, 1): "New Year's Day", + (1, 6): "Epiphany", + (12, 8): "Immaculate conception", + (12, 25): "Christmas Day", + (12, 26): "Boxing Day", + } + combined: collections.defaultdict[ tuple[date, str], set[str] ] = collections.defaultdict(set) @@ -143,12 +154,21 @@ def combine_holidays(holidays: list[Holiday]) -> list[Event]: for h in holidays: assert isinstance(h.name, str) and isinstance(h.date, date) - event_key = (h.date, h.name) + event_key = (h.date, standard_name.get((h.date.month, h.date.day), h.name)) combined[event_key].add(h.country) events: list[Event] = [] for (d, name), countries in combined.items(): - title = f'{name} ({", ".join(country.upper() for country in countries)})' + if len(countries) == len(all_countries): + country_list = "" + elif len(countries) < len(all_countries) / 2: + country_list = ", ".join(sorted(country.upper() for country in countries)) + else: + country_list = "not " + ", ".join( + sorted(country.upper() for country in all_countries - set(countries)) + ) + + title = f"{name} ({country_list})" e = Event(name="holiday", date=d, title=title) events.append(e)