Skip to content

Commit

Permalink
REF: repr Legs (#413)
Browse files Browse the repository at this point in the history
Co-authored-by: JHM Darbyshire (win11) <[email protected]>
  • Loading branch information
attack68 and attack68 authored Sep 24, 2024
1 parent 877c050 commit 7b4b0c4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/source/i_whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ email contact, see `rateslib <https://rateslib.com>`_.
`(410) <https://github.com/attack68/rateslib/pull/410>`_
* - Refactor
- The ``__repr__`` method of all *Curve* types, *FXRates* and *FXForwards* types, the *Solver*,
and all *Period* types are changed for better display in associated
and all *Period* and *Leg* types are changed for better display in associated
packages.
`(387) <https://github.com/attack68/rateslib/pull/387>`_
`(388) <https://github.com/attack68/rateslib/pull/388>`_
`(389) <https://github.com/attack68/rateslib/pull/389>`_
`(390) <https://github.com/attack68/rateslib/pull/390>`_
`(413) <https://github.com/attack68/rateslib/pull/413>`_
* - Performance
- Improve the speed of bond :meth:`~rateslib.instruments.FixedRateBond.ytm` calculations from about 750us to
500us on average.
Expand Down
3 changes: 3 additions & 0 deletions python/rateslib/legs.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@ def _spread(self, target_npv, fore_curve, disc_curve, fx=NoInput(0)):
# _ = self._spread_isda_dual2(target_npv, fore_curve, disc_curve, fx)
return _

def __repr__(self):
return f"<rl.{type(self).__name__} at {hex(id(self))}>"


class FixedLegMixin:
"""
Expand Down
5 changes: 1 addition & 4 deletions python/tests/test_instruments_bonds.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,7 @@ def test_sgb_1060s_price_and_accrued(self, settlement, exp_accrued, exp_price) -
def test_sgb_ultra_short_ytm(self):
# SE0010469205
komins = FixedRateBond(
effective=dt(2017, 10, 2),
termination=dt(2024, 10, 2),
fixed_rate=1.0,
spec="se_gb"
effective=dt(2017, 10, 2), termination=dt(2024, 10, 2), fixed_rate=1.0, spec="se_gb"
)
dp = komins.price(ytm=3.42092, settlement=dt(2024, 9, 24), dirty=True)
cp = komins.price(ytm=3.42092, settlement=dt(2024, 9, 24), dirty=False)
Expand Down
56 changes: 56 additions & 0 deletions python/tests/test_legs.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,62 @@ def curve():
return Curve(nodes=nodes, interpolation="log_linear")


@pytest.mark.parametrize(
"Leg",
[
FloatLeg,
FixedLeg,
ZeroFixedLeg,
ZeroFloatLeg,
],
)
def test_repr(Leg):
leg = Leg(
effective=dt(2022, 1, 1),
termination="1y",
frequency="Q",
)
result = leg.__repr__()
expected = f"<rl.{type(leg).__name__} at {hex(id(leg))}>"
assert result == expected


@pytest.mark.parametrize(
"Leg",
[
IndexFixedLeg,
ZeroIndexLeg,
],
)
def test_repr_index(Leg):
leg = Leg(effective=dt(2022, 1, 1), termination="1y", frequency="Q", index_base=100.0)
result = leg.__repr__()
expected = f"<rl.{type(leg).__name__} at {hex(id(leg))}>"
assert result == expected


@pytest.mark.parametrize("Leg", [FixedLegMtm, FloatLegMtm])
def test_repr_mtm(Leg):
leg = Leg(
effective=dt(2022, 1, 1),
termination="1y",
frequency="Q",
alt_currency="usd",
currency="eur",
)
result = leg.__repr__()
expected = f"<rl.{type(leg).__name__} at {hex(id(leg))}>"
assert result == expected


def test_repr_custom():
period = FixedPeriod(
start=dt(2000, 1, 1), end=dt(2000, 2, 1), payment=dt(2000, 2, 1), frequency="m"
)
leg = CustomLeg([period])
assert leg.__repr__() == f"<rl.CustomLeg at {hex(id(leg))}>"


class TestFloatLeg:
@pytest.mark.parametrize(
"obj",
Expand Down

0 comments on commit 7b4b0c4

Please sign in to comment.