Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python
How can I return an array from C to python
Collapse
X
-
You'll have to remind me which tool you are using. Also, please post an example of what you are working on.Originally posted by shashi59Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python -
#include <Python.h>
#include <Numeric/arrayobject.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#define IDENTITY_DOC "\
This method simply unpacks the Array (so that it could be modified)\
and then returns it unchanged."
static PyObject *identity(PyObj ect *self, PyObject *args)
{
int i,n;
PyObject *input;
PyArrayObject *array;
char *aptr;
if (!PyArg_ParseTu ple(args, "O", &input ))
return NULL;
array = (PyArrayObject *) PyArray_Contigu ousFromObject(i nput, PyArray_INT, 0, 3);
if (array == NULL)
return NULL;
// Compute Size of Array
if(array->nd == 0)
n = 1;
else {
n = 1;
for(i=0;i<array->nd;i++)
n = n * array->dimensions[i];
}
aptr=(char*)(ar ray->data);
// Do Your Array Operation(s)
// Return the result
return PyArray_Return( array);
}
this is my wrapper code in c.in this code finally i was return a array (pyArray_Return (array).so how can i get the return value in python
And also i had Python2.5Comment
-
How can i return a array C to python
Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python
this is my samplecode
this is my wrapper code in c.in this code finally i was return a array (pyArray_Return (array).so how can i get the return value in pythonCode:#include <Python.h> #include <Numeric/arrayobject.h> #include <sys/types.h> #include <stdio.h> #include <stdlib.h> #define IDENTITY_DOC "\ This method simply unpacks the Array (so that it could be modified)\ and then returns it unchanged." static PyObject *identity(PyObject *self, PyObject *args) { int i,n; PyObject *input; PyArrayObject *array; char *aptr; if (!PyArg_ParseTuple(args, "O", &input )) return NULL; array = (PyArrayObject *) PyArray_ContiguousFromObject(input, PyArray_INT, 0, 3); if (array == NULL) return NULL; // Compute Size of Array if(array->nd == 0) n = 1; else { n = 1; for(i=0;i<array->nd;i++) n = n * array->dimensions[i]; } aptr=(char*)(array->data); // Do Your Array Operation(s) // Return the result return PyArray_Return(array); }
And also i had Python2.5Comment
-
Hello, card. Welcome to the Python Forum on TheScripts.Originally posted by cardDid you ever figure this out? I'm new to the forum, but I have the answer if you still need it.
Please feel free to update any posts that may assist others in the future.
We appreciate any contribution and hope to be able to assist you with you queries.
Thanks for joining.Comment
Comment