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

Python: Flatten a list of Objects
flask: command not found - flask 0.12
PYTHON (Controls & conditional flow)
Efficiently sorting and grouping really big arrays
Remove unwanted leading slash from nginx
Callback function on pin state change
Why does my Python code keeps running even after 10 seconds?
How to resample a time series per decades in python
What's the role of string = “” in a program Python
Autoresizing Canvas in a Kivy App
Pip install for Python 3.0 (win XP)
Subselect by columns in MultiIndex Pandas DataFame
Machine learning for a Ruby on rails application [closed]
Memory error with np array when making document term matrix in python 2.7
How to Create an XML file directly from DS18B20 sensor in raspberry Pi?
is there any pyspark function for add next month like DATE_ADD(date, month(int type))

Categories

HOME
json
sidekiq
ibm-watson-cognitive
jar
wsdl
vs-team-services
rocketmq
terrain
ll
jframe
deeplearning4j
google-classroom
xul
dlib
local
cosmicmind
typeahead
sql-tuning
android-toolbar
google-openid
zoho
flexboxgrid
google-maps-android-api-2
avplayeritem
tcpclient
multiplayer
memsql
samsung-mobile
hash-collision
codelite
xilinx-ise
datadog
dhtmlx-scheduler
devforce
outsystems
fabric-digits
transient
code-behind
floating-accuracy
mediawiki-extensions
android-maps-v2
g1gc
defold
emoticons
sqlexception
hspec
picasa
slam-algorithm
cron-task
angularjs-ng-pattern
pax-web
gmsmapview
gemini
date-format
dac
mpeg-4
android-recyclerview
file-diffs
coldfusion-7
enyo
jericho-html-parser
python-winshell
subresource-integrity
pyrocms
gray-code
rspec3
httpie
google-earth-plugin
ticket-system
python-ggplot
java-melody
gmaps4rails
sortable
neoload
codeigniter-a3m
kraken.js
config-files
late-static-binding
unison
bigint
image-zoom
ruboto
adomd.net
pascals-triangle
phpsh
crocodoc
userid
datacontract
windows-live-id
dcpu-16
nvelocity
post-redirect-get
pantheios
quick-search
usability-testing
soft-keyboard
handheld
msf
rd





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