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

SciPy: n-dimensional interpolation of sparse data
Write a program to display all the anagrams for “abcdef” in Python
Python prime numbers generators in terminal
Python Square function using newton's algorithm
Titles for elements in tkinter
Adding items to a list if it's not a function
Selenium (python): can't switch to iframe (name is dynamically generated)
How to redirect printed value of python script to the linux terminal as a command
Selenuim webdriver fails without notice for ssl error
Unable to locate button element using selenium webdriver
Simple Image Deconvolution issue
In Python, what is the easiest way to add a list consisting of keyword pairs to a dictionary?
I can't find what's wrong with this circle bounce calculation in python
Determine Postgresql datatype for a variable in Ruby, Python, or C++?
Z3 v4.3.2 example.py execution error on Mac OS X 10.10
How do I specify the root module directory in python?

Categories

HOME
caching
cluster-computing
erlang
npm
oauth
sed
cocos2d-x-3.0
mapbox-gl-js
docker-swarm
angular2-directives
concourse
phpstorm
search-engine
safari
webdav
hyperledger-fabric
histogrammar
goutte
vuex
hosts
iso
wysiwyg
camera-calibration
sendkeys
bootstrap-popover
bar-chart
alfresco-share
off-canvas-menu
derived
jsonserializer
explode
rhmap
vcard
reply
data-conversion
pitest
devforce
protobuf-net
oracle-xml-db
division
ocs
vmd
inkscape
brute-force
tic-tac-toe
polyfills
flex4.5
rule
resuming-training
ajp
tuleap
number-theory
web-deployment-project
sgmlreader
seamless-immutable
slidesjs
ruby-on-rails-2
infinite-scroll
adler32
iron.io
payu
packagemaker
software-product-lines
wcf-ria-services
slam-algorithm
scalar
fps
date-format
django-south
application-loader
vips
selendroid
qbfc
magento-1.12
django-1.6
sid
adaptive-compression
smartystreets
colt
slick-2.0
wic
carddav
apache2.2
surveyor-gem
appfog
wpdb
sametime
bulbs
javascriptserializer
sunspot-rails
fotoware
ninject-extensions
parameterization
makefile-project
wchar
objectbrowser
handwriting
easygui
executable-format
paintcomponent
gtk2hs
search-path
sendfile
audiostreamer
libavformat
surefire
communicationexception
idictionary
graph-layout

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