Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/python.hpp>
- #include <iostream>
- #include <set>
- namespace bp = boost::python;
- std::string my_function(std::set<std::string> const& set_a
- , std::set<std::string> const& set_b
- , int32_t number_a
- , int32_t number_b)
- {
- std::stringstream os;
- os << "Set A: ";
- std::for_each(begin(set_a), end(set_a), [&](auto& s) { os << s << " "; });
- os << "\nSet B: ";
- std::for_each(begin(set_b), end(set_b), [&](auto& s) { os << s << " "; });
- os << "\nNumber A: " << number_a << "\nNumber B: " << number_b << "\n";
- return os.str();
- }
- std::set<std::string> extract_string_set(bp::object const& python_set)
- {
- if (!PySet_Check(python_set.ptr())) {
- throw std::runtime_error("Not a set.");
- }
- std::set<std::string> result;
- // Need a list to iterate over...
- boost::python::list set_as_list(python_set);
- for (int32_t i(0); i < len(set_as_list); ++i) {
- boost::python::extract<std::string> extractor(set_as_list[i]);
- if (!extractor.check()) {
- throw std::runtime_error("Not a string.");
- }
- result.emplace(extractor());
- }
- return result;
- }
- std::string my_function_wrapper(bp::object const& python_set_a
- , bp::object const& python_set_b
- , int32_t number_a
- , int32_t number_b)
- {
- auto set_a(extract_string_set(python_set_a));
- auto set_b(extract_string_set(python_set_b));
- return my_function(set_a, set_b, number_a, number_b);
- }
- BOOST_PYTHON_MODULE(test)
- {
- bp::def("my_function", my_function_wrapper);
- }
- int main()
- {
- if (!Py_IsInitialized()) {
- Py_Initialize();
- }
- inittest();
- bp::object main = bp::import("__main__");
- bp::object globals = main.attr("__dict__");
- bp::dict locals;
- bp::exec("import test\n"
- "print test.my_function({'3H', '3S'}, {'8S', '4S', 'QH', '8C', '4H'}, 2, 10000)\n"
- , globals, locals);
- Py_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement