-
Notifications
You must be signed in to change notification settings - Fork 487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement HolidayBase::get_closest_holiday functionality #2211
base: dev
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## dev #2211 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 200 200
Lines 12215 12230 +15
Branches 1737 1741 +4
=========================================
+ Hits 12215 12230 +15 ☔ View full report in Codecov by Sentry. |
@Rosi2143, it's good start! But some checks are required. Try to run this: from holidays.countries.ukraine import UA
ua = UA()
ua.get_next_holiday("1991-01-01", True)
ua.get_next_holiday("2022-03-08") |
@KJhellico: Thanks - I didn't know that there is a start/end-date for a calender. But I guess for some countries it makes sense. Added the checks and the documentation with the latest version. |
053f942
to
562bfa1
Compare
562bfa1
to
c91e03e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Rosi2143, , well done! Please take a look at a few suggestions.
b353dde
to
a5554fb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great speed improvement! 👍
a5554fb
to
c9cd832
Compare
c9cd832
to
adf4653
Compare
Since the holidays in the holidays entity object are not always placed in chronological order, it's necessary to sort them before searching: >>> from holidays.countries.united_states import US
>>> h = US(years=2024)
>>> h.get_next_holiday("2024-02-01")
(datetime.date(2024, 5, 27), 'Memorial Day')
>>> for d, name in h.items():
>>> print(d, name)
2024-01-01 New Year's Day
2024-05-27 Memorial Day
2024-06-19 Juneteenth National Independence Day
2024-07-04 Independence Day
2024-09-02 Labor Day
2024-11-11 Veterans Day
2024-11-28 Thanksgiving
2024-12-25 Christmas Day
2024-01-15 Martin Luther King Jr. Day
2024-02-19 Washington's Birthday
2024-10-14 Columbus Day |
8934029
to
e56effc
Compare
Thanks for the hint that no sorting is applied to calendars. That was one of the reasons I choose my initial implementation. I wanted to keep all the corner cases out of this function - at the cost of having a longer runtime. But fixed this now as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this @Rosi2143
Please consider these comments:
677ac23
to
6fda9a8
Compare
ec129fb
to
ecb340a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This version LGTM. For Ukraine, we can override this method individually when the time comes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
191ec86
to
bc9f92e
Compare
Hi @KJhellico and @arkid15r , will you merge this? I have no write access to your branch. Thanks |
I'll review it this week. Sorry for the delay. |
a new function get_next_holiday is added to retrieve the date of the next known holiday. Also the name of the holiday is returned. It is possible to search forward and backward in time. This should solve vacanza#1825 Signed-off-by: Schrotti <Schrott.Micha@web.de>
Use more stable calender for testing make sure calendar is filled also if no date is passed Signed-off-by: Schrotti <Schrott.Micha@web.de>
add test to check that years without holiday are handled correctly Signed-off-by: Schrotti <Schrott.Micha@web.de>
Signed-off-by: Schrotti <Schrott.Micha@web.de>
Signed-off-by: Schrotti <Schrott.Micha@web.de>
8921633
to
bc39a89
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Rosi2143
I've updated the implementation. Please let me know if you have any suggestions/comments.
@@ -967,6 +967,29 @@ def get_named( | |||
|
|||
raise AttributeError(f"Unknown lookup type: {lookup}") | |||
|
|||
def get_closest_holiday( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember I offered you a more efficient solution last month here and I think we should use that approach:
- populate both previous and next years and use
sorted
once - ensure it's O(log n) by using binary search and avoiding
next()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
I've seen your change - but was concerned that this adds a new dependency to the bisect module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine as it's a part of the standard library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To compare efficiency:
>>> python -m timeit -s "from datetime import date; from holidays.calendars.gregorian import _timedelta; from holidays.countries.united_states import US; us = US(years=2024); start = date(2024, 1, 1)" -n 1000 "for n in range(366): d = us.get_closest_holiday(_timedelta(start, n))"
1000 loops, best of 5: 1.06 msec per loop
>>> python -m timeit -s "from datetime import date; from holidays.calendars.gregorian import _timedelta; from holidays.countries.united_states import US; us = US(years=2024); start = date(2024, 1, 1)" -n 1000 "for n in range(366): d = us.get_closest_holiday(_timedelta(start, n), direction='backward')"
1000 loops, best of 5: 1.14 msec per loop
Previous version:
...
1000 loops, best of 5: 1.17 msec per loop
...
1000 loops, best of 5: 1.34 msec per loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
Proposed change
a new function get_next_holiday is added to retrieve the date of the next known holiday.
Also the name of the holiday is returned.
It is possible to search forward and backward in time.
This should solve #1825
Documentation will be added once the basic changes are approved.
holidays
functionality in general)Checklist
make check
, all checks and tests are green