Commit 6980697a authored by Davis King's avatar Davis King

Just moved code into dlib namespace.

parent 43e5f42e
......@@ -10,69 +10,73 @@
namespace py = pybind11;
template <typename T>
std::vector<T> python_list_to_vector (
const py::list& obj
)
/*!
ensures
- converts a python object into a std::vector<T> and returns it.
!*/
namespace dlib
{
std::vector<T> vect(len(obj));
for (unsigned long i = 0; i < vect.size(); ++i)
template <typename T>
std::vector<T> python_list_to_vector (
const py::list& obj
)
/*!
ensures
- converts a python object into a std::vector<T> and returns it.
!*/
{
vect[i] = obj[i].cast<T>();
std::vector<T> vect(len(obj));
for (unsigned long i = 0; i < vect.size(); ++i)
{
vect[i] = obj[i].cast<T>();
}
return vect;
}
return vect;
}
template <typename T>
py::list vector_to_python_list (
const std::vector<T>& vect
)
/*!
ensures
- converts a std::vector<T> into a python list object.
!*/
{
py::list obj;
for (unsigned long i = 0; i < vect.size(); ++i)
obj.append(vect[i]);
return obj;
}
template <typename T>
py::list vector_to_python_list (
const std::vector<T>& vect
)
/*!
ensures
- converts a std::vector<T> into a python list object.
!*/
{
py::list obj;
for (unsigned long i = 0; i < vect.size(); ++i)
obj.append(vect[i]);
return obj;
}
template <typename T>
void extend_vector_with_python_list (
std::vector<T> &v,
const py::list &l
)
/*!
ensures
- appends items from a python list to the end of std::vector<T>.
!*/
{
for (const auto &item : l)
v.push_back(item.cast<T>());
}
template <typename T>
void extend_vector_with_python_list (
std::vector<T> &v,
const py::list &l
)
/*!
ensures
- appends items from a python list to the end of std::vector<T>.
!*/
{
for (const auto &item : l)
v.push_back(item.cast<T>());
}
// ----------------------------------------------------------------------------------------
template <typename T>
std::shared_ptr<T> load_object_from_file (
const std::string& filename
)
/*!
ensures
- deserializes an object of type T from the given file and returns it.
!*/
{
std::ifstream fin(filename.c_str(), std::ios::binary);
if (!fin)
throw dlib::error("Unable to open " + filename);
auto obj = std::make_shared<T>();
deserialize(*obj, fin);
return obj;
template <typename T>
std::shared_ptr<T> load_object_from_file (
const std::string& filename
)
/*!
ensures
- deserializes an object of type T from the given file and returns it.
!*/
{
std::ifstream fin(filename.c_str(), std::ios::binary);
if (!fin)
throw dlib::error("Unable to open " + filename);
auto obj = std::make_shared<T>();
deserialize(*obj, fin);
return obj;
}
}
// ----------------------------------------------------------------------------------------
......
......@@ -8,58 +8,65 @@
#include <sstream>
#include <dlib/vectorstream.h>
template<typename T>
py::tuple getstate(const T& item)
{
using namespace dlib;
std::vector<char> buf;
buf.reserve(5000);
vectorstream sout(buf);
serialize(item, sout);
return py::make_tuple(py::handle(
PyBytes_FromStringAndSize(buf.size()?&buf[0]:0, buf.size())));
}
namespace py = pybind11;
template<typename T>
T setstate(py::tuple state)
namespace dlib
{
using namespace dlib;
if (len(state) != 1)
{
PyErr_SetObject(PyExc_ValueError,
py::str("expected 1-item tuple in call to __setstate__; got {}").format(state).ptr()
);
throw py::error_already_set();
}
// We used to serialize by converting to a str but the boost.python routines for
// doing this don't work in Python 3. You end up getting an error about invalid
// UTF-8 encodings. So instead we access the python C interface directly and use
// bytes objects. However, we keep the deserialization code that worked with str
// for backwards compatibility with previously pickled files.
T item;
py::object obj = state[0];
if (py::isinstance<py::str>(obj))
template<typename T>
py::tuple getstate(const T& item)
{
py::str data = state[0].cast<py::str>();
std::string temp = data;
std::istringstream sin(temp);
deserialize(item, sin);
using namespace dlib;
std::vector<char> buf;
buf.reserve(5000);
vectorstream sout(buf);
serialize(item, sout);
return py::make_tuple(py::handle(
PyBytes_FromStringAndSize(buf.size()?&buf[0]:0, buf.size())));
}
else if(PyBytes_Check(py::object(state[0]).ptr()))
template<typename T>
T setstate(py::tuple state)
{
using namespace dlib;
if (len(state) != 1)
{
PyErr_SetObject(PyExc_ValueError,
py::str("expected 1-item tuple in call to __setstate__; got {}").format(state).ptr()
);
throw py::error_already_set();
}
// We used to serialize by converting to a str but the boost.python routines for
// doing this don't work in Python 3. You end up getting an error about invalid
// UTF-8 encodings. So instead we access the python C interface directly and use
// bytes objects. However, we keep the deserialization code that worked with str
// for backwards compatibility with previously pickled files.
T item;
py::object obj = state[0];
char* data = PyBytes_AsString(obj.ptr());
unsigned long num = PyBytes_Size(obj.ptr());
std::istringstream sin(std::string(data, num));
deserialize(item, sin);
}
else
{
throw error("Unable to unpickle, error in input file.");
if (py::isinstance<py::str>(obj))
{
py::str data = state[0].cast<py::str>();
std::string temp = data;
std::istringstream sin(temp);
deserialize(item, sin);
}
else if(PyBytes_Check(py::object(state[0]).ptr()))
{
py::object obj = state[0];
char* data = PyBytes_AsString(obj.ptr());
unsigned long num = PyBytes_Size(obj.ptr());
std::istringstream sin(std::string(data, num));
deserialize(item, sin);
}
else
{
throw error("Unable to unpickle, error in input file.");
}
return item;
}
return item;
}
#endif // DLIB_SERIALIZE_PiCKLE_Hh_
......
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