[Numpy-svn] r5715 - in trunk/numpy: . core lib

numpy-svn@scip... numpy-svn@scip...
Wed Aug 27 09:31:18 CDT 2008


Author: oliphant
Date: 2008-08-27 09:31:18 -0500 (Wed, 27 Aug 2008)
New Revision: 5715

Modified:
   trunk/numpy/_import_tools.py
   trunk/numpy/core/memmap.py
   trunk/numpy/core/numerictypes.py
   trunk/numpy/lib/_datasource.py
   trunk/numpy/lib/format.py
Log:
Apply modified version of Andrew Dalke's patch in #874 to create a quicker-loading numpy.

Modified: trunk/numpy/_import_tools.py
===================================================================
--- trunk/numpy/_import_tools.py	2008-08-27 10:09:59 UTC (rev 5714)
+++ trunk/numpy/_import_tools.py	2008-08-27 14:31:18 UTC (rev 5715)
@@ -2,7 +2,6 @@
 import os
 import sys
 import imp
-from glob import glob
 
 __all__ = ['PackageLoader']
 

Modified: trunk/numpy/core/memmap.py
===================================================================
--- trunk/numpy/core/memmap.py	2008-08-27 10:09:59 UTC (rev 5714)
+++ trunk/numpy/core/memmap.py	2008-08-27 14:31:18 UTC (rev 5715)
@@ -1,6 +1,5 @@
 __all__ = ['memmap']
 
-import mmap
 import warnings
 from numeric import uint8, ndarray, dtype
 
@@ -173,6 +172,8 @@
     __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:

Modified: trunk/numpy/core/numerictypes.py
===================================================================
--- trunk/numpy/core/numerictypes.py	2008-08-27 10:09:59 UTC (rev 5714)
+++ trunk/numpy/core/numerictypes.py	2008-08-27 14:31:18 UTC (rev 5715)
@@ -88,10 +88,23 @@
 
 # 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.
 

Modified: trunk/numpy/lib/_datasource.py
===================================================================
--- trunk/numpy/lib/_datasource.py	2008-08-27 10:09:59 UTC (rev 5714)
+++ trunk/numpy/lib/_datasource.py	2008-08-27 14:31:18 UTC (rev 5715)
@@ -36,22 +36,40 @@
 
 import os
 from shutil import rmtree
-from urlparse import urlparse
 
+# 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?
-_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
+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 @@
 
     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,7 +297,10 @@
             `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:
         #        path = /home/guido/datafile.txt

Modified: trunk/numpy/lib/format.py
===================================================================
--- trunk/numpy/lib/format.py	2008-08-27 10:09:59 UTC (rev 5714)
+++ trunk/numpy/lib/format.py	2008-08-27 14:31:18 UTC (rev 5715)
@@ -57,7 +57,6 @@
 """
 
 import cPickle
-import struct
 
 import numpy
 from numpy.lib.utils import safe_eval
@@ -180,6 +179,7 @@
         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 @@
     """
     # 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"



More information about the Numpy-svn mailing list