Combine more holidays

Closes: #84
This commit is contained in:
Edward Betts 2023-12-15 12:01:50 +00:00
parent fb62dffa5c
commit 33359c165e

View file

@ -136,6 +136,17 @@ async def bristol_waste_collection_events(
def combine_holidays(holidays: list[Holiday]) -> list[Event]: def combine_holidays(holidays: list[Holiday]) -> list[Event]:
"""Combine UK and US holidays with the same date and title.""" """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[ combined: collections.defaultdict[
tuple[date, str], set[str] tuple[date, str], set[str]
] = collections.defaultdict(set) ] = collections.defaultdict(set)
@ -143,12 +154,21 @@ def combine_holidays(holidays: list[Holiday]) -> list[Event]:
for h in holidays: for h in holidays:
assert isinstance(h.name, str) and isinstance(h.date, date) 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) combined[event_key].add(h.country)
events: list[Event] = [] events: list[Event] = []
for (d, name), countries in combined.items(): 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) e = Event(name="holiday", date=d, title=title)
events.append(e) events.append(e)