diff options
Diffstat (limited to 'python/analytics')
| -rw-r--r-- | python/analytics/basket_index.py | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/python/analytics/basket_index.py b/python/analytics/basket_index.py index 7e131170..a7d70d60 100644 --- a/python/analytics/basket_index.py +++ b/python/analytics/basket_index.py @@ -36,12 +36,16 @@ class BasketIndex(CreditIndex): value_date: pd.Timestamp tweaks: List[float] - __cache = {} + _cache = {} def __new__(cls, index_type, series, tenors, **kwargs): - k = (index_type, series, tuple(tenors)) - if k in cls.__cache: - return cls.__cache[k] + if isinstance(tenors, str): + tenors = (tenors,) + else: + tenors = tuple(tenors) + k = (index_type, series, tenors) + if k in cls._cache: + return cls._cache[k] else: return super().__new__(cls) @@ -54,7 +58,8 @@ class BasketIndex(CreditIndex): value_date: pd.Timestamp = pd.Timestamp.today().normalize() - BDay(), ): k = (index_type, series, tuple(tenors)) - self.__cache[k] = self + if k in self._cache: + return self.index_type = index_type self.series = series if index_type in ("HY", "HY.BB"): @@ -118,6 +123,7 @@ class BasketIndex(CreditIndex): ] ) super().__init__(self.issue_date, maturities, curves, value_date=value_date) + self._cache[k] = self def __reduce__(self): _, args = CreditIndex.__reduce__(self) |
