python


Pybind11 Type Error


I've recently been trying to write a function in C++ that converts a vector of doubles into a vector of strings. I want to run this from a python interpreter so I'm using Pybind11 to interface C++ and Python. This is what I have so far,
#include <pybind11/pybind11.h>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
std::string castToString(double v) {
std::string str = boost::lexical_cast<std::string>(v);
return str;
}
std::vector<std::vector<std::string> > num2StringVector(std::vector<std::vector<double> >& inputVector) {
//using namespace boost::multiprecision;
std::vector<std::vector<std::string> > outputVector;
std::transform(inputVector.begin(), inputVector.end(), std::back_inserter(outputVector), [](const std::vector<double> &iv) {
std::vector<std::string> dv;
std::transform(iv.begin(), iv.end(), std::back_inserter(dv), &castToString);
return dv;
});
return outputVector;
}
namespace py = pybind11;
PYBIND11_PLUGIN(num2String) {
py::module m("num2String", "pybind11 example plugin");
m.def("num2StringVector", &num2StringVector, "this converts a vector of doubles to a vector of strings.");
m.def("castToString", &castToString, "This function casts a double to a string using Boost.");
return m.ptr();
}
Now this compiles into a shared library, using the follow command from command line:
c++ -O3 -shared -std=gnu++11 -I ../include `python-config --cflags --ldflags --libs` num2StringVectorPyBind.cpp -o num2String.so -fPIC -lquadmath
where the ../include is where the pybind11 includes are. After compilation start up python and use,
import num2String
values = [[10, 20], [30, 40]]
num2String.num2StringVector(values)
And I get the following error, "Incompatible function arguments. The following argument types are supported". Where it then gives me a list of possible types, this is strange because I'm just trying to use a vector as my function argument which according to the documentation for pybind11 this is a supported data type:
http://pybind11.readthedocs.io/en/latest/basics.html#supported-data-types
Is it that I have a vector of vectors (a 2d vector) and that is unsupported?
You can make this work with just one extra line of code:
#include <pybind11/stl.h>
As per the documentation:
The following basic data types are supported out of the box (some may require an additional extension header to be included).
Before including pybind11/stl.h:
>>> import num2String
>>> num2String.num2StringVector([[1, 2], [3, 4, 5]])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Incompatible function arguments. The following argument types are supported:
1. (std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >) -> std::vector<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >
After including pybind11/stl.h and recompiling the shared library:
>>> import num2String
>>> num2String.num2StringVector([[1, 2], [3, 4, 5]])
[['1', '2'], ['3', '4', '5']]

Related Links

Panda3D - Setting date
decoding json in python
Python debugging, stop at particular output
Why an avi file can not be opened in OpenCV-Python
Can't compile msgpack Python extension under windows
Python sockets for a n-players game
HTMLParser or urllib2 unicode issue
How to stop PyCharm inserting spaces for fine alignment when reformating code?
Install NLTK with IronPyton for VS2012
pip install won't install on my machine
Integrating using scipy.integrate.simps
Problems with django and virtualenv
Returning when any Future in a list finishes
Python Django - matching query does not exist when using pk
R translation to Python
Are there downsides to using Python locals() for string formatting? [duplicate]

Categories

HOME
npm
wmic
ncurses
decorator
wms
angular2-directives
phonegap-cli
java-home
devstack
i2c
structuremap
google-shopping
cortex-a
maxima
glpk
pdo
eclipselink
jni
apply
fop
header-files
qsub
samsung-mobile
hash-collision
aurelia-binding
mangodb
pep8-assembly
protobuf-net
strophe
consumer
exponential
oracle-xml-db
spring-insight
nsurlconnection
recurrence-relation
honeysql
geomesa
bigcartel
codesys
coreclr
sonarqube-scan
s3cmd
bayesian-networks
sendgrid-templates
pytest-django
prototypejs
broadcast
rollback
pluck
laravel-query-builder
barcode-printing
sql-import-wizard
pdflib
cjson
gradle-script-kotlin
cfeclipse
hspec
slam-algorithm
brython
kendo-combobox
elastix
inet
approval-tests
android-viewholder
function-fitting
file-diffs
app.xaml
windowlistener
urn
risk-analysis
team-build
ffprobe
divide-by-zero
xaml-designer
git-checkout
ticket-system
tilestache
utf-32
libsndfile
iostream
outline
dache
bsp
phpdocx
openkinect
ftp4j
visual-studio-2003
shiva3d
xslkey
vt100
mvs
visual-web-gui
emacs23
querystringparameter
nemerle
nvelocity
open-graph-beta
requestfactory
surefire
iphone-sdk-3.2
scatterview
web-analytics-tools
divx
dotproject

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App