Simplify _yf_retry: remove unused variable and unreachable code
This commit is contained in:
parent
781dd971ba
commit
e0b41fbb82
|
|
@ -13,24 +13,21 @@ logger = logging.getLogger(__name__)
|
||||||
def _yf_retry(func, max_retries=3, initial_delay=2.0):
|
def _yf_retry(func, max_retries=3, initial_delay=2.0):
|
||||||
"""Retry a yfinance call with exponential backoff on rate limit errors."""
|
"""Retry a yfinance call with exponential backoff on rate limit errors."""
|
||||||
delay = initial_delay
|
delay = initial_delay
|
||||||
last_error = None
|
|
||||||
for attempt in range(max_retries + 1):
|
for attempt in range(max_retries + 1):
|
||||||
try:
|
try:
|
||||||
return func()
|
return func()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
last_error = e
|
|
||||||
error_str = str(e).lower()
|
error_str = str(e).lower()
|
||||||
if "rate" in error_str or "too many" in error_str or "429" in error_str:
|
is_rate_limit = "rate" in error_str or "too many" in error_str or "429" in error_str
|
||||||
if attempt < max_retries:
|
if is_rate_limit and attempt < max_retries:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"yfinance rate limited, retrying in {delay:.0f}s "
|
f"yfinance rate limited, retrying in {delay:.0f}s "
|
||||||
f"(attempt {attempt + 1}/{max_retries})"
|
f"(attempt {attempt + 1}/{max_retries})"
|
||||||
)
|
)
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
delay *= 2
|
delay *= 2
|
||||||
continue
|
else:
|
||||||
raise
|
raise
|
||||||
raise last_error
|
|
||||||
|
|
||||||
def get_YFin_data_online(
|
def get_YFin_data_online(
|
||||||
symbol: Annotated[str, "ticker symbol of the company"],
|
symbol: Annotated[str, "ticker symbol of the company"],
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue