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 for Solver #389

Merged
merged 1 commit into from
Sep 16, 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
4 changes: 4 additions & 0 deletions docs/source/i_whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ email contact through **[email protected]**.
- The ``__repr__`` method of *FXRates* and *FXForwards* types is changed for better
display in associated packages.
`(388) <https://github.com/attack68/rateslib/pull/388>`_
* - Refactor
- The ``__repr__`` method of *Solver* types is changed for better
display in associated packages.
`(389) <https://github.com/attack68/rateslib/pull/389>`_
* - 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/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,9 @@ def __init__(
}
self.iterate()

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

def _parse_instrument(self, value):
"""
Parses different input formats for an instrument given to the ``Solver``.
Expand Down
27 changes: 27 additions & 0 deletions python/tests/test_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,33 @@ def test_basic_solver(algo) -> None:
assert abs(float(curve.nodes[key]) - expected[i]) < 1e-6


def test_solver_repr():
curve = Curve(
{
dt(2022, 1, 1): 1.0,
dt(2023, 1, 1): 1.0,
dt(2024, 1, 1): 1.0,
dt(2025, 1, 1): 1.0,
},
id="v",
)
instruments = [
(IRS(dt(2022, 1, 1), "1Y", "Q"), (curve,), {}),
(IRS(dt(2022, 1, 1), "2Y", "Q"), (curve,), {}),
(IRS(dt(2022, 1, 1), "3Y", "Q"), (curve,), {}),
]
s = np.array([1.0, 1.6, 2.0])
solver = Solver(
curves=[curve],
instruments=instruments,
s=s,
id="S_ID"
)
result = solver.__repr__()
expected = f"<rl.Solver:S_ID at {hex(id(solver))}>"
assert result == expected


@pytest.mark.parametrize("algo", ["gauss_newton", "levenberg_marquardt", "gradient_descent"])
def test_solver_reiterate(algo) -> None:
# test that curves are properly updated by a reiterate
Expand Down
Loading