feat: Implemented an internal cache for ticker objects

This commit is contained in:
Soumyadip Sarkar 2025-07-01 00:53:50 +05:30 committed by GitHub
parent 718df34932
commit 327583d904
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 2 deletions

View File

@ -1,7 +1,7 @@
# gets data/stats
import yfinance as yf
from typing import Annotated, Callable, Any, Optional
from typing import Annotated, Callable, Any, Optional, Dict
from pandas import DataFrame
import pandas as pd
from functools import wraps
@ -9,12 +9,23 @@ from functools import wraps
from .utils import save_output, SavePathType, decorate_all_methods
_TICKER_CACHE: Dict[str, yf.Ticker] = {}
def clear_cache() -> None:
"""Clear the internal ticker cache."""
_TICKER_CACHE.clear()
def init_ticker(func: Callable) -> Callable:
"""Decorator to initialize yf.Ticker and pass it to the function."""
@wraps(func)
def wrapper(symbol: Annotated[str, "ticker symbol"], *args, **kwargs) -> Any:
ticker = yf.Ticker(symbol)
ticker = _TICKER_CACHE.get(symbol)
if ticker is None:
ticker = yf.Ticker(symbol)
_TICKER_CACHE[symbol] = ticker
return func(ticker, *args, **kwargs)
return wrapper