| numpy.ctypeslib | index /usr/lib/python2.6/dist-packages/numpy/ctypeslib.py |
============================
``ctypes`` Utility Functions
============================
See Also
---------
load_library : Load a C library.
ndpointer : Array restype/argtype with verification.
as_ctypes : Create a ctypes array from an ndarray.
as_array : Create an ndarray from a ctypes array.
References
----------
.. [1] "SciPy Cookbook: ctypes", http://www.scipy.org/Cookbook/Ctypes
Examples
--------
Load the C library:
>>> _lib = np.ctypeslib.load_library('libmystuff', '.') #DOCTEST: +ignore
Our result type, an ndarray that must be of type double, be 1-dimensional
and is C-contiguous in memory:
>>> array_1d_double = np.ctypeslib.ndpointer(
... dtype=np.double,
... ndim=1, flags='CONTIGUOUS') #DOCTEST: +ignore
Our C-function typically takes an array and updates its values
in-place. For example::
void foo_func(double* x, int length)
{
int i;
for (i = 0; i < length; i++) {
x[i] = i*i;
}
}
We wrap it using:
>>> lib.foo_func.restype = None #DOCTEST: +ignore
>>> lib.foo.argtypes = [array_1d_double, c_int] #DOCTEST: +ignore
Then, we're ready to call ``foo_func``:
>>> out = np.empty(15, dtype=np.double)
>>> _lib.foo_func(out, len(out)) #DOCTEST: +ignore
| Modules | ||||||
| ||||||
| Classes | ||||||||
|
| ||||||||
| Functions | ||
| ||
| Data | ||
| TYPESTR = '<%c%d' __all__ = ['load_library', 'ndpointer', 'test', 'ctypes_load_library', 'c_intp', 'as_ctypes', 'as_array'] __file__ = '/usr/lib/python2.6/dist-packages/numpy/ctypeslib.pyc' __name__ = 'numpy.ctypeslib' __package__ = 'numpy' _flagdict = {'A': 256, 'ALIGNED': 256, 'C': 1, 'CONTIGUOUS': 1, 'C_CONTIGUOUS': 1, 'F': 2, 'FORTRAN': 2, 'F_CONTIGUOUS': 2, 'O': 4, 'OWNDATA': 4, ...} _flagnames = ['C_CONTIGUOUS', 'F_CONTIGUOUS', 'ALIGNED', 'WRITEABLE', 'OWNDATA', 'UPDATEIFCOPY'] _pointer_type_cache = {} _typecodes = {'<f4': <class 'ctypes.c_float'>, '<f8': <class 'ctypes.c_double'>, '<i1': <class 'ctypes.c_byte'>, '<i2': <class 'ctypes.c_short'>, '<i4': <class 'ctypes.c_int'>, '<i8': <class 'ctypes.c_long'>, '<u1': <class 'ctypes.c_ubyte'>, '<u2': <class 'ctypes.c_ushort'>, '<u4': <class 'ctypes.c_uint'>, '<u8': <class 'ctypes.c_ulong'>} code = 'f' simple_types = [((<class 'ctypes.c_byte'>, <class 'ctypes.c_short'>, <class 'ctypes.c_int'>, <class 'ctypes.c_long'>, <class 'ctypes.c_long'>), 'i'), ((<class 'ctypes.c_ubyte'>, <class 'ctypes.c_ushort'>, <class 'ctypes.c_uint'>, <class 'ctypes.c_ulong'>, <class 'ctypes.c_ulong'>), 'u'), ((<class 'ctypes.c_float'>, <class 'ctypes.c_double'>), 'f')] types = (<class 'ctypes.c_float'>, <class 'ctypes.c_double'>) | ||