Commit 3a5a05b0 authored by Purves, Murray's avatar Purves, Murray
Browse files

Refactored conversion methods to utility file

parent 6acabfbf
Pipeline #27477 failed with stages
in 1 minute and 52 seconds
#include <Python.h>
#include <stdexcept>
#include <vector>
#include "../interpolate.hh"
//#include "radixmath/interpolate.hh"
/**
* Conversion from PyObject to double vector.
* Adapted from https://gist.github.com/rjzak/5681680
*/
std::vector<double> pyObjectToDoubleVector(PyObject* object)
{
std::vector<double> vector;
// Different conversion methods for list and tuple objects
if (PyTuple_Check(object))
{
for(Py_ssize_t i = 0; i < PyTuple_Size(object); ++i)
{
PyObject* value = PyTuple_GetItem(object, i);
vector.push_back(PyFloat_AsDouble(value));
}
}
else if (PyList_Check(object))
{
for(Py_ssize_t i = 0; i < PyList_Size(object); ++i)
{
PyObject* value = PyList_GetItem(object, i);
vector.push_back(PyFloat_AsDouble(value));
}
}
else
{
throw std::logic_error(
"Input PyObject was not a list or tuple - cannot convert");
}
return vector;
}
/**
* Conversion from double vector to PyObject list.
* Adapted from https://gist.github.com/rjzak/5681680
*/
PyObject* doubleVectorToListPyObject(const std::vector<double> &vector)
{
PyObject* list = PyList_New(vector.size());
if (!list)
{
throw std::logic_error("Unable to initialise Python list - cannot convert");
}
for(size_t i = 0; i < vector.size(); ++i)
{
PyObject* value = PyFloat_FromDouble(vector[i]);
if(!value)
{
Py_DECREF(list);
throw std::logic_error("Unable to convert list member - cannot convert");
}
PyList_SetItem(list, i, value);
}
return list;
}
#include "python_utils.hh"
static PyObject* interpolateValues(PyObject* self, PyObject* args)
{
......@@ -97,6 +38,19 @@ static PyObject* interpolateValues(PyObject* self, PyObject* args)
return interpolatedValuesObj;
}
// static PyObject* interpolateValues(PyObject* self, PyObject* args)
// {
// int input;
// if (!PyArg_ParseTuple(args, "i", &input))
// {
// return NULL;
// }
// long square = (long)input * (long)input;
// return PyLong_FromLong(square);
// }
static PyMethodDef interpolate_methods[] = {
{"interpolateValues", interpolateValues, METH_VARARGS, "Interpolate/extrapolate missing values from a data vector"},
{NULL, NULL, 0, NULL},
......
#include <Python.h>
#include <stdexcept>
#include <vector>
/**
* Conversion from PyObject to double vector.
* Adapted from https://gist.github.com/rjzak/5681680
*/
static inline std::vector<double> pyObjectToDoubleVector(PyObject* object)
{
std::vector<double> vector;
// Different conversion methods for list and tuple objects
if (PyTuple_Check(object))
{
for(Py_ssize_t i = 0; i < PyTuple_Size(object); ++i)
{
PyObject* value = PyTuple_GetItem(object, i);
vector.push_back(PyFloat_AsDouble(value));
}
}
else if (PyList_Check(object))
{
for(Py_ssize_t i = 0; i < PyList_Size(object); ++i)
{
PyObject* value = PyList_GetItem(object, i);
vector.push_back(PyFloat_AsDouble(value));
}
}
else
{
throw std::logic_error(
"Input PyObject was not a list or tuple - cannot convert");
}
return vector;
}
/**
* Conversion from double vector to PyObject list.
* Adapted from https://gist.github.com/rjzak/5681680
*/
static inline PyObject* doubleVectorToListPyObject(const std::vector<double> &vector)
{
PyObject* list = PyList_New(vector.size());
if (!list)
{
throw std::logic_error("Unable to initialise Python list - cannot convert");
}
for(size_t i = 0; i < vector.size(); ++i)
{
PyObject* value = PyFloat_FromDouble(vector[i]);
if(!value)
{
Py_DECREF(list);
throw std::logic_error("Unable to convert list member - cannot convert");
}
PyList_SetItem(list, i, value);
}
return list;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment