Skip to content
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

REF: repr Legs #413

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading