c++ - Python swig-wrapped vector of vector of doubles appears as Tuple -


i have function in c++ returns vector<vector<double> > object. have wrapped python using swig. when call it, unable subsequently modify function's output using resize() or push_back() vector methods.

when try this, error 'tuple' object has no attribute 'resize' or 'push_back'. swig transform vectors tuple objects when interact them in python? if that's case, assume output function in python immutable, problem. can pass object wrapped methods accept vector of vectors of doubles. can't mess using vector methods within python. explanation on why or ideas on work-arounds appreciated.

here swig file reference. stl template lines towards end:

/* solutioncombiner.i */ %module solutioncombiner %{     /* put header files here or function declarations below */     #include "coord.hpp"     #include "materialdata.hpp"     #include "failurecriterion.hpp"     #include "quadpointdata.hpp"     #include "modelsolution.hpp"     #include "exclusionzone.hpp"     #include "criticallocation.hpp"     #include "failuremode.hpp"     #include "executivefunctions.hpp"      #include <fstream>     #include <iostream>  %} %{     #define swig_file_with_init     std::ostream& new_ofstream(const char* filename){         return *(new std::ofstream(filename));     }      std::istream& new_ifstream(const char* filename){         return *(new std::ifstream(filename));     }      void write(std::ostream* fout, char* outstring){         *fout << outstring;     }      std::ostream *get_cout(){return &std::cout;} %}  %include "std_vector.i" %include "std_string.i" %include "std_set.i" %include "../../source/coord.hpp" %include "../../source/materialdata.hpp" %include "../../source/failurecriterion.hpp" %include "../../source/quadpointdata.hpp" %include "../../source/modelsolution.hpp" %include "../../source/exclusionzone.hpp" %include "../../source/criticallocation.hpp" %include "../../source/failuremode.hpp" %include "../../source/executivefunctions.hpp"  namespace std {    %template(intvector) vector<int>;    %template(doublevector) vector<double>;    %template(doublevvector) vector<vector<double> >;    %template(doublevvvector) vector<vector<vector<double> > >;    %template(solutionvector) vector<modelsolution>;    %template(critlocvector) vector<criticallocation>;    %template(critlocvvector) vector<vector<criticallocation> >;    %template(modevector) vector<failuremode>;    %template(intset) set<int>; } std::ofstream& new_ofstream(char* filename); std::ifstream& new_ifstream(char* filename); std::iostream *get_cout(); 

yes, vector template returns immutable python tuple. maybe modify std_vector.i implementation return lists, there reason choice. convert them lists can manipulate them in python:

>>> x.func() ((1.5, 2.5, 3.5), (1.5, 2.5, 3.5), (1.5, 2.5, 3.5), (1.5, 2.5, 3.5)) >>> [list(n) n in x.func()] [[1.5, 2.5, 3.5], [1.5, 2.5, 3.5], [1.5, 2.5, 3.5], [1.5, 2.5, 3.5]]   

note: made sample function returned vector<vector<double>> test.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -