Skip to content

Commit

Permalink
REF: amend cache_states to decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
attack68 committed Jan 28, 2025
1 parent 577dafc commit 0172dab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions python/rateslib/curves/curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
_clear_cache_post,
_new_state_post,
_validate_states,
_WithState,
_WithCache,
_WithState,
)
from rateslib.rs import Modifier, index_left_f64
from rateslib.rs import from_json as from_json_rs
Expand All @@ -57,7 +57,7 @@
# Contact rateslib at gmail.com if this code is observed outside its intended sphere.


class Curve(_WithState, _WithCache):
class Curve(_WithState, _WithCache[datetime, DualTypes]):
"""
Curve based on DF parametrisation at given node dates with interpolation.
Expand Down
6 changes: 3 additions & 3 deletions python/rateslib/fx_volatility.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
from rateslib.mutability import (
_clear_cache_post,
_new_state_post,
_WithState,
_WithCache,
_WithState,
)
from rateslib.rs import index_left_f64
from rateslib.splines import PPSplineDual, PPSplineDual2, PPSplineF64, evaluate
Expand All @@ -46,7 +46,7 @@
TERMINAL_DATE = dt(2100, 1, 1)


class FXDeltaVolSmile(_WithState, _WithCache):
class FXDeltaVolSmile(_WithState, _WithCache[float, DualTypes]):
r"""
Create an *FX Volatility Smile* at a given expiry indexed by delta percent.
Expand Down Expand Up @@ -774,7 +774,7 @@ def update_node(self, key: float, value: DualTypes) -> None:
# Serialization


class FXDeltaVolSurface(_WithState, _WithCache):
class FXDeltaVolSurface(_WithState, _WithCache[datetime, FXDeltaVolSmile]):
r"""
Create an *FX Volatility Surface* parametrised by cross-sectional *Smiles* at different
expiries.
Expand Down
9 changes: 5 additions & 4 deletions python/rateslib/mutability/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
from collections import OrderedDict
from collections.abc import Callable
from typing import ParamSpec, TypeVar, Generic
from typing import ParamSpec, TypeVar

from rateslib import defaults

Expand Down Expand Up @@ -56,7 +56,7 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
return wrapper


class _WithState[KT, VT]:
class _WithState:
"""
Record and manage the `state_id` of mutable classes.
Expand All @@ -70,8 +70,6 @@ class _WithState[KT, VT]:

_state: int = 0
_mutable_by_association = False
_cache: OrderedDict[KT, VT]
_cache_len: int

def _set_new_state(self) -> None:
"""Set the state_id of a superclass. Some objects which are 'mutable by association'
Expand All @@ -95,6 +93,9 @@ def _get_composited_state(self) -> int:

class _WithCache[KT, VT]:

_cache: OrderedDict[KT, VT]
_cache_len: int

def _cached_value(self, key: KT, val: VT) -> VT:
"""Used to add a value to the cache and control memory size when returning some
parameter from an object using cache and state management."""
Expand Down

0 comments on commit 0172dab

Please sign in to comment.