summaryrefslogtreecommitdiff
path: root/numpy
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
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')
-rw-r--r--numpy/_import_tools.py1
-rw-r--r--numpy/core/memmap.py3
-rw-r--r--numpy/core/numerictypes.py19
-rw-r--r--numpy/lib/_datasource.py50
-rw-r--r--numpy/lib/format.py3
5 files changed, 57 insertions, 19 deletions
diff --git a/numpy/_import_tools.py b/numpy/_import_tools.py
index b06fbff7e..4bda26d87 100644
--- a/numpy/_import_tools.py
+++ b/numpy/_import_tools.py
@@ -2,7 +2,6 @@
import os
import sys
import imp
-from glob import glob
__all__ = ['PackageLoader']
diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py
index 05aaefef0..dd09b8e28 100644
--- a/numpy/core/memmap.py
+++ b/numpy/core/memmap.py
@@ -1,6 +1,5 @@
__all__ = ['memmap']
-import mmap
import warnings
from numeric import uint8, ndarray, dtype
@@ -173,6 +172,8 @@ class memmap(ndarray):
__array_priority__ = -100.0
def __new__(subtype, filename, dtype=uint8, mode='r+', offset=0,
shape=None, order='C'):
+ # Import here to minimize 'import numpy' overhead
+ import mmap
try:
mode = mode_equivalents[mode]
except KeyError:
diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py
index a368a2bec..8a89f40c6 100644
--- a/numpy/core/numerictypes.py
+++ b/numpy/core/numerictypes.py
@@ -88,9 +88,22 @@ from __builtin__ import bool, int, long, float, complex, object, unicode, str
# String-handling utilities to avoid locale-dependence.
-import string
-LOWER_TABLE = string.maketrans(string.ascii_uppercase, string.ascii_lowercase)
-UPPER_TABLE = string.maketrans(string.ascii_lowercase, string.ascii_uppercase)
+# "import string" is costly to import!
+# Construct the translation tables directly
+# "A" = chr(65), "a" = chr(97)
+_all_chars = map(chr, range(256))
+_ascii_upper = _all_chars[65:65+26]
+_ascii_lower = _all_chars[97:97+26]
+LOWER_TABLE="".join(_all_chars[:65] + _ascii_lower + _all_chars[65+26:])
+UPPER_TABLE="".join(_all_chars[:97] + _ascii_upper + _all_chars[97+26:])
+
+#import string
+# assert (string.maketrans(string.ascii_uppercase, string.ascii_lowercase) == \
+# LOWER_TABLE)
+# assert (string.maketrnas(string_ascii_lowercase, string.ascii_uppercase) == \
+# UPPER_TABLE)
+#LOWER_TABLE = string.maketrans(string.ascii_uppercase, string.ascii_lowercase)
+#UPPER_TABLE = string.maketrans(string.ascii_lowercase, string.ascii_uppercase)
def english_lower(s):
""" Apply English case rules to convert ASCII strings to all lower case.
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"