summaryrefslogtreecommitdiff
path: root/numpy/lib/_datasource.py
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2008-08-27 14:31:18 +0000
committerTravis Oliphant <oliphant@enthought.com>2008-08-27 14:31:18 +0000
commiteda3f09142f08498f479f6353c5c063ecef4aac0 (patch)
tree390a4bec0155841536b83da26bb5c17860ee6fe6 /numpy/lib/_datasource.py
parent6aa2c46129ca2e18aa5e720a9a45f8830f1bfb58 (diff)
downloadnumpy-eda3f09142f08498f479f6353c5c063ecef4aac0.tar.gz
Apply modified version of Andrew Dalke's patch in #874 to create a quicker-loading numpy.
Diffstat (limited to 'numpy/lib/_datasource.py')
-rw-r--r--numpy/lib/_datasource.py50
1 files changed, 37 insertions, 13 deletions
diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py
index 1201f3d7e..3055bf47a 100644
--- a/numpy/lib/_datasource.py
+++ b/numpy/lib/_datasource.py
@@ -36,21 +36,39 @@ __docformat__ = "restructuredtext en"
import os
from shutil import rmtree
-from urlparse import urlparse
-# TODO: .zip support, .tar support?
-_file_openers = {None: open}
-try:
- import bz2
- _file_openers[".bz2"] = bz2.BZ2File
-except ImportError:
- pass
-try:
- import gzip
- _file_openers[".gz"] = gzip.open
-except ImportError:
- pass
+# Using a class instead of a module-level dictionary
+# to reduce the inital 'import numpy' overhead by
+# deferring the import of bz2 and gzip until needed
+# TODO: .zip support, .tar support?
+class _FileOpeners(object):
+ def __init__(self):
+ self._loaded = False
+ self._file_openers = {None: open}
+ def _load(self):
+ if self._loaded:
+ return
+ try:
+ import bz2
+ self._file_openers[".bz2"] = bz2.BZ2File
+ except ImportError:
+ pass
+ try:
+ import gzip
+ self._file_openers[".gz"] = gzip.open
+ except ImportError:
+ pass
+ self._loaded = True
+
+ def keys(self):
+ self._load()
+ return self._file_openers.keys()
+ def __getitem__(self, key):
+ self._load()
+ return self._file_openers[key]
+
+_file_openers = _FileOpeners()
def open(path, mode='r', destpath=os.curdir):
"""Open ``path`` with ``mode`` and return the file object.
@@ -179,6 +197,9 @@ class DataSource (object):
def _isurl(self, path):
"""Test if path is a net location. Tests the scheme and netloc."""
+
+ # We do this here to reduce the 'import numpy' initial import time.
+ from urlparse import urlparse
# BUG : URLs require a scheme string ('http://') to be used.
# www.google.com will fail.
@@ -276,6 +297,9 @@ class DataSource (object):
`open` : Method that downloads and opens files.
"""
+ # We do this here to reduce the 'import numpy' initial import time.
+ from urlparse import urlparse
+
# TODO: This should be more robust. Handles case where path includes
# the destpath, but not other sub-paths. Failing case: