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

Using the Python Request Library to get Google Oauth2 tokens
(Beginner)Python functions Codeacademy
Python script hangs
How to shift a list nth time in Left or Right and fill it up with 0 in python
NumPy, why equality check does not work for an array of objects?
String formatting of timedeltas in Pandas
Celery - How to send task from remote machine?
YouTube Api v3 handling Exceptions in Python
Copy data from one oracle database to another with Python
converting python program into executable
How to use multidatabase across multiproject in django?
SSLHandshakeError when connecting to Google Analytics using Google APIs Client Library for Python
Pandas Pivot Table Display in ReportLab
python multiprocessing behaviour
How do You Hack the Enthought Canopy Editor Background Color?
Why does PySide still require QString for textChanged SIGNAL?

Categories

HOME
jsf
crystal-reports
google-cloud-bigtable
formal-verification
csvhelper
safari
specflow
ext.net
browserify
pjsip
jacoco
typeahead
event-log
scaling
android-toolbar
off-canvas-menu
zoho
thumbnails
os161
binutils
explode
source-maps
go-cd
profile
non-deterministic
guile
metis
classpath
i3
clockwork
read-write
getline
boilerplate
vsts-package-management
parent
distributed-transactions
taglib
crop
upsert
php-ews
lumen-5.3
vcf
mouseclick-event
spell-checking
rhel6
greenhills
etherpad
emoticons
sqldatareader
coremidi
c#-interactive
uiprogressview
image-editing
contour
gemini
mfc-feature-pack
qt-linguist
drf-nested-routers
zuora
futuretask
risk-analysis
ubercart
mdm-zinc
jython-2.7
consensus
multipleselection
boost-test
mbox
flash-cc
db4o
javascriptmvc
html-form-post
cgimageref
graph-coloring
couchdb-lucene
static-variables
caliper
ninject-extensions
visual-web-gui
authenticode
testunit
email-spec
trampolines
asp.net-mvc-controller

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