Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
LEFEBVREJP email
radix
Commits
3a5a05b0
Commit
3a5a05b0
authored
Jan 10, 2019
by
Purves, Murray
Browse files
Refactored conversion methods to utility file
parent
6acabfbf
Pipeline
#27477
failed with stages
in 1 minute and 52 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
radixmath/python/interpolatemodule.cc
View file @
3a5a05b0
#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
},
...
...
radixmath/python/python_utils.hh
0 → 100644
View file @
3a5a05b0
#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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment