diff options
author | Travis Oliphant <oliphant@enthought.com> | 2008-08-27 14:31:18 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2008-08-27 14:31:18 +0000 |
commit | eda3f09142f08498f479f6353c5c063ecef4aac0 (patch) | |
tree | 390a4bec0155841536b83da26bb5c17860ee6fe6 /numpy/lib | |
parent | 6aa2c46129ca2e18aa5e720a9a45f8830f1bfb58 (diff) | |
download | numpy-eda3f09142f08498f479f6353c5c063ecef4aac0.tar.gz |
Apply modified version of Andrew Dalke's patch in #874 to create a quicker-loading numpy.
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/_datasource.py | 50 | ||||
-rw-r--r-- | numpy/lib/format.py | 3 |
2 files changed, 39 insertions, 14 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: diff --git a/numpy/lib/format.py b/numpy/lib/format.py index 4192e1225..32a23ae18 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -57,7 +57,6 @@ means there is 1 element) by dtype.itemsize. """ import cPickle -import struct import numpy from numpy.lib.utils import safe_eval @@ -180,6 +179,7 @@ def write_array_header_1_0(fp, d): This has the appropriate entries for writing its string representation to the header of the file. """ + import struct header = ["{"] for key, value in sorted(d.items()): # Need to use repr here, since we eval these when reading @@ -230,6 +230,7 @@ def read_array_header_1_0(fp): """ # Read an unsigned, little-endian short int which has the length of the # header. + import struct hlength_str = fp.read(2) if len(hlength_str) != 2: msg = "EOF at %s before reading array header length" |