int periodise_wrap(boost::python::list args ) { int iSize = extract<int> (args.attr("__len__")()), iIb ; char ** argv = new char*[iSize+1] ; #define NOM "periodise" argv[0] = new char[strlen(NOM)+1] ; strcpy (argv[0], NOM); for (iIb=0; iIb < iSize ; iIb ++) { const char * pcArg ; extract <const char*> earg(args[iIb]); if (earg.check()) { pcArg= earg() ; argv[iIb+1] = new char[strlen(pcArg)+1] ; strcpy (argv[iIb+1], pcArg); } } int result = periodise (iSize+1, argv ) ; for (iIb = 0 ; iIb < iSize ;iIb ++) { delete [] argv[iIb] ; } delete [](argv) ; return result ; }
inline void json::insert(const json::key_type& k, const boost::python::list& li) { unsigned n = boost::python::extract<unsigned>(li.attr("__len__")()); if(!n) return; jsonvec jv; for(unsigned i=0;i<n;++i) { try { qm_real v = boost::python::extract<qm_real>(li[i]); jv.add(v); } catch(...) { try { qm_string s = boost::python::extract<qm_string>(li[i]); jv.add(s); } catch(...) { try { json js(*boost::python::extract<json*>(li[i])); jv.add(js); } catch(...) { QM_FAIL("Could not add list"); } } } } this->insert(k,jv); }
Array<double,1>* ObjToVector (const list& list) { int len = boost::python::extract<int>(list.attr("__len__")());/**< Get the list length */ Array<double,1>* vector = new Array<double,1>(len); for (int i = 0; i < len; i++) { (*vector)(i) = (boost::python::extract<int>(list[i])); } return vector; }
bool PythonSignal::open(boost::python::list macAddress) { BOOST_VERIFY(mpSignal != 0); uint8_t mac[6] = {0,0,0,0,0,0}; if(boost::python::extract<size_t>(macAddress.attr("__len__")()) >= 6) { for (int i = 5; i >= 0; i--) { mac[i] = boost::python::extract<uint8_t>(macAddress.pop()); } } return mpSignal->open(mac); }
bool mpi_init(boost::python::list python_argv) { using boost::python::extract; using boost::python::object; // If already initialized, do nothing but note that initialization // is done. int flag = 0; int result = MPI_Initialized(&flag); assert(result == MPI_SUCCESS); if (flag) return false; // Convert Python argv into C-style argc/argv. Ewwwww! int my_argc = extract<int>(python_argv.attr("__len__")()); char** my_argv = new char*[my_argc]; for (int arg = 0; arg < my_argc; ++arg) my_argv[arg] = strdup(extract<const char*>(python_argv[arg])); // Initialize MPI int mpi_argc = my_argc; char** mpi_argv = my_argv; result = MPI_Init(&mpi_argc, &mpi_argv); assert(result == MPI_SUCCESS); // If anything changed, convert C-style argc/argv into Python argv if (mpi_argv != my_argv) { // Tear down Python argv while (int(extract<int>(python_argv.attr("__len__")())) > 0) python_argv.pop(); // Build up new Python argv for (int arg = 0; arg < mpi_argc; ++arg) python_argv.append(object(mpi_argv[arg])); } for (int arg = 0; arg < my_argc; ++arg) free(my_argv[arg]); delete [] my_argv; return true; }
Array<double,2>* ObjToArray(const list& list) { int row = boost::python::extract<int>(list.attr("__len__")());/**< Get the row length */ int col = boost::python::extract<int>(list[1].attr("__len__")());/**< Get the column length, all columns have the same size * */ Array<double,2>* tab = new Array<double,2>(row,col); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j ++) { (*tab)(i,j) = (boost::python::extract<int>(list[i][j])); } } return tab; }
ConvexHull2::ConvexHull2(boost::python::list points) { boost::python::object lenObj = points.attr( "__len__" )(); boost::python::extract<int> lenExtract( lenObj ); if ( lenExtract.check() ) { int numPoints = lenExtract; for (int pointI = 0; pointI < numPoints; pointI++) { boost::python::object pointObj = points[pointI]; boost::python::extract<Point2&> pointExtract( pointObj ); if ( pointExtract.check() ) { addPoint( pointExtract ); } } } }
Polygon2::Polygon2(boost::python::list verts) { boost::python::object lenObj = verts.attr( "__len__" )(); boost::python::extract<int> lenExtract( lenObj ); if ( lenExtract.check() ) { int numVerts = lenExtract; vertices.reserve( numVerts ); for (int i = 0; i < numVerts; i++) { boost::python::object pointObj = verts[i]; boost::python::extract<Point2&> pointExtract( pointObj ); if ( pointExtract.check() ) { vertices.push_back( pointExtract ); } } } }