8000 [MRG+1] plot_stock_market retry on failed download of google data by hakaa1 · Pull Request #9437 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG+1] plot_stock_market retry on failed download of google data #9437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion examples/applications/plot_stock_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@
# #############################################################################
# Retrieve the data from Internet

def retry(f, n_attempts=3):
"Wrapper function to retry function calls in case of exceptions"
def wrapper(*args, **kwargs):
for i in range(n_attempts):
try:
return f(*args, **kwargs)
except Exception as e:
if i == n_attempts - 1:
raise
return wrapper


def quotes_historical_google(symbol, date1, date2):
"""Get the historical data from Google finance.
Expand Down Expand Up @@ -179,8 +190,10 @@ def quotes_historical_google(symbol, date1, date2):

symbols, names = np.array(list(symbol_dict.items())).T

# retry is used because quotes_historical_google can temporarily fail
# for various reasons (e.g. empty result from Google API).
quotes = [
quotes_historical_google(symbol, d1, d2) for symbol in symbols
retry(quotes_historical_google)(symbol, d1, d2) for symbol in symbols
]

close_prices = np.vstack([q['close'] for q in quotes])
Expand Down
0