0

I am working on a Python C-extension code. Currently, a 1D C-array is converted to a Python List. Now I need to convert an ND-array, described by a 1) data type, 2) shape (as a 1D integer vector, for example, 2x4x3 array), and 3) data binary payload as serialized array elements of data-type.

I noticed that there is an array object in python: https://docs.python.org/2/library/array.html

I prefer to convert the ND array buffer to an array object instead of requiring installation of numpy ndarray - an additional dependency, but I failed to find any C-API to create/read/write Python-array objects from C.

does this interface (something similar to PyList_New) exist?

FangQ
  • 1,444
  • 10
  • 18
  • 1
    The Python `array` class is not multi-dimensional. But for interface the class offers a `buffer protocol`. Also switch to Py3 if you can. If you don't want to use `numpy`, why the [numpy] tag? – hpaulj Jun 17 '20 at 04:15
  • I found the buffer object API, which supports shapes (ND) and data types: https://docs.python.org/2/c-api/buffer.html#the-new-style-py-buffer-struct is a buffer object good for this purpose? – FangQ Jun 17 '20 at 04:20
  • 1
    I haven't used it so can't help. BUT, what do you intend to do with these 'arrays' in Python. Without `numpy` or `PIL` it doesn't look like you can do much; at least I'm not aware of any builtin packages that use attributes like shape and strides. – hpaulj Jun 17 '20 at 04:28
  • 1
    You should almost certainly use either nested lists or NumPy. `array.array` arrays will be useless to you, and the buffer API is mostly useful for C-level access, not for providing a Python interface. – user2357112 Jun 17 '20 at 04:44
  • is there a convenient/short way to create multi-dimensional list? the data loading may have various dimensions , 3D, or 4D, a nested loop is a bit cumbersome? alternatively, a list reshape function in C-API would also solve this problem. this is in C. – FangQ Jun 17 '20 at 13:35
  • if I switch to numpy C-APIs, I assume a user of the module must pre-install numpy, correct? – FangQ Jun 17 '20 at 13:42

0 Answers0