Daras
Daras

Reputation: 19

How to give a name to an function arguments - written as C++ extension for Python?

Here is screenshot of my problem: enter image description here

I've written a function in C++ to extend my Python module, but when i try to call this function in Python despite argument name which is "labels_list" i see "*args, **kwargs". Can i somehow change it?

static PyObject* encode_one_hot(PyObject* self, PyObject* args, PyObject* kwargs) {

PyArrayObject* labels = NULL;
PyArrayObject* one_hot;

npy_intp dims[2];

map<int, int> classes_map;

int current_label;
int labels_size;

int new_numeration = 0;

void* ptr;

static char* kwlist[] = { (char*)"labels_list", NULL };

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &labels))
    return NULL;


PyArray_Sort(labels, 0, NPY_QUICKSORT);

labels_size = PyArray_SIZE(labels);

for (int i = 0; i < labels_size; i++) {
    ptr = PyArray_GETPTR1(labels, i);
    current_label = PyLong_AsLong(PyArray_GETITEM(labels, ptr));

    if (classes_map.find(current_label) == classes_map.end()) {
        classes_map[current_label] = new_numeration;
        new_numeration++;
    }
}

dims[0] = labels_size;
dims[1] = (int)classes_map.size();

one_hot = (PyArrayObject*)PyArray_ZEROS(2, dims, NPY_INT, 0);

for (int i = 0; i < labels_size; i++) {
    current_label = classes_map[PyLong_AsLong(PyArray_GETITEM(labels, PyArray_GETPTR1(labels, i)))];
    ptr = PyArray_GETPTR2(one_hot, i, current_label);

    PyArray_SETITEM(one_hot, ptr, PyLong_FromLong(1));
}     
return PyArray_Return(one_hot); }

Upvotes: 0

Views: 452

Answers (1)

Daras
Daras

Reputation: 19

I've solved the problem by 2 hours of exploring matplotlib github repo :D

If you want to name your function parameters you should do something like this:

Screenshot

You just have to in the top of "ml_doc" field type "your_function_name(arguments_names)", and thats all.

Upvotes: 1

Related Questions