Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

finance: Fixed making directories for explicit cachename #1834

Merged
merged 2 commits into from

2 participants

@mgiuca-google

In finance.fetch_historical_yahoo, if an explicit cachename is supplied, the code still attempts to create a directory in the usual cache location (which is not necessary), and fails to create directories required for the supplied cache directory.

I have patched it so that it creates the necessary directories leading up to cachename.

As an aside, I also fixed it so that it explicitly closes the URL object opened by urlopen and uses the modern with style context managers.

mgiuca-google added some commits
@mgiuca-google mgiuca-google finance.fetch_historical_yahoo: Create directories leading up to cach…
…ename.

Previously, if cachename was specified, it would create the directories to
cachedir instead, resulting in OSError if the directories leading up to
cachename were not specified.
ae3195f
@mgiuca-google mgiuca-google finance: Use context manager to close files.
Previously, the urllib object opened in fetch_historical_yahoo was not
explicitly closed.
87dff9c
@pelson
Collaborator
@pelson pelson merged commit 0c417e0 into from
@mgiuca-google mgiuca-google deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 18, 2013
  1. @mgiuca-google

    finance.fetch_historical_yahoo: Create directories leading up to cach…

    mgiuca-google authored
    …ename.
    
    Previously, if cachename was specified, it would create the directories to
    cachedir instead, resulting in OSError if the directories leading up to
    cachename were not specified.
  2. @mgiuca-google

    finance: Use context manager to close files.

    mgiuca-google authored
    Previously, the urllib object opened in fetch_historical_yahoo was not
    explicitly closed.
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 7 deletions.
  1. +5 −7 lib/matplotlib/finance.py
View
12 lib/matplotlib/finance.py
@@ -4,7 +4,7 @@
"""
from __future__ import division, print_function
-import os, sys, warnings
+import contextlib, os, sys, warnings
from urllib2 import urlopen
if sys.version_info[0] < 3:
@@ -184,12 +184,10 @@ def fetch_historical_yahoo(ticker, date1, date2, cachename=None,dividends=False)
fh = open(cachename)
verbose.report('Using cachefile %s for %s'%(cachename, ticker))
else:
- mkdirs(cachedir)
- urlfh = urlopen(url)
-
- fh = open(cachename, 'wb')
- fh.write(urlfh.read())
- fh.close()
+ mkdirs(os.path.abspath(os.path.dirname(cachename)))
+ with contextlib.closing(urlopen(url)) as urlfh:
+ with open(cachename, 'wb') as fh:
+ fh.write(urlfh.read())
verbose.report('Saved %s data to cache file %s'%(ticker, cachename))
fh = open(cachename, 'r')
Something went wrong with that request. Please try again.