Skip to content

Commit fb66956

Browse files
committed
Improve error messages in plot_stock_market when Google finance misbehaves.
Also few cosmetic changes.
1 parent d761161 commit fb66956

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

examples/applications/plot_stock_market.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,14 @@
7676

7777
print(__doc__)
7878

79-
# #############################################################################
80-
# Retrieve the data from Internet
8179

8280
def retry(f, n_attempts=3):
8381
"Wrapper function to retry function calls in case of exceptions"
8482
def wrapper(*args, **kwargs):
8583
for i in range(n_attempts):
8684
try:
8785
return f(*args, **kwargs)
88-
except Exception as e:
86+
except Exception:
8987
if i == n_attempts - 1:
9088
raise
9189
return wrapper
@@ -122,15 +120,27 @@ def quotes_historical_google(symbol, date1, date2):
122120
'formats': ['object', 'f4', 'f4', 'f4', 'f4', 'f4']
123121
}
124122
converters = {0: lambda s: datetime.strptime(s.decode(), '%d-%b-%y')}
125-
return np.genfromtxt(response, delimiter=',', skip_header=1,
123+
data = np.genfromtxt(response, delimiter=',', skip_header=1,
126124
dtype=dtype, converters=converters,
127125
missing_values='-', filling_values=-1)
126+
expected_len_data = 1258
127+
len_data = len(data)
128+
min_date = data['date'].min()
129+
max_date = data['date'].max()
130+
if (len_data != expected_len_data or min_date != d1 or max_date != d2):
131+
raise ValueError('min_date, max_date, len(data) should be {}, {}, {} '
132+
'Got {}, {}, {} instead.'.format(
133+
d1, d2, expected_len_data,
134+
min_date, max_date, len_data))
135+
return data
128136

137+
# #############################################################################
138+
# Retrieve the data from Internet
129139

130140
# Choose a time period reasonably calm (not too long ago so that we get
131141
# high-tech firms, and before the 2008 crash)
132-
d1 = datetime(2003, 1, 1)
133-
d2 = datetime(2008, 1, 1)
142+
d1 = datetime(2003, 1, 2)
143+
d2 = datetime(2007, 12, 31)
134144

135145
symbol_dict = {
136146
'TOT': 'Total',

0 commit comments

Comments
 (0)