Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KJhellico committed Dec 23, 2024
1 parent c0ec0ee commit de94ef7
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 103 deletions.
35 changes: 32 additions & 3 deletions holidays/calendars/hebrew.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,36 @@ class _HebrewLunisolar:
2100: (OCT, 13),
}

@staticmethod
def _get_holiday(holiday: str, year: int) -> Optional[date]:
dt = getattr(_HebrewLunisolar, f"{holiday}_DATES", {}).get(year, ())
def _get_holiday(self, holiday: str, year: int) -> Optional[date]:
dt = getattr(self, f"{holiday}_DATES", {}).get(year, ())
return date(year, *dt) if dt else None

def hanukkah_date(self, year: int) -> set[Optional[date]]:
return {self._get_holiday(HANUKKAH, y) for y in (year - 1, year)}

def israel_independence_date(self, year: int) -> Optional[date]:
return self._get_holiday(INDEPENDENCE_DAY, year)

def lag_baomer_date(self, year: int) -> Optional[date]:
return self._get_holiday(LAG_BAOMER, year)

def passover_date(self, year: int) -> Optional[date]:
return self._get_holiday(PASSOVER, year)

def purim_date(self, year: int) -> Optional[date]:
return self._get_holiday(PURIM, year)

def rosh_hashanah_date(self, year: int) -> Optional[date]:
return self._get_holiday(ROSH_HASHANAH, year)

def shavuot_date(self, year: int) -> Optional[date]:
return self._get_holiday(SHAVUOT, year)

def sukkot_date(self, year: int) -> Optional[date]:
return self._get_holiday(SUKKOT, year)

def tisha_bav_date(self, year: int) -> Optional[date]:
return self._get_holiday(TISHA_BAV, year)

def yom_kippur_date(self, year: int) -> Optional[date]:
return self._get_holiday(YOM_KIPPUR, year)
64 changes: 38 additions & 26 deletions holidays/countries/israel.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Israel(ObservedHolidayBase, HebrewCalendarHolidays):
start_year = 1948

def __init__(self, *args, **kwargs):
HebrewCalendarHolidays.__init__(self)
kwargs.setdefault("observed_rule", FRI_TO_PREV_THU + SAT_TO_PREV_THU)
super().__init__(*args, **kwargs)

Expand All @@ -56,81 +57,92 @@ def _add_observed(self, dt, name, rule):

def _populate_public_holidays(self):
# Rosh Hashanah (New Year).
self._add_hebrew_holiday(tr("ראש השנה"), self._rosh_hashanah_date, range(2))
self._add_rosh_hashanah(tr("ראש השנה"), range(2))

# Yom Kippur (Day of Atonement).
self._add_hebrew_holiday(tr("יום כיפור"), self._yom_kippur_date)
self._add_yom_kippur(tr("יום כיפור"))

# Sukkot (Feast of Tabernacles).
self._add_hebrew_holiday(tr("סוכות"), self._sukkot_date)
self._add_sukkot(tr("סוכות"))
# Simchat Torah / Shemini Atzeret.
self._add_hebrew_holiday(tr("שמחת תורה/שמיני עצרת"), self._sukkot_date, +7)
self._add_sukkot(tr("שמחת תורה/שמיני עצרת"), +7)

# Pesach (Passover).
self._add_hebrew_holiday(tr("פסח"), self._passover_date)
self._add_passover(tr("פסח"))
# Shvi'i shel Pesach (Seventh day of Passover)
self._add_hebrew_holiday(tr("שביעי של פסח"), self._passover_date, +6)
self._add_passover(tr("שביעי של פסח"), +6)

rule = FRI_TO_PREV_THU + SAT_TO_PREV_THU
if self._year >= 2004:
rule += MON_TO_NEXT_TUE
# Yom Ha-Atzmaut (Independence Day).
self._add_observed(self._israel_independence_date, tr("יום העצמאות"), rule)
self._add_observed(
self._hebrew_calendar.israel_independence_date(self._year),
# Yom Ha-Atzmaut (Independence Day).
tr("יום העצמאות"),
rule,
)

# Shavuot.
self._add_hebrew_holiday(tr("שבועות"), self._shavuot_date)
self._add_shavuot(tr("שבועות"))

def _populate_optional_holidays(self):
# Chol HaMoed Sukkot (Feast of Tabernacles holiday).
self._add_hebrew_holiday(tr("חול המועד סוכות"), self._sukkot_date, range(1, 6))
self._add_sukkot(tr("חול המועד סוכות"), range(1, 6))

if self._year >= 2008:
# Sigd.
self._add_hebrew_holiday(tr("סיגד"), self._yom_kippur_date, +49)
self._add_yom_kippur(tr("סיגד"), +49)

# Purim.
self._add_hebrew_holiday(tr("פורים"), self._purim_date)
self._add_purim(tr("פורים"))

# Chol HaMoed Pesach (Passover holiday).
self._add_hebrew_holiday(tr("חול המועד פסח"), self._passover_date, range(1, 6))
self._add_passover(tr("חול המועד פסח"), range(1, 6))

if self._year >= 1963:
rule = THU_TO_PREV_WED + FRI_TO_PREV_WED
if self._year >= 2004:
rule += SUN_TO_NEXT_MON
self._add_observed(
_timedelta(self._israel_independence_date, -1),
_timedelta(self._hebrew_calendar.israel_independence_date(self._year), -1),
# Yom Hazikaron (Fallen Soldiers and Victims of Terrorism Remembrance Day).
tr("יום הזיכרון לחללי מערכות ישראל ונפגעי פעולות האיבה"),
rule,
)

if self._year >= 1998:
# Yom Yerushalayim (Jerusalem Day).
self._add_hebrew_holiday(tr("יום ירושלים"), self._lag_baomer_date, +10)
self._add_lag_baomer(tr("יום ירושלים"), +10)

# Tisha B'Av (Tisha B'Av, fast).
self._add_observed(self._tisha_bav_date, tr("תשעה באב"), SAT_TO_NEXT_SUN)
self._add_observed(
self._hebrew_calendar.tisha_bav_date(self._year),
# Tisha B'Av (Tisha B'Av, fast).
tr("תשעה באב"),
SAT_TO_NEXT_SUN,
)

def _populate_school_holidays(self):
# Chol HaMoed Sukkot (Feast of Tabernacles holiday).
self._add_hebrew_holiday(tr("חול המועד סוכות"), self._sukkot_date, range(1, 6))
self._add_sukkot(tr("חול המועד סוכות"), range(1, 6))

for hanukkah_dt in self._hanukkah_date:
# Hanukkah.
self._add_hebrew_holiday(tr("חנוכה"), hanukkah_dt, range(8))
# Hanukkah.
self._add_hanukkah(tr("חנוכה"), range(8))

# Ta`anit Ester (Fast of Esther).
self._add_observed(_timedelta(self._purim_date, -1), tr("תענית אסתר"), SAT_TO_PREV_THU)
self._add_observed(
_timedelta(self._hebrew_calendar.purim_date(self._year), -1),
# Ta`anit Ester (Fast of Esther).
tr("תענית אסתר"),
SAT_TO_PREV_THU,
)

# Purim.
self._add_hebrew_holiday(tr("פורים"), self._purim_date)
self._add_purim(tr("פורים"))

# Chol HaMoed Pesach (Passover holiday).
self._add_hebrew_holiday(tr("חול המועד פסח"), self._passover_date, range(1, 6))
self._add_passover(tr("חול המועד פסח"), range(1, 6))

# Lag Ba'omer (Lag BaOmer).
self._add_hebrew_holiday(tr('ל"ג בעומר'), self._lag_baomer_date)
self._add_lag_baomer(tr('ל"ג בעומר'))


class IL(Israel):
Expand Down
132 changes: 58 additions & 74 deletions holidays/groups/hebrew.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,141 +16,125 @@

from holidays.calendars import _HebrewLunisolar
from holidays.calendars.gregorian import _timedelta
from holidays.calendars.hebrew import (
HANUKKAH,
INDEPENDENCE_DAY,
LAG_BAOMER,
PASSOVER,
PURIM,
ROSH_HASHANAH,
SHAVUOT,
SUKKOT,
TISHA_BAV,
YOM_KIPPUR,
)


class HebrewCalendarHolidays:
"""
Hebrew lunisolar calendar holidays.
"""

@property
def _hanukkah_date(self) -> set[Optional[date]]:
def __init__(self) -> None:
self._hebrew_calendar = _HebrewLunisolar()

def _add_hebrew_calendar_holiday(
self, name: str, hol_date: Optional[date], days_delta: Union[int, Iterable[int]] = 0
) -> set[date]:
added_dates = set()
if hol_date:
for delta in (days_delta,) if isinstance(days_delta, int) else days_delta:
if dt := self._add_holiday(name, _timedelta(hol_date, delta)):
added_dates.add(dt)
return added_dates

def _add_hanukkah(
self, name: str, days_delta: Union[int, Iterable[int]] = 0
) -> set[Optional[date]]:
"""
Return Hanukkah date.
Add Hanukkah.
In some Gregorian years, there may be two Hanukkah dates.
Hanukkah is a Jewish festival commemorating the recovery of Jerusalem
and subsequent rededication of the Second Temple.
https://en.wikipedia.org/wiki/Hanukkah
"""
return {
_HebrewLunisolar._get_holiday(HANUKKAH, year) for year in (self._year - 1, self._year)
}

@property
def _israel_independence_date(self) -> Optional[date]:
"""
Return Israel Independence Day date.
Yom Ha'atzmaut is Israel's national day, commemorating the Israeli Declaration
of Independence on 14 May 1948.
https://en.wikipedia.org/wiki/Independence_Day_(Israel)
"""
return _HebrewLunisolar._get_holiday(INDEPENDENCE_DAY, self._year)
dts = self._hebrew_calendar.hanukkah_date(self._year)
for dt in dts:
self._add_hebrew_calendar_holiday(name, dt, days_delta)
return dts

@property
def _lag_baomer_date(self) -> Optional[date]:
def _add_lag_baomer(self, name: str, days_delta: Union[int, Iterable[int]] = 0) -> set[date]:
"""
Return Lag BaOmer date.
Add Lag BaOmer.
Lag BaOmer, also Lag B'Omer or Lag LaOmer, is a Jewish religious holiday celebrated
on the 33rd day of the Counting of the Omer, which occurs on the 18th day of
the Hebrew month of Iyar.
https://en.wikipedia.org/wiki/Lag_BaOmer
"""
return _HebrewLunisolar._get_holiday(LAG_BAOMER, self._year)
return self._add_hebrew_calendar_holiday(
name, self._hebrew_calendar.lag_baomer_date(self._year), days_delta
)

@property
def _passover_date(self) -> Optional[date]:
def _add_passover(self, name: str, days_delta: Union[int, Iterable[int]] = 0) -> set[date]:
"""
Return Passover date.
Add Passover.
Passover, also called Pesach, is a major Jewish holiday and one of the Three Pilgrimage
Festivals. It celebrates the Exodus of the Israelites from slavery in Egypt.
https://en.wikipedia.org/wiki/Passover
"""
return _HebrewLunisolar._get_holiday(PASSOVER, self._year)
return self._add_hebrew_calendar_holiday(
name, self._hebrew_calendar.passover_date(self._year), days_delta
)

@property
def _purim_date(self) -> Optional[date]:
def _add_purim(self, name: str) -> set[date]:
"""
Return Purim date.
Add Purim.
Purim is a Jewish holiday that commemorates the saving of the Jewish people
from annihilation at the hands of an official of the Achaemenid Empire named Haman,
as it is recounted in the Book of Esther.
https://en.wikipedia.org/wiki/Purim
"""
return _HebrewLunisolar._get_holiday(PURIM, self._year)
return self._add_hebrew_calendar_holiday(
name, self._hebrew_calendar.purim_date(self._year)
)

@property
def _rosh_hashanah_date(self) -> Optional[date]:
def _add_rosh_hashanah(
self, name: str, days_delta: Union[int, Iterable[int]] = 0
) -> set[date]:
"""
Return Rosh Hashanah date.
Add Rosh Hashanah.
Rosh Hashanah is the New Year in Judaism.
https://en.wikipedia.org/wiki/Rosh_Hashanah
"""
return _HebrewLunisolar._get_holiday(ROSH_HASHANAH, self._year)
return self._add_hebrew_calendar_holiday(
name, self._hebrew_calendar.rosh_hashanah_date(self._year), days_delta
)

@property
def _shavuot_date(self) -> Optional[date]:
def _add_shavuot(self, name: str) -> set[date]:
"""
Return Shavuot date.
Add Shavuot.
Shavuot, or Shvues, is a Jewish holiday, one of the biblically ordained
Three Pilgrimage Festivals. It occurs on the sixth day of the Hebrew month of Sivan.
https://en.wikipedia.org/wiki/Shavuot
"""
return _HebrewLunisolar._get_holiday(SHAVUOT, self._year)
return self._add_hebrew_calendar_holiday(
name, self._hebrew_calendar.shavuot_date(self._year)
)

@property
def _sukkot_date(self) -> Optional[date]:
def _add_sukkot(self, name: str, days_delta: Union[int, Iterable[int]] = 0) -> set[date]:
"""
Return Sukkot date.
Add Sukkot.
Sukkot, also known as the Feast of Tabernacles or Feast of Booths, is a Torah-commanded
holiday celebrated for seven days, beginning on the 15th day of the month of Tishrei.
https://en.wikipedia.org/wiki/Sukkot
"""
return _HebrewLunisolar._get_holiday(SUKKOT, self._year)
return self._add_hebrew_calendar_holiday(
name, self._hebrew_calendar.sukkot_date(self._year), days_delta
)

@property
def _tisha_bav_date(self) -> Optional[date]:
def _add_yom_kippur(self, name: str, days_delta: Union[int, Iterable[int]] = 0) -> set[date]:
"""
Return Tisha B'Av date.
Tisha B'Av is an annual fast day in Judaism.
https://en.wikipedia.org/wiki/Tisha_B%27Av
"""
return _HebrewLunisolar._get_holiday(TISHA_BAV, self._year)

@property
def _yom_kippur_date(self) -> Optional[date]:
"""
Return Yom Kippur date.
Add Yom Kippur.
Yom Kippur (Day of Atonement) is the holiest day of the year in Judaism.
It occurs annually on the 10th of Tishrei.
https://en.wikipedia.org/wiki/Yom_Kippur
"""
return _HebrewLunisolar._get_holiday(YOM_KIPPUR, self._year)

def _add_hebrew_holiday(self, name: str, dt: date, days_delta: Union[int, Iterable[int]] = 0):
if days_delta:
for delta in (days_delta,) if isinstance(days_delta, int) else days_delta:
self._add_holiday(name, _timedelta(dt, delta))
else:
self._add_holiday(name, dt)
return self._add_hebrew_calendar_holiday(
name, self._hebrew_calendar.yom_kippur_date(self._year), days_delta
)

0 comments on commit de94ef7

Please sign in to comment.