Skip to content

Commit

Permalink
Add numpy_ptr to generate a pointer type for the array.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakirkham committed Oct 3, 2016
1 parent 252e926 commit 003dc79
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions npctypes/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,45 @@ def ctype(a_type):
"""

return(type(numpy.ctypeslib.as_ctypes(numpy.array(0, dtype=a_type))))


def numpy_ptr(a):
"""
Takes a numpy.ndarray and gets a pointer type for that array.
Args:
a(ndarray): the ndarray to get the pointer type for.
Returns:
(PyCSimpleType): the pointer type associated with this array.
Examples:
>>> a = numpy.zeros((3, 4), dtype=float)
>>> a_ptr = numpy_ptr(a)
>>> a_ptr
<class 'numpy.ctypeslib.ndpointer_<f8_2d_3x4_C_CONTIGUOUS_ALIGNED_WRITEABLE_OWNDATA'>
>>> a_ptr._dtype_
dtype('float64')
>>> a_ptr._ndim_
2
>>> a_ptr._shape_
(3, 4)
>>> a_ptr._flags_
1285
>>> numpy.ctypeslib.flagsobj(a_ptr._flags_)
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : False
ALIGNED : True
UPDATEIFCOPY : False
"""

return(numpy.ctypeslib.ndpointer(
dtype=a.dtype,
ndim=a.ndim,
shape=a.shape,
flags=a.flags
))

0 comments on commit 003dc79

Please sign in to comment.