i want to know if particular date (in this example today's date) is holiday somwhere in the world. if yes - i would like to create list with tupple, in each tuple - (holiday name, where in the world). if it is not holiday anywhere - empty list. i tried to import holidays but i need to run on each country like in this example: anyone has something more efficient?
from datetime import date
import holidays
listOfHolidays = []
for ptr in holidays.ISR(years=date.today().year).items():
if date.today() == ptr[0]:
listOfHolidays.append(tuple((ptr[1], "ISRAEL")))
for ptr in holidays.US(years=date.today().year).items():
if date.today() == ptr[0]:
listOfHolidays.append((tuple(ptr[1], "US")))
for ptr in holidays.UK(years=date.today().year).items():
if date.today() == ptr[0]:
listOfHolidays.append((tuple(ptr[1], "UK")))
for ptr in holidays.CHN(years=date.today().year).items():
if date.today() == ptr[0]:
listOfHolidays.append((tuple(ptr[1], "CHN")))
print(listOfHolidays)
Due to how the
holidayspackage is structured, you will indeed have to iterate through all the supported regions. The functionholidays.list_supported_countriescan help you with this. The good news is that you can do direct lookup in a country object without having to manually search through the dict items:You can write this as a comprehension in versions of python that support the walrus operator:
The problem is that there will be a lot of duplicates here, because almost every supported country has multiple ways to reference it. This is also an issue because you do not want to generate the same list over and over on the fly.
On inspecting the library, I find that the most reliable way to check if the country class is "real" is to check if it introduces any new holidays via the
_populatemethod. You can add the condition for that with:Or as a comprehension:
Finally, the country code is always given as the name when you use the
countryattribute. If you want something more legible, I find it's better to use the name of the class that implements the_populatemethod. For the second loop, replace(holiday, country.country)withFor the second list comprehension, replace
(c[target], c.country)with