Repository: datapythonista/mnist

Issue:
Allowing specification of the download directory
Right now the functions listed on the README.md doesn't allow specifying the target directory. This functionality should be possible just by propagating extra parameters to the download function.

Discussion:

Patch:
diff --git a/README.md b/README.md
index b164037..80a9d4e 100644
--- a/README.md
+++ b/README.md
@@ -63,4 +63,18 @@ train_images = mnist.train_images()
 x = images.reshape((images.shape[0], images.shape[1] * images.shape[2]))
 ```
 
+Both the url where the files can be found, and the temporary directory where
+they will be cached locally can be modified in the next way:
+```
+import mnist
+
+mnist.datasets_url = 'http://url-to-the/datasets'
+
+# temporary_dir is a function, so it can be dinamically created
+# like Python stdlib `tempfile.gettempdir` (which is the default)
+mnist.temporary_dir = lambda: '/tmp/mnist'
+
+train_images = mnist.train_images()
+```
+
 It supports Python 2.7 and Python >= 3.5.
diff --git a/mnist/__init__.py b/mnist/__init__.py
index 2a41398..a567875 100644
--- a/mnist/__init__.py
+++ b/mnist/__init__.py
@@ -16,8 +16,11 @@ except ImportError:
 import numpy
 
 
-# the url can be changed by the users of the library (not a constant)
+# `datasets_url` and `temporary_dir` can be set by the user using:
+# >>> mnist.datasets_url = 'http://my.mnist.url'
+# >>> mnist.temporary_dir = lambda: '/tmp/mnist'
 datasets_url = 'http://yann.lecun.com/exdb/mnist/'
+temporary_dir = tempfile.gettempdir
 
 
 class IdxDecodeError(ValueError):
@@ -45,8 +48,7 @@ def download_file(fname, target_dir=None, force=False):
     fname : str
         Full path of the downloaded file
     """
-    if not target_dir:
-        target_dir = tempfile.gettempdir()
+    target_dir = target_dir or temporary_dir()
     target_fname = os.path.join(target_dir, fname)
 
     if force or not os.path.isfile(target_fname):


Test patch:
diff --git a/tests/test_download_mnist.py b/tests/test_download_mnist.py
index 631104d..e71aabe 100644
--- a/tests/test_download_mnist.py
+++ b/tests/test_download_mnist.py
@@ -97,8 +97,20 @@ class TestDownloadMNIST(unittest.TestCase):
 
     @mock.patch('mnist.urlretrieve')
     def test_datasets_url_is_used(self, urlretrieve):
+        original_url = mnist.datasets_url
         mnist.datasets_url = 'http://aaa.com/'
         mnist.download_file('mnist_datasets_url.gz')
         fname = os.path.join(tempfile.gettempdir(), 'mnist_datasets_url.gz')
         urlretrieve.assert_called_once_with(
             'http://aaa.com/mnist_datasets_url.gz', fname)
+        mnist.datasets_url = original_url
+
+    @mock.patch('mnist.urlretrieve')
+    def test_temporary_dir_is_used(self, urlretrieve):
+        original_temp_dir = mnist.temporary_dir
+        mnist.temporary_dir = lambda: '/another/tmp/dir/'
+        fname = mnist.download_file('test')
+        urlretrieve.assert_called_once_with(mnist.datasets_url + 'test',
+                                            '/another/tmp/dir/test')
+        self.assertEqual(fname, '/another/tmp/dir/test')
+        mnist.temporary_dir = original_temp_dir