Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1adfc85

Browse filesBrowse files
committed
finance: Gracefully handle the case of there being no config dir.
Instead of raising RuntimeError, now avoids reading and writing the cache.
1 parent 1dbd6de commit 1adfc85
Copy full SHA for 1adfc85

File tree

Expand file treeCollapse file tree

1 file changed

+24
-16
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+24
-16
lines changed

‎lib/matplotlib/finance.py

Copy file name to clipboardExpand all lines: lib/matplotlib/finance.py
+24-16Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@
2828

2929

3030
configdir = get_configdir()
31-
if configdir is None:
32-
raise RuntimeError('Could not find a suitable configuration directory')
33-
cachedir = os.path.join(configdir, 'finance.cache')
31+
# cachedir will be None if there is no writable directory.
32+
if configdir is not None:
33+
cachedir = os.path.join(configdir, 'finance.cache')
34+
else:
35+
# Should only happen in a restricted environment (such as Google App
36+
# Engine). Deal with this gracefully by not caching finance data.
37+
cachedir = None
3438

3539

3640
stock_dt = np.dtype([('date', object),
@@ -180,20 +184,24 @@ def fetch_historical_yahoo(ticker, date1, date2, cachename=None,dividends=False)
180184
d2[0], d2[1], d2[2], ticker, g)
181185

182186

183-
if cachename is None:
184-
cachename = os.path.join(cachedir, md5(url).hexdigest())
185-
if os.path.exists(cachename):
186-
fh = open(cachename)
187-
verbose.report('Using cachefile %s for %s'%(cachename, ticker))
187+
# Cache the finance data if there is a writable cache directory.
188+
if cachedir is not None:
189+
if cachename is None:
190+
cachename = os.path.join(cachedir, md5(url).hexdigest())
191+
if os.path.exists(cachename):
192+
fh = open(cachename)
193+
verbose.report('Using cachefile %s for %s'%(cachename, ticker))
194+
else:
195+
mkdirs(os.path.abspath(os.path.dirname(cachename)))
196+
with contextlib.closing(urlopen(url)) as urlfh:
197+
with open(cachename, 'wb') as fh:
198+
fh.write(urlfh.read())
199+
verbose.report('Saved %s data to cache file %s'%(ticker, cachename))
200+
fh = open(cachename, 'r')
201+
202+
return fh
188203
else:
189-
mkdirs(os.path.abspath(os.path.dirname(cachename)))
190-
with contextlib.closing(urlopen(url)) as urlfh:
191-
with open(cachename, 'wb') as fh:
192-
fh.write(urlfh.read())
193-
verbose.report('Saved %s data to cache file %s'%(ticker, cachename))
194-
fh = open(cachename, 'r')
195-
196-
return fh
204+
return urlopen(url)
197205

198206

199207
def quotes_historical_yahoo(ticker, date1, date2, asobject=False,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.