Skip to content

Commit

Permalink
Small cleanup to ease finding bug ranaroussi#1076. Begun by getting r…
Browse files Browse the repository at this point in the history
…id of multiple calls to self.info (get_info).
  • Loading branch information
fredrik-corneliusson committed Oct 23, 2022
1 parent 6c21c19 commit d01d378
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
42 changes: 16 additions & 26 deletions yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import numpy as _np
import re as _re

from pytz import UnknownTimeZoneError

try:
from urllib.parse import quote as urlencode
except ImportError:
Expand Down Expand Up @@ -381,48 +383,36 @@ def _get_ticker_tz(self):

tz = utils.tz_cache.lookup(self.ticker)

if tz is not None:
invalid_value = not isinstance(tz, str)
if not invalid_value:
try:
_tz.timezone(tz)
except:
invalid_value = True

if invalid_value:
# Clear from cache and force re-fetch
utils.tz_cache.store(self.ticker, None)
tz = None
if tz and not utils.is_valid_timezone(tz):
# Clear from cache and force re-fetch
utils.tz_cache.store(self.ticker, None)
tz = None

if tz is None:
if not 'exchangeTimezoneName' in self.info:
try:
tz = self.info["exchangeTimezoneName"]
except KeyError:
return None
tz = self.info["exchangeTimezoneName"]
if not isinstance(tz, str):
tz = None
else:
try:
_tz.timezone(tz)
except:
tz = None
if tz is not None:

if utils.is_valid_timezone(tz):
# info fetch is relatively slow so cache timezone
utils.tz_cache.store(self.ticker, tz)
else:
tz = None

self._tz = tz
return tz


def _get_info(self, proxy=None):
# setup proxy in requests format
if proxy is not None:
if isinstance(proxy, dict) and "https" in proxy:
proxy = proxy["https"]
proxy = {"https": proxy}

if (self._info is None) or (self._sustainability is None) or (self._recommendations is None):
## Need to fetch
pass
else:
if None not in (self._info, self._sustainability, self._recommendations):
# No need to fetch
return

ticker_url = "{}/{}".format(self._scrape_url, self.ticker)
Expand Down
9 changes: 9 additions & 0 deletions yfinance/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

from threading import Lock

from pytz import UnknownTimeZoneError

try:
import ujson as _json
except ImportError:
Expand Down Expand Up @@ -442,6 +444,13 @@ def fix_Yahoo_dst_issue(df, interval):
df.index += _pd.TimedeltaIndex(dst_error_hours, 'h')
return df

def is_valid_timezone(tz: str) -> bool:
try:
_tz.timezone(tz)
except UnknownTimeZoneError:
return False
return True


class ProgressBar:
def __init__(self, iterations, text='completed'):
Expand Down

0 comments on commit d01d378

Please sign in to comment.