Skip to content

Instantly share code, notes, and snippets.

@gruzovator
Last active August 29, 2015 14:14
Show Gist options
  • Save gruzovator/ed7e326a44ab9a521059 to your computer and use it in GitHub Desktop.
Save gruzovator/ed7e326a44ab9a521059 to your computer and use it in GitHub Desktop.
Boost.Python example
//------------------------------------------------------------------------------
// C++ module xxx.h, xxx.cpp
//------------------------------------------------------------------------------
#include <string>
#include <boost/regex.hpp>
#include <boost/locale.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
class PhoneExtractor
{
public:
//.............
std::list<std::string> extract(const std::string&, ExtractMode mode);
//.............
};
//.............
std::list<std::string> PhoneExtractor::extract(const std::string &str, ExtractMode mode)
{
//.............
}
//------------------------------------------------------------------------------
// end of C++ module
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Export to python
//------------------------------------------------------------------------------
#include <boost/python.hpp>
boost::python::list normalize(const std::string &phones)
{
// TODO: error, pass vector to python::list
PhoneExtractor extractor;
return boost::python::list(extractor.extract(phones, PhoneExtractor::StrictMode));
}
BOOST_PYTHON_MODULE(phones_utils)
{
def("normalize", normalize);
}
/*
BUILD:
g++ -g -shared -fPIC -I/usr/include/python2.7 phones_utils.cpp -lboost_regex -lboost_python -o phones_utils.so
USAGE:
import phones_utils
phones = phones_utils.normalize('84951234567')
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment