Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct {
- PyObject_HEAD
- PyTypeObject *typeobj;
- char kind;
- char type;
- char byteorder;
- char unused;
- int flags;
- int type_num;
- int elsize;
- int alignment;
- PyArray_ArrayDescr *subarray;
- PyObject *fields;
- PyArray_ArrFuncs *f;
- } PyArray_Descr;
- #include <Python.h>
- #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
- #include <arrayobject.h>
- #include <vector>
- #include <iostream>
- using namespace std;
- int *init() {
- import_array();
- return NULL;
- }
- int main(int argc, char const *argv[])
- {
- PyObject *pModule, *pFunc, *pResp, *sysPath;
- PyArrayObject *pArr, *pfieldArr;
- PyArray_Descr *dtype;
- PyObject *tnames, *name;
- Py_ssize_t N;
- PyObject *recItem;
- Py_Initialize();
- init();
- sysPath = PySys_GetObject("path");
- PyList_Append(sysPath, PyUnicode_FromString("/tmp"));
- pModule = PyImport_Import(PyUnicode_FromString("myscript"));
- pFunc = PyObject_GetAttrString(pModule, "main");
- pResp = PyObject_CallObject(pFunc, nullptr);
- pArr = (PyArrayObject *) pResp;
- dtype = PyArray_DTYPE(pArr);
- tnames = PySequence_Fast(dtype->names, nullptr);
- N = PySequence_Fast_GET_SIZE(tnames);
- for (Py_ssize_t i = 0; i < N; i++) { // цикл по полям массива
- name = PySequence_Fast_GET_ITEM(tnames, i); // имя поля
- recItem = PyObject_GetItem(pResp, name);
- pfieldArr = (PyArrayObject *) recItem; // вектор значений по полю
- dtype = PyArray_DTYPE(pfieldArr);
- cout << dtype->type << "t" << dtype->type_num << endl;
- // вывод символьного и порядковых кодов типа
- // std::vector< %DTYPE_TO_CTYPE% > v; // - как хотелось бы
- }
- return 0;
- }
- def main():
- import numpy as np
- return np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement