Skip to content

Commit

Permalink
ENH: fast cal fetching from defaults.calendars (#684)
Browse files Browse the repository at this point in the history
  • Loading branch information
attack68 authored Feb 17, 2025
1 parent ecf17d2 commit a43a0ba
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/source/i_whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ email contact, see `rateslib <https://rateslib.com>`_.
* - Period
- Add :class:`~rateslib.periods.NonDeliverableFixedPeriod`
(`681 <https://github.com/attack68/rateslib/pull/681>`_)
* - Calendars
- Allow custom calendar additions to ``defaults.calendars`` and fast fetching with
:meth:`~rateslib.calendars.get_calendar`.
* - Refactor
- Rename :class:`~rateslib.instruments.BaseMixin` to :class:`~rateslib.instruments.Metrics`.
(`678 <https://github.com/attack68/rateslib/pull/678>`_)
Expand Down
2 changes: 2 additions & 0 deletions python/rateslib/calendars/rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def get_calendar(
"""
if isinstance(calendar, str):
# parse the string in Python and return Rust Cal/UnionCal objects directly
if calendar in defaults.calendars:
return defaults.calendars[calendar]
return _parse_str_calendar(calendar, named)
elif isinstance(calendar, NoInput):
return defaults.calendars["all"]
Expand Down
6 changes: 6 additions & 0 deletions python/tests/test_calendars.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,9 @@ def test_is_day_type_tenor(tenor):
@pytest.mark.parametrize("tenor", ["1M", "1m", "4Y", "4y"])
def test_is_not_day_type_tenor(tenor):
assert not _is_day_type_tenor(tenor)


def test_get_calendar_from_defaults() -> None:
defaults.calendars["custom"] = "my_object"
assert get_calendar("custom") == "my_object"
defaults.calendars.pop("custom")

0 comments on commit a43a0ba

Please sign in to comment.