Skip to content

Commit

Permalink
Hardened data.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
connorsanders committed Dec 17, 2023
1 parent 86173f3 commit dffbf53
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 5 additions & 4 deletions test/test_yahoofinancials.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def setUp(self):
self.test_yf_currencies = yf(currencies)
self.test_yf_concurrent = yf(stocks, concurrent=True)
self.test_yf_stock_flat = yf('C', flat_format=True)
self.test_yf_stock_analytic = yf('WFC')

# Fundamentals Test
def test_yf_fundamentals(self):
Expand Down Expand Up @@ -120,15 +121,15 @@ def test_yf_fundamentals_flat(self):
def test_yf_analytic_methods(self):

# Get Insights
out = self.test_yf_stock_single.get_insights()
if out.get("C").get("instrumentInfo").get("technicalEvents").get("sector") == "Financial Services":
out = self.test_yf_stock_analytic.get_insights()
if out.get("WFC").get("instrumentInfo").get("technicalEvents").get("sector") == "Financial Services":
self.assertEqual(True, True)
else:
self.assertEqual(False, True)

# Get Recommendations
out = self.test_yf_stock_single.get_recommendations()
if isinstance(out.get("C"), list):
out = self.test_yf_stock_analytic.get_recommendations()
if isinstance(out.get("WFC"), list):
self.assertEqual(True, True)
else:
self.assertEqual(False, True)
Expand Down
17 changes: 14 additions & 3 deletions yahoofinancials/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,17 @@ def _create_dict_ent(self, up_ticker, statement_type, tech_type, report_name, hi
dict_ent = {up_ticker: re_data}
return dict_ent

def _retry_create_dict_ent(self, up_ticker, statement_type, tech_type, report_name, hist_obj):
i = 0
while i < 250:
try:
out = self._create_dict_ent(up_ticker, statement_type, tech_type, report_name, hist_obj)
return out
except:
time.sleep(random.randint(2, 10))
i += 1
continue

# Private method to return the stmt_id for the reformat_process
def _get_stmt_id(self, statement_type, raw_data):
stmt_id = ''
Expand Down Expand Up @@ -611,17 +622,17 @@ def get_time_code(self, time_interval):
# Public Method to get stock data
def get_stock_data(self, statement_type='income', tech_type='', report_name='', hist_obj={}):
data = {}
if statement_type == 'income' and tech_type == '' and report_name == '': # temp, so this method doesn't return nulls
if statement_type == 'income' and tech_type == '' and report_name == '': # temp, so this method doesn't return nulls
statement_type = 'profile'
tech_type = 'assetProfile'
report_name = 'assetProfile'
if isinstance(self.ticker, str):
dict_ent = self._create_dict_ent(self.ticker, statement_type, tech_type, report_name, hist_obj)
dict_ent = self._retry_create_dict_ent(self.ticker, statement_type, tech_type, report_name, hist_obj)
data.update(dict_ent)
else:
if self.concurrent:
with Pool(self._get_worker_count()) as pool:
dict_ents = pool.map(partial(self._create_dict_ent,
dict_ents = pool.map(partial(self._retry_create_dict_ent,
statement_type=statement_type,
tech_type=tech_type,
report_name=report_name,
Expand Down

0 comments on commit dffbf53

Please sign in to comment.