//#define BOOST_PYTHON_STATIC_LIB #include #include #include #include #include #include #include #include #include #include "json.hpp" namespace obp { using namespace std; namespace bp = boost::python; namespace bn = bp::numpy; template std::string operator%(std::string fmt, std::tuple args) { char line[800]; sprintf(line, fmt.c_str(), args); return line; } char const *greet() { // string str; // auto a = "%d-%d" % std::tuple(2, 3); return "hello, world"; } struct World { void set(string msg) { this->msg = msg; } string greet() { return msg; } string msg; }; class ArrayHandler { public: ArrayHandler(); bp::object Generate(); bp::object GeneratebyTuple(bp::object &data); bp::object GeneratebyList(bp::object &data); bp::object Reshape(bp::object &data); void ShowData(bp::object &data); void DataType(bp::object &data); }; ArrayHandler::ArrayHandler() { } bp::object ArrayHandler::Generate() { bp::tuple shape = bp::make_tuple(4, 4); bn::dtype type = bn::dtype::get_builtin(); bn::ndarray newArray = bn::zeros(shape, type); //bn::ndarray newArray = bn::empty(shape, type); return newArray; } bp::object ArrayHandler::GeneratebyTuple(bp::object &data) { bp::tuple dataList = (bp::tuple)data; bn::ndarray newArray = bn::array(dataList); return newArray; } bp::object ArrayHandler::GeneratebyList(bp::object &data) { bp::list dataList = (bp::list)data; bn::ndarray newArray = bn::array(dataList); return newArray; } bp::object ArrayHandler::Reshape(bp::object &data) { bn::ndarray dataArray = bn::from_object(data); for (int i = 0; i < dataArray.get_nd(); i++) { std::cout << "Size of Dim" << i + 1 << ": " << dataArray.get_shape()[i] << std::endl; } bp::tuple newShape = bp::make_tuple(2, 2, 2, 2); bn::ndarray newArray = dataArray.reshape(newShape); return newArray; } void ArrayHandler::ShowData(bp::object &data) { std::cout << "Original Array:" << bp::extract(bp::str(data)) << std::endl; bn::ndarray dataArray = bn::from_object(data); data = dataArray.reshape(bp::make_tuple(16)); std::cout << "Reshaped Array:" << bp::extract(bp::str(data)) << std::endl; } void ArrayHandler::DataType(bp::object &data) { bn::ndarray dataArray = bn::from_object(data); std::cout << "Datatype is " << bp::extract(bp::str(dataArray.get_dtype())) << std::endl; } template struct _point:bp::object{ public: T _x,_y; _point():_x(),_y(){} _point(bp::object obj){_x=((_point)obj)._x;_y=((_point)obj)._y;} _point( T x, T y):_x((T)x),_y((T)y){} T abs(){return std::sqrt(_x*_x+_y*_y);} std::string to_json(){ nlohmann::json j1; j1["x"]=_x; j1["y"]=_y; stringstream ss; ss<{ };*/ template using point=struct _point; template struct _line:bp::object{ point _s; point _e; _line():_s(),_e(){} _line(bp::object obj){_s=((_line)obj)._s;_e=((_line)obj)._e;} _line( point s, point e):_s(s),_e(e){} point dif(){return point(_e._x-_s._x,_e._y-_s._y);} T abs(){return dif().abs();} std::string to_json(){ nlohmann::json j1; stringstream ss; j1["s"]["x"]=_s._x; j1["s"]["y"]=_s._y; j1["e"]["x"]=_e._x; j1["e"]["y"]=_e._y; ss< using line=struct _line; line from_dict(bp::object&obj){ double sx=0,sy=0,ex=0,ey=0; bp::dict dict=(bp::dict)(obj); auto s=bp::extract(dict["s"]); if(s.check()){ bp::dict xy=s(); sx=bp::extract(xy["x"]); sy=bp::extract(xy["y"]); } auto e=bp::extract(dict["e"]); if(e.check()){ bp::dict xy=e(); ex=bp::extract(xy["x"]); ey=bp::extract(xy["y"]); } return line(point(sx,sy),point(ex,ey)); } line from_json(string str){ double sx=0,sy=0,ex=0,ey=0; nlohmann::json j1=nlohmann::json::parse(str); sx=j1["s"]["x"]; sy=j1["s"]["y"]; ex=j1["e"]["x"]; ey=j1["e"]["y"]; return line(point(sx,sy),point(ex,ey)); } } // namespace obp BOOST_PYTHON_MODULE(cpp_example) //导出的module 名字 { obp::bn::initialize(); obp::bp::def("greet", obp::greet); obp::bp::class_("World") .def("greet", &obp::World::greet) .def("set", &obp::World::set); //bp::def("SetDictValue", SetDictValue); obp::bp::class_("ArrayHandler", obp::bp::init<>()) .def("Generate", &obp::ArrayHandler::Generate) .def("GeneratebyTuple", &obp::ArrayHandler::GeneratebyTuple) .def("GeneratebyList", &obp::ArrayHandler::GeneratebyList) .def("ShowData", &obp::ArrayHandler::ShowData) .def("DataType", &obp::ArrayHandler::DataType) .def("Reshape", &obp::ArrayHandler::Reshape); obp::bp::class_>("point",obp::bp::init<>()) .def(obp::bp::init()) .def_readwrite("x",&obp::point::_x) .def_readwrite("y",&obp::point::_y) .def_readonly("abs",&obp::point::abs) .def("to_json",&obp::point::to_json); obp::bp::class_>("line",obp::bp::init<>()) .def(obp::bp::init,obp::point>()) .def_readwrite("s",&obp::line::_s) .def_readwrite("e",&obp::line::_e) .def_readonly("dif",&obp::line::dif) .def_readonly("abs",&obp::line::abs) .def("to_json",&obp::line::to_json); obp::bp::def("from_dict",&obp::from_dict); obp::bp::def("from_json",&obp::from_json); } #if 0 int main(){ Py_Initialize(); PyRun_SimpleString("import helloworld"); PyRun_SimpleString("helloworld.printHello()"); Py_Finalize(); } #endif