Attention une mise à jour du serveur va être effectuée le vendredi 16 avril entre 12h et 12h30. Cette mise à jour va générer une interruption du service de quelques minutes.

Commit f9e88721 authored by Laurent Belcour's avatar Laurent Belcour

Updating the Python interface. It is now functional but not very complete.

parent dce30992
......@@ -13,24 +13,30 @@
#define bp boost::python
// here comes the magic
/* The following code register ALTA's shared pointer as a valid shared ptr
* to be used by boost::python .
*/
template <typename T>
T* get_pointer(ptr<T> const& p) {
//notice the const_cast<> at this point
//for some unknown reason, bp likes to have it like that
return const_cast<T*>(p.get());
}
// some boost.python plumbing is required as you already know
namespace boost { namespace python {
template <typename T>
struct pointee< ::ptr<T> > {
typedef T type;
};
namespace boost {
namespace python {
template <typename T>
struct pointee< ::ptr<T> > {
typedef T type;
};
}
}
}}
/* Wrapper to ALTA's vec class. This is only here to allow init with Python's
* list.
*
* TODO: Make sure that the value passed to this vector are floatting point
* convertible.
*/
struct my_vec : public vec {
my_vec() : vec() {}
......@@ -41,18 +47,52 @@ struct my_vec : public vec {
}
};
/* This class is a wrapper to ALTA's arguments class to add Python specific
* behaviour such as dictionnary initialization.
*/
struct python_arguments : public arguments {
python_arguments() : arguments() {}
python_arguments(bp::dict d) : arguments() {
bp::list keys = d.keys();
for(int i=0; i<bp::len(keys); ++i) {
const std::string s_key = bp::extract<std::string>(keys[i]);
const std::string s_val = bp::extract<std::string>(d[keys[i]]);
this->update(s_key, s_val);
}
}
};
/* Create a data object from a plugin's name and the data filename. This
* function is here to accelerate the loading of data file.
*/
ptr<data> load_data(const std::string& plugin_name, const std::string& filename) {
ptr<data> d = plugins_manager::get_data(plugin_name);
d->load(filename);
return d;
}
// Creating functions for the plugins_manager calls
function* get_function(const std::string& filename) {
return plugins_manager::get_function(filename);
/* Creating functions for the plugins_manager calls
*
* TODO: Throw python exceptions if the function is not correctly created.
* Those function should disapear when the return type of get_Function
* in the plugin_manager will be ptr<function>.
*/
ptr<function> get_function(const std::string& filename) {
ptr<function> func(plugins_manager::get_function(filename));
if(!func) {
std::cerr << "<<ERROR>> no function created" << std::endl;
}
return func;
}
function* get_function_from_args(const arguments& args) {
return plugins_manager::get_function(args);
ptr<function> get_function_from_args(const arguments& args) {
ptr<function> func(plugins_manager::get_function(args));
if(!func) {
std::cerr << "<<ERROR>> no function created" << std::endl;
}
return func;
}
/* Exporting the ALTA module */
......@@ -60,9 +100,9 @@ BOOST_PYTHON_MODULE(alta)
{
// Argument class
//
// TODO: {Laurent: we should be able to set arguments using python's maps}
bp::class_<arguments>("arguments")
bp::class_<python_arguments>("arguments")
.def(bp::init<>())
.def(bp::init<bp::dict>())
.def("update", &arguments::update);
bp::class_<my_vec>("vec")
......@@ -72,25 +112,23 @@ BOOST_PYTHON_MODULE(alta)
// Function interface
//
bp::class_<function, ptr<function>, boost::noncopyable>("function", bp::no_init)
.def("value", &function::value);
bp::def("get_function", get_function, bp::return_value_policy<bp::manage_new_object>());
bp::def("get_function", get_function_from_args, bp::return_value_policy<bp::manage_new_object>());
.def("value", &function::value)
.def("load", &function::load)
.def("save", &function::save);
bp::def("get_function", get_function);
bp::def("get_function", get_function_from_args);
// Data interface
//
bp::class_<data, ptr<data>, boost::noncopyable>("data", bp::no_init)
.def("load", static_cast< void(data::*)(const std::string&)>(&data::load))
.def("size", &data::size);
bp::def("get_data", plugins_manager::get_data);
.def("size", &data::size)
.def("save", &data::save);
bp::def("get_data", plugins_manager::get_data);
bp::def("load_data", load_data);
// Fitter interface
//
// TODO: {Laurent: to make the call to fit_data possible, we need to
// implement a Python converter to ptr<T>. This seems to be
// tricky (see boost/python/shared_ptr_to_python.hpp). The
// other solution is to use boost's shared_ptr instead of ptr
// when compiling for the Python interface.}
bp::class_<fitter, ptr<fitter>, boost::noncopyable>("fitter", bp::no_init)
.def("fit_data", &fitter::fit_data);
bp::def("get_fitter", plugins_manager::get_fitter);
......
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