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

numpy-svn at scipy.org numpy-svn at scipy.org
Mon Jul 3 16:17:40 EDT 2006


Author: oliphant
Date: 2006-07-03 15:17:32 -0500 (Mon, 03 Jul 2006)
New Revision: 2728

Modified:
   trunk/numpy/core/numeric.py
   trunk/numpy/lib/utils.py
Log:
Add a require function, a issubdtype, and a load_ctypes_function to NumPy

Modified: trunk/numpy/core/numeric.py
===================================================================
--- trunk/numpy/core/numeric.py	2006-07-03 09:41:49 UTC (rev 2727)
+++ trunk/numpy/core/numeric.py	2006-07-03 20:17:32 UTC (rev 2728)
@@ -10,7 +10,7 @@
            'alterdot', 'restoredot', 'cross',
            'array2string', 'get_printoptions', 'set_printoptions',
            'array_repr', 'array_str', 'set_string_function',
-           'little_endian',
+           'little_endian', 'require',
            'fromiter',
            'indices', 'fromfunction',
            'load', 'loads', 'isscalar', 'binary_repr', 'base_repr',
@@ -133,6 +133,39 @@
     """
     return array(a, dtype, copy=False, order='F', ndmin=1)
 
+def require(a, dtype=None, requirements=None):
+    if requirements is None:
+        requirements = []
+    else:
+        requirements = [x.upper() for x in requirements]
+
+    if not requirements:
+        return asanyarray(a, dtype=dtype)
+
+    if 'ENSUREARRAY' in requirements or 'E' in requirements:
+        func = asarray
+    else:
+        func = asanyarray
+
+    if 'FORCECAST' in requirements or 'FORCE' in requirements:
+        arr = func(a)
+        if dtype is not None and arr.dtype != dtype:
+            arr = arr.astype(dtype)
+    else:
+        arr = func(a, dtype=dtype)
+
+    copychar = 'A'
+    if 'FORTRAN' in requirements or 'F' in requirements:
+        copychar = 'F'
+    elif 'CONTIGUOUS' in requirements or 'C' in requirements:
+        copychar = 'C'
+
+    for prop in requirements:
+        if not arr.flags[prop]:
+            arr = arr.copy(copychar)
+            break
+    return arr   
+
 def isfortran(a):
     """Returns True if 'a' is laid out in Fortran-order in memory.
     """

Modified: trunk/numpy/lib/utils.py
===================================================================
--- trunk/numpy/lib/utils.py	2006-07-03 09:41:49 UTC (rev 2727)
+++ trunk/numpy/lib/utils.py	2006-07-03 20:17:32 UTC (rev 2728)
@@ -1,8 +1,10 @@
-import sys
+import sys, os
 from numpy.core.numerictypes import obj2sctype
+from numpy.core.multiarray import dtype
 
-__all__ = ['issubclass_', 'get_numpy_include', 'issubsctype', 'deprecate',
-	   'get_include']
+__all__ = ['issubclass_', 'get_numpy_include', 'issubsctype',
+           'issubdtype', 'deprecate',
+	   'get_include', 'load_ctypes_library']
 
 def issubclass_(arg1, arg2):
     try:
@@ -13,6 +15,9 @@
 def issubsctype(arg1, arg2):
     return issubclass(obj2sctype(arg1), obj2sctype(arg2))
 
+def issubdtype(arg1, arg2):
+    return issubclass(dtype(arg1).type, dtype(arg2).type)
+
 def get_include():
     """Return the directory in the package that contains the numpy/*.h header
     files.
@@ -29,6 +34,25 @@
     assert len(include_dirs)==1,`include_dirs`
     return include_dirs[0]
 
+# Adapted from Albert Strasheim
+def load_ctypes_library(libname, loader_path):
+    if '.' not in libname:
+        if sys.platform == 'win32':
+            libname = '%s.dll' % libname
+        elif sys.platform == 'darwin':
+            libname = '%s.dylib' % libname
+        else:
+            libname = '%s.so' % libname
+    loader_path = os.path.abspath(loader_path)
+    if not os.path.isdir(loader_path):
+        libdir = os.path.dirname(loader_path)
+    else:
+        libdir = loader_path
+    import ctypes
+    libpath = os.path.join(libdir, libname)
+    return ctypes.cdll[libpath]
+
+
 if sys.version_info < (2, 4):
     # Can't set __name__ in 2.3
     import new
@@ -65,3 +89,5 @@
 
 
 get_numpy_include = deprecate(get_include, 'get_numpy_include', 'get_include')
+
+




More information about the Numpy-svn mailing list