Skip to content

Commit

Permalink
REF: create sub period containers on legs
Browse files Browse the repository at this point in the history
  • Loading branch information
attack68 committed Jan 14, 2025
1 parent 9bb50fd commit fd46bf5
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions python/rateslib/legs.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def _set_periods(self) -> None:
]
interleaved_periods_.append(self._regular_periods[-1]) # add last regular period
else:
interleaved_periods_ = self._regular_periods
interleaved_periods_ = list(self._regular_periods)
periods_.extend(interleaved_periods_)

if self._exchange_periods[1] is not None:
Expand All @@ -241,11 +241,10 @@ def _set_periods(self) -> None:
def _set_exchange_periods(self) -> None:
"""Set default cashflow exchanges on Legs with `initial_exchange` or `final_exchange`."""

periods_: list[Cashflow | None] = []
periods_: list[Cashflow | None] = [None, None]

if self.initial_exchange:
periods_.append(
Cashflow(
periods_[0] = Cashflow(
notional=-self.notional,
payment=self.schedule.calendar.lag(
self.schedule.aschedule[0],
Expand All @@ -254,14 +253,10 @@ def _set_exchange_periods(self) -> None:
),
currency=self.currency,
stub_type="Exchange",
),
)
else:
periods_.append(None)
)

if self.final_exchange:
periods_.append(
Cashflow(
periods_[1] = Cashflow(
notional=self.notional - self.amortization * (self.schedule.n_periods - 1),
payment=self.schedule.calendar.lag(
self.schedule.aschedule[-1],
Expand All @@ -270,12 +265,9 @@ def _set_exchange_periods(self) -> None:
),
currency=self.currency,
stub_type="Exchange",
),
)
else:
periods_.append(None)
)

self._exchange_periods: list[Cashflow | None] = periods_
self._exchange_periods: tuple[Cashflow | None, Cashflow | None] = tuple(periods_) # type: ignore[assignment]

def _set_interim_exchange_periods(self) -> None:
"""Set cashflow exchanges if `amortization` and `final_exchange` are present."""
Expand All @@ -299,7 +291,8 @@ def _set_interim_exchange_periods(self) -> None:
self._interim_exchange_periods: list[Cashflow] | None = periods_

def _set_regular_periods(self) -> None:
periods_: list[Period] = [
self._regular_periods: tuple[Period, ...] = tuple(
[
self._regular_period(
start=period[defaults.headers["a_acc_start"]],
end=period[defaults.headers["a_acc_end"]],
Expand All @@ -310,7 +303,7 @@ def _set_regular_periods(self) -> None:
)
for i, period in enumerate(self.schedule.table.to_dict(orient="index").values())
]
self._regular_periods: list[Period] = periods_
)

@abstractmethod
def _regular_period(
Expand Down Expand Up @@ -531,6 +524,7 @@ class FixedLeg(_FixedLegMixin, BaseLeg): # type: ignore[misc]
""" # noqa: E501

periods: list[FixedPeriod | Cashflow] # type: ignore[assignment]
_regular_periods: tuple[FixedPeriod, ...]

def __init__(
self, *args: Any, fixed_rate: DualTypes | NoInput = NoInput(0), **kwargs: Any
Expand Down Expand Up @@ -2241,6 +2235,8 @@ class CreditPremiumLeg(_FixedLegMixin, BaseLeg):

periods: list[CreditPremiumPeriod] # type: ignore[assignment]

_regular_periods: tuple[CreditPremiumPeriod, ...]

def __init__(
self,
*args: Any,
Expand Down

0 comments on commit fd46bf5

Please sign in to comment.