npctypes.shared module

npctypes.shared.as_ndarray(*args, **kwds)[source]

Context manager to provide NumPy ndarray views of NDArray instances.

Parameters:
  • shape (tuple of ints) – Shape of the array to allocate.
  • dtype (type) – Type of the array to allocate.
  • order (char or None) – Order of the array (‘C’, ‘F’, or None). Defaults to None.
Returns:

Custom Array instance allocated on the

shared process heap.

Return type:

ctypes.Array

Examples

>>> numpy.set_printoptions(legacy="1.13")
>>> a = ndarray((2,3), float)
>>> with as_ndarray(a) as nd_a:
...     nd_a[...] = 0
...     print(nd_a)
[[ 0.  0.  0.]
 [ 0.  0.  0.]]
>>> a = ndarray((2,3), float)
>>> with as_ndarray(a) as nd_a:
...     for i in range(nd_a.size):
...         nd_a.flat[i] = i
...
...     print(nd_a)
[[ 0.  1.  2.]
 [ 3.  4.  5.]]
npctypes.shared.ndarray(shape, dtype, order=None)[source]

Factory to generate N-D Arrays shared across process boundaries.

This creates a custom dynamic type (if one doesn’t already exist) that is a ctypes.Array instance. If one does already exist, we reuse it so that things like type comparisons work. In addition to the typical properties that ``ctypes.Array``s have, this tracks its number of dimensions, shape, and order (‘C’ or ‘F’ for C and Fortran respectively). Having this information allows us to easily construct a NumPy ndarray in other processes.

Parameters:
  • shape (tuple of ints) – Shape of the array to allocate.
  • dtype (type) – Type of the array to allocate.
  • order (char or None) – Order of the array (‘C’, ‘F’, or None). Defaults to None.
Returns:

Custom Array (NDArray) instance

allocated on the shared process heap.

Return type:

ctypes.Array

Examples

>>> ndarray((2,3), float)               # doctest: +ELLIPSIS
<npctypes.shared.NDArray_<f8_2d_2x3_C object at 0x...>
>>> ndarray((2,3), float, order='F')    # doctest: +ELLIPSIS
<npctypes.shared.NDArray_<f8_2d_2x3_F object at 0x...>