Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. typedef struct {
  2. PyObject_HEAD
  3. PyTypeObject *typeobj;
  4. char kind;
  5. char type;
  6. char byteorder;
  7. char unused;
  8. int flags;
  9. int type_num;
  10. int elsize;
  11. int alignment;
  12. PyArray_ArrayDescr *subarray;
  13. PyObject *fields;
  14. PyArray_ArrFuncs *f;
  15. } PyArray_Descr;
  16.  
  17. #include <Python.h>
  18.  
  19. #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
  20. #include <arrayobject.h>
  21.  
  22. #include <vector>
  23. #include <iostream>
  24.  
  25. using namespace std;
  26.  
  27. int *init() {
  28. import_array();
  29. return NULL;
  30. }
  31.  
  32. int main(int argc, char const *argv[])
  33. {
  34. PyObject *pModule, *pFunc, *pResp, *sysPath;
  35. PyArrayObject *pArr, *pfieldArr;
  36. PyArray_Descr *dtype;
  37. PyObject *tnames, *name;
  38. Py_ssize_t N;
  39. PyObject *recItem;
  40.  
  41. Py_Initialize();
  42. init();
  43.  
  44. sysPath = PySys_GetObject("path");
  45. PyList_Append(sysPath, PyUnicode_FromString("/tmp"));
  46. pModule = PyImport_Import(PyUnicode_FromString("myscript"));
  47.  
  48. pFunc = PyObject_GetAttrString(pModule, "main");
  49. pResp = PyObject_CallObject(pFunc, nullptr);
  50.  
  51. pArr = (PyArrayObject *) pResp;
  52. dtype = PyArray_DTYPE(pArr);
  53.  
  54. tnames = PySequence_Fast(dtype->names, nullptr);
  55. N = PySequence_Fast_GET_SIZE(tnames);
  56.  
  57. for (Py_ssize_t i = 0; i < N; i++) { // цикл по полям массива
  58. name = PySequence_Fast_GET_ITEM(tnames, i); // имя поля
  59.  
  60. recItem = PyObject_GetItem(pResp, name);
  61. pfieldArr = (PyArrayObject *) recItem; // вектор значений по полю
  62. dtype = PyArray_DTYPE(pfieldArr);
  63.  
  64. cout << dtype->type << "t" << dtype->type_num << endl;
  65. // вывод символьного и порядковых кодов типа
  66. // std::vector< %DTYPE_TO_CTYPE% > v; // - как хотелось бы
  67. }
  68. return 0;
  69. }
  70.  
  71. def main():
  72. import numpy as np
  73. return np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement