Commit b68e5a37 authored by Davis King's avatar Davis King

Added functions for easily converting between python list objects and std::vector.

parent 7640dece
......@@ -20,5 +20,42 @@ inline bool hasattr(
return PyObject_HasAttrString(obj.ptr(), attr_name.c_str());
}
// ----------------------------------------------------------------------------------------
template <typename T>
std::vector<T> python_list_to_vector (
const boost::python::object& obj
)
/*!
ensures
- converts a python object into a std::vector<T> and returns it.
!*/
{
std::vector<T> vect(len(obj));
for (unsigned long i = 0; i < vect.size(); ++i)
{
vect[i] = boost::python::extract<T>(obj[i]);
}
return vect;
}
template <typename T>
boost::python::list vector_to_python_list (
const std::vector<T>& vect
)
/*!
ensures
- converts a std::vector<T> into a python list object.
!*/
{
boost::python::list obj;
for (unsigned long i = 0; i < vect.size(); ++i)
obj.append(vect[i]);
return obj;
}
// ----------------------------------------------------------------------------------------
#endif // DLIB_BOOST_PYTHON_UtILS_H__
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