From 91e79908028ed0caa9d5eb693bf9b88856d57b26 Mon Sep 17 00:00:00 2001 From: Takezo Kensei Date: Sat, 22 Jul 2017 16:28:44 +0200 Subject: [PATCH 1/3] plot_stock_market example retry on failed download of data from google (#9172) --- examples/applications/plot_stock_market.py | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/examples/applications/plot_stock_market.py b/examples/applications/plot_stock_market.py index f7ad4dcb526b5..a520152ecaebc 100644 --- a/examples/applications/plot_stock_market.py +++ b/examples/applications/plot_stock_market.py @@ -77,7 +77,34 @@ # ############################################################################# # Retrieve the data from Internet +def retry(f, n_retry=3): + """Define wrapper function to retry function calls in case of Exceptions + Parameters + ----------- + f: function + Function that will be wrapped + n_retry: int + Number of retries of function call + + Returns + ------- + f : function + Wrapped input function + + """ + def wrapper(*args, **kwargs): + exception = None + for i in range(n_retry): + try: + return f(*args, **kwargs) + except Exception as e: + exception = e + raise exception + return wrapper + + +@retry def quotes_historical_google(symbol, date1, date2): """Get the historical data from Google finance. From 4a27deceed9efa00d0473359e451d0dd67d7b5f8 Mon Sep 17 00:00:00 2001 From: Takezo Kensei Date: Mon, 24 Jul 2017 17:23:53 +0200 Subject: [PATCH 2/3] Decorate stock download function with retry() explicitly; comment on retry decision --- examples/applications/plot_stock_market.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/applications/plot_stock_market.py b/examples/applications/plot_stock_market.py index a520152ecaebc..099fb645eeeb3 100644 --- a/examples/applications/plot_stock_market.py +++ b/examples/applications/plot_stock_market.py @@ -104,7 +104,6 @@ def wrapper(*args, **kwargs): return wrapper -@retry def quotes_historical_google(symbol, date1, date2): """Get the historical data from Google finance. @@ -206,8 +205,10 @@ def quotes_historical_google(symbol, date1, date2): symbols, names = np.array(list(symbol_dict.items())).T +# Function for downloading stock data is decorated with retry() because download +# can temporary 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]) From 198c45597a4cb42386b07b5c77c76a2e4cf49ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 25 Jul 2017 08:57:48 +0200 Subject: [PATCH 3/3] Minor changes --- examples/applications/plot_stock_market.py | 29 ++++++---------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/examples/applications/plot_stock_market.py b/examples/applications/plot_stock_market.py index 099fb645eeeb3..8a85b0645cb8c 100644 --- a/examples/applications/plot_stock_market.py +++ b/examples/applications/plot_stock_market.py @@ -77,30 +77,15 @@ # ############################################################################# # Retrieve the data from Internet -def retry(f, n_retry=3): - """Define wrapper function to retry function calls in case of Exceptions - - Parameters - ----------- - f: function - Function that will be wrapped - n_retry: int - Number of retries of function call - - Returns - ------- - f : function - Wrapped input function - - """ +def retry(f, n_attempts=3): + "Wrapper function to retry function calls in case of exceptions" def wrapper(*args, **kwargs): - exception = None - for i in range(n_retry): + for i in range(n_attempts): try: return f(*args, **kwargs) except Exception as e: - exception = e - raise exception + if i == n_attempts - 1: + raise return wrapper @@ -205,8 +190,8 @@ def quotes_historical_google(symbol, date1, date2): symbols, names = np.array(list(symbol_dict.items())).T -# Function for downloading stock data is decorated with retry() because download -# can temporary fail for various reasons (e.g. empty result from Google API). +# retry is used because quotes_historical_google can temporarily fail +# for various reasons (e.g. empty result from Google API). quotes = [ retry(quotes_historical_google)(symbol, d1, d2) for symbol in symbols ]