Belle II Software development
Belle2::PyObjConvUtils Namespace Reference

Python object converter utilities namespace. More...

Classes

class  convertToPythonObjectVisitor
 Helper function object to unpack a value from a variant to a python object. More...
 
struct  SizeT
 Helper construct for TMP that provides an index at compile time to recurse through type lists. More...
 
struct  Type
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< bool >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< boost::variant< Types... > >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< double >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< float >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< int >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< std::map< A, B > >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< std::optional< T > >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< std::set< T > >
 Convert a templeate arugment into a string for corresponding Python type. More...
 
struct  Type< std::shared_ptr< Path > >
 Some modules take a path as argument so let's add this as well. More...
 
struct  Type< std::string >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< std::tuple< Types... > >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< std::vector< T > >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< unsigned int >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  Type< unsigned long int >
 Converts a template argument into a string for corresponding Python type. More...
 
struct  VariadicType
 Recursively convert multiple types to type names (used for tuples). More...
 
struct  VariadicType< T >
 Recursively convert multiple types to type names (used for tuples). More...
 

Functions

bool checkPythonObject (const boost::python::object &pyObject, bool)
 check if the python object can be converted to the given type.
 
bool checkPythonObject (const boost::python::object &pyObject, float)
 check if the python object can be converted to the given type.
 
bool checkPythonObject (const boost::python::object &pyObject, double)
 check if the python object can be converted to the given type.
 
bool checkPythonObject (const boost::python::object &pyObject, int)
 check if the python object can be converted to the given an integral number
 
bool checkPythonObject (const boost::python::object &pyObject, unsigned int)
 check if the python object can be converted to the given an integral number
 
bool checkPythonObject (const boost::python::object &pyObject, unsigned long int)
 check if the python object can be converted to the given an integral number
 
bool checkPythonObject (const boost::python::object &pyObject, const std::string &)
 check if the python object can be converted to the given a string
 
template<typename Key , typename Value >
bool checkPythonObject (const boost::python::object &pyObject, const std::map< Key, Value > &)
 Check if the python object is a mapping of the correct key and value types.
 
template<typename Value >
bool checkPythonObject (const boost::python::object &pyObject, const std::vector< Value > &)
 check if the python object can be converted to the given std::vector type
 
template<typename Value >
bool checkPythonObject (const boost::python::object &pyObject, const std::set< Value > &)
 check if the python object can be converted to the given std::set type
 
template<typename... Types>
bool checkPythonObject (const boost::python::object &pyObject, const std::tuple< Types... > &)
 check if the python object can be converted to the given std::tuple type
 
template<typename... Types>
bool checkPythonObject (const boost::python::object &pyObject, const boost::variant< Types... > &)
 check if the python object can be converted to the given boost::variant type
 
template<typename Type >
bool checkPythonObject (const boost::python::object &pyObject, const std::optional< Type > &)
 check if the python object can be converted to the given std::optional<T> type
 
template<typename Scalar >
Scalar convertPythonObject (const boost::python::object &pyObject, Scalar)
 Convert from Python to given type.
 
template<typename Key , typename Value >
std::map< Key, Value > convertPythonObject (const boost::python::object &pyObject, const std::map< Key, Value > &)
 Convert from Python to given type.
 
template<typename Value >
std::vector< Value > convertPythonObject (const boost::python::object &pyObject, const std::vector< Value > &)
 Convert from Python to given type.
 
template<typename Value >
std::set< Value > convertPythonObject (const boost::python::object &pyObject, const std::set< Value > &)
 Convert from Python to given type.
 
template<typename... Types>
std::tuple< Types... > convertPythonObject (const boost::python::object &pyObject, const std::tuple< Types... > &)
 Convert from Python to given type.
 
template<typename... Types>
boost::variant< Types... > convertPythonObject (const boost::python::object &pyObject, const boost::variant< Types... > &)
 Convert from Python to given type.
 
template<typename Type >
std::optional< TypeconvertPythonObject (const boost::python::object &pyObject, const std::optional< Type > &)
 Convert from Python to the given std::optional type.
 
template<typename Scalar >
boost::python::object convertToPythonObject (const Scalar &value)
 ------------— From C++ TO Python Converter ---------------------—
 
template<typename Value >
boost::python::list convertToPythonObject (const std::vector< Value > &vector)
 Writes content of a std::vector to a python list.
 
template<typename Value >
boost::python::object convertToPythonObject (const std::set< Value > &set)
 Convert std::set to a python object.
 
template<typename Key , typename Value >
boost::python::dict convertToPythonObject (const std::map< Key, Value > &map)
 Writes content of a std::map to a python dict.
 
template<typename... Types>
boost::python::tuple convertToPythonObject (const std::tuple< Types... > &tuple)
 Writes content of a std::tuple to a python tuple.
 
template<typename... Types>
boost::python::object convertToPythonObject (const boost::variant< Types... > &variant)
 Writes content of a boost::variant to a python object.
 
template<typename Type >
boost::python::object convertToPythonObject (const std::optional< Type > &optional)
 Convert from the given std::optional type to Python.
 
template<class Functor >
bool iteratePythonObject (const boost::python::object &pyObject, Functor function)
 Helper function to loop over a python object that implements the iterator concept and call a functor with each element as argument.
 
template<typename... Types, std::size_t ... Is>
bool CheckTuple (const std::tuple< Types... > &tuple, const boost::python::tuple &pyTuple, std::index_sequence< Is... >)
 Check if all tuple elements match.
 
template<typename VariantType >
bool CheckVariant (const VariantType &, const boost::python::object &, SizeT< 0 >)
 Recursion sentinal for the case that all former type checks failed for the variant.
 
template<typename... Types, size_t N>
bool CheckVariant (const boost::variant< Types... > &variant, const boost::python::object &pyObject, SizeT< N >)
 Recursion through the variant types checking the python object for the type at position N - 1.
 
template<typename... Types>
bool checkPythonObject (const boost::python::object &pyObject, const boost::variant< Types... > variant)
 Check if the python object is a tuple of objects of the correct types.
 
template<typename TupleType >
void GetTuple (const TupleType &tuple, boost::python::list &pyList)
 TMP (Template Meta Programming ) The given python list is filled, and later converted into a python tuple (in convertToPythonObject).
 
template<typename TupleType >
void GetTuple (const TupleType &, boost::python::list &, SizeT< 0 >)
 Break recursion when all members are already appended to the list.
 
template<typename TupleType , size_t N>
void GetTuple (const TupleType &tuple, boost::python::list &pyList, SizeT< N >)
 Append elements of the tuple to pyList by recursively calling this function with the element index as argument type.
 
template<typename TupleType >
void SetTuple (TupleType &tuple, const boost::python::tuple &pyTuple)
 TMP (Template Meta Programming ) The given python tuple is written into the given c++ tuple.
 
template<typename TupleType >
void SetTuple (TupleType &, const boost::python::tuple &, SizeT< 0 >)
 Break recursion once all elements have been set.
 
template<typename TupleType , size_t N>
void SetTuple (TupleType &tuple, const boost::python::tuple &pyTuple, SizeT< N >)
 Set the elements of the tuple recursively by calling with the function with the next tuple index and converting one element.
 
template<typename... Types>
void SetVariant (boost::variant< Types... > &, const boost::python::object &pyObject, SizeT< 0 >)
 TMP (Template Meta Programming ) The given python object is written into the given c++ boost variant.
 
template<typename... Types, size_t N>
void SetVariant (boost::variant< Types... > &variant, const boost::python::object &pyObject, SizeT< N >)
 Recursively go through all possible types of the variant and check which one is a possible conversion.
 

Detailed Description

Python object converter utilities namespace.

Provides templates for converting python objects to c++ objects and the other way round.

This is done by using the type of the value provided by the module as a template parameter. Hence the compiler can infer which converter should be used. Note: We are only interested in the type of the value, the actual value isn't used at all!

Function Documentation

◆ checkPythonObject() [1/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
bool   
)
inline

check if the python object can be converted to the given type.

------------— Check python objects for exact type match ---------------------—

Check if the python object is a boolean

Definition at line 192 of file PyObjConvUtils.h.

193 {
194 return PyBool_Check(pyObject.ptr());
195 }

◆ checkPythonObject() [2/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
const boost::variant< Types... >  variant 
)

Check if the python object is a tuple of objects of the correct types.

Definition at line 327 of file PyObjConvUtils.h.

328 {
329 return CheckVariant(variant, pyObject, SizeT<sizeof...(Types)>());
330 }
bool CheckVariant(const VariantType &, const boost::python::object &, SizeT< 0 >)
Recursion sentinal for the case that all former type checks failed for the variant.
Helper construct for TMP that provides an index at compile time to recurse through type lists.

◆ checkPythonObject() [3/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
const std::map< Key, Value > &   
)

Check if the python object is a mapping of the correct key and value types.

Definition at line 234 of file PyObjConvUtils.h.

235 {
236 if (not PyDict_Check(pyObject.ptr())) return false;
237 const boost::python::dict& pyDict = static_cast<const boost::python::dict&>(pyObject);
238 boost::python::list keys = pyDict.keys();
239 boost::python::list values = pyDict.values();
240 for (int i = 0; i < boost::python::len(keys); ++i) {
241 if (not checkPythonObject(keys[i], Key())) return false;
242 if (not checkPythonObject(values[i], Value())) return false;
243 }
244 return true;
245 }
bool checkPythonObject(const boost::python::object &pyObject, bool)
check if the python object can be converted to the given type.

◆ checkPythonObject() [4/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
const std::optional< Type > &   
)

check if the python object can be converted to the given std::optional<T> type

Check if a python object can be converted to std::optional.

Returns
true if pyObject is None or convertible to the type of the std::optional

Definition at line 336 of file PyObjConvUtils.h.

337 {
338 return pyObject.is_none() or checkPythonObject(pyObject, Type());
339 }
Converts a template argument into a string for corresponding Python type.

◆ checkPythonObject() [5/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
const std::set< Value > &   
)

check if the python object can be converted to the given std::set type

Definition at line 278 of file PyObjConvUtils.h.

279 {
280 if (not PyAnySet_Check(pyObject.ptr())) return false;
281 return iteratePythonObject(pyObject, [](const boost::python::object & element) {
282 return checkPythonObject(element, Value());
283 });
284 }
bool iteratePythonObject(const boost::python::object &pyObject, Functor function)
Helper function to loop over a python object that implements the iterator concept and call a functor ...

◆ checkPythonObject() [6/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
const std::string &   
)
inline

check if the python object can be converted to the given a string

Check if the python object is a string.

Definition at line 227 of file PyObjConvUtils.h.

228 {
229 return PyUnicode_Check(pyObject.ptr());
230 }

◆ checkPythonObject() [7/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
const std::tuple< Types... > &  tuple 
)

check if the python object can be converted to the given std::tuple type

Check if the python object is a tuple of objects of the correct types.

Definition at line 296 of file PyObjConvUtils.h.

297 {
298 if (not PyTuple_Check(pyObject.ptr())) return false;
299 const boost::python::tuple& pyTuple = static_cast<const boost::python::tuple&>(pyObject);
300 return CheckTuple(tuple, pyTuple, std::index_sequence_for<Types ...>());
301 }
bool CheckTuple(const std::tuple< Types... > &tuple, const boost::python::tuple &pyTuple, std::index_sequence< Is... >)
Check if all tuple elements match.

◆ checkPythonObject() [8/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
const std::vector< Value > &   
)

check if the python object can be converted to the given std::vector type

Check if the python object is a list of objects of the correct value type.

Definition at line 269 of file PyObjConvUtils.h.

270 {
271 if (not PyList_Check(pyObject.ptr())) return false;
272 return iteratePythonObject(pyObject, [](const boost::python::object & element) {
273 return checkPythonObject(element, Value());
274 });
275 }

◆ checkPythonObject() [9/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
double   
)
inline

check if the python object can be converted to the given type.

Check if the python object is a float number.

Definition at line 221 of file PyObjConvUtils.h.

222 {
223 return PyFloat_CheckExact(pyObject.ptr());
224 }

◆ checkPythonObject() [10/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
float   
)
inline

check if the python object can be converted to the given type.

Check if the python object is a float number.

Definition at line 215 of file PyObjConvUtils.h.

216 {
217 return PyFloat_CheckExact(pyObject.ptr());
218 }

◆ checkPythonObject() [11/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
int   
)
inline

check if the python object can be converted to the given an integral number

Check if the python object is an integral number.

Definition at line 198 of file PyObjConvUtils.h.

199 {
200 return PyLong_CheckExact(pyObject.ptr());
201 }

◆ checkPythonObject() [12/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
unsigned int   
)
inline

check if the python object can be converted to the given an integral number

Check if the python object is an integral number.

Definition at line 203 of file PyObjConvUtils.h.

204 {
205 return PyLong_CheckExact(pyObject.ptr());
206 }

◆ checkPythonObject() [13/13]

bool checkPythonObject ( const boost::python::object &  pyObject,
unsigned long int   
)
inline

check if the python object can be converted to the given an integral number

Check if the python object is an integral number.

Definition at line 209 of file PyObjConvUtils.h.

210 {
211 return PyLong_CheckExact(pyObject.ptr());
212 }

◆ CheckTuple()

bool CheckTuple ( const std::tuple< Types... > &  tuple,
const boost::python::tuple &  pyTuple,
std::index_sequence< Is... >   
)

Check if all tuple elements match.

Definition at line 288 of file PyObjConvUtils.h.

290 {
291 return (... && checkPythonObject(pyTuple[Is], std::get<Is>(tuple)));
292 }

◆ CheckVariant() [1/2]

bool CheckVariant ( const boost::variant< Types... > &  variant,
const boost::python::object &  pyObject,
SizeT< N >   
)

Recursion through the variant types checking the python object for the type at position N - 1.

Definition at line 314 of file PyObjConvUtils.h.

317 {
318 using Scalar = typename std::tuple_element < N - 1, std::tuple<Types...> >::type;
319 if (checkPythonObject(pyObject, Scalar())) {
320 return true;
321 }
322 return CheckVariant(variant, pyObject, SizeT < N - 1 > ());
323 }

◆ CheckVariant() [2/2]

bool CheckVariant ( const VariantType &  ,
const boost::python::object &  ,
SizeT< 0 >   
)

Recursion sentinal for the case that all former type checks failed for the variant.

Definition at line 305 of file PyObjConvUtils.h.

308 {
309 return false;
310 }

◆ convertPythonObject() [1/7]

boost::variant< Types... > convertPythonObject ( const boost::python::object &  pyObject,
const boost::variant< Types... > &   
)

Convert from Python to given type.

Reads boost::variant from a python object.

Parameters
pyObjectPython object which stores the value.
Returns
boost::variant<Types...>, which holds the value from the python object

Definition at line 667 of file PyObjConvUtils.h.

668 {
669 boost::variant<Types...> tmpVariant;
670 SetVariant(tmpVariant, pyObject, SizeT<sizeof...(Types)>());
671 return tmpVariant;
672 }
void SetVariant(boost::variant< Types... > &, const boost::python::object &pyObject, SizeT< 0 >)
TMP (Template Meta Programming ) The given python object is written into the given c++ boost variant.

◆ convertPythonObject() [2/7]

std::map< Key, Value > convertPythonObject ( const boost::python::object &  pyObject,
const std::map< Key, Value > &   
)

Convert from Python to given type.

Reads std::map from a python object.

Parameters
pyObjectPython object which stores the map.
Returns
Map, which holds the map from the python object

Definition at line 562 of file PyObjConvUtils.h.

563 {
564 std::map<Key, Value> tmpMap;
565 const boost::python::dict& pyDict = static_cast<const boost::python::dict&>(pyObject);
566 boost::python::list keys = pyDict.keys();
567
568 for (int i = 0; i < boost::python::len(keys); ++i) {
569 Key key = convertPythonObject(keys[boost::python::object(i)], Key());
570 Value value = convertPythonObject(pyDict[key], Value());
571 tmpMap.insert(std::pair<Key, Value>(key, value));
572 }
573 return tmpMap;
574 }
Scalar convertPythonObject(const boost::python::object &pyObject, Scalar)
Convert from Python to given type.

◆ convertPythonObject() [3/7]

std::optional< Type > convertPythonObject ( const boost::python::object &  pyObject,
const std::optional< Type > &   
)

Convert from Python to the given std::optional type.

Convert a python object to std::optional.

Returns
std::optional which is not set if pyObject is None. Otherwise it will be converted to the type of the boost optional

Definition at line 681 of file PyObjConvUtils.h.

682 {
683 std::optional<Type> tmpOptional = std::nullopt;
684 if (!pyObject.is_none()) {
685 tmpOptional = convertPythonObject(pyObject, Type());
686 }
687 return tmpOptional;
688 }

◆ convertPythonObject() [4/7]

std::set< Value > convertPythonObject ( const boost::python::object &  pyObject,
const std::set< Value > &   
)

Convert from Python to given type.

Convert a python set to a std::set.

Definition at line 540 of file PyObjConvUtils.h.

541 {
542 std::set<Value> result;
543 if (PyAnySet_Check(pyObject.ptr())) {
544 iteratePythonObject(pyObject, [&result](const boost::python::object & element) {
545 result.emplace(convertPythonObject(element, Value()));
546 return true;
547 });
548 } else {
549 result.emplace(convertPythonObject(pyObject, Value()));
550 }
551 return result;
552 }

◆ convertPythonObject() [5/7]

std::tuple< Types... > convertPythonObject ( const boost::python::object &  pyObject,
const std::tuple< Types... > &   
)

Convert from Python to given type.

Reads std::tuple from a python object.

Parameters
pyObjectPython object which stores the map.
Returns
std::tuple<Types...>, which holds the map from the python object

Definition at line 618 of file PyObjConvUtils.h.

619 {
620 std::tuple<Types...> tmpTuple;
621 const boost::python::tuple& pyTuple = static_cast<const boost::python::tuple&>(pyObject);
622 SetTuple(tmpTuple, pyTuple);
623 return tmpTuple;
624 }
void SetTuple(TupleType &tuple, const boost::python::tuple &pyTuple)
TMP (Template Meta Programming ) The given python tuple is written into the given c++ tuple.

◆ convertPythonObject() [6/7]

std::vector< Value > convertPythonObject ( const boost::python::object &  pyObject,
const std::vector< Value > &   
)

Convert from Python to given type.

Reads std::vector from a python object.

If the python object isn't a list, a std::vector with the given object as single entry is returned.

Parameters
pyObjectPython object which stores the vector.
Returns
Vector, which holds the vector from the python object

Definition at line 523 of file PyObjConvUtils.h.

524 {
525
526 std::vector<Value> tmpVector;
527 if (PyList_Check(pyObject.ptr()) or PyGen_Check(pyObject.ptr())) {
528 iteratePythonObject(pyObject, [&tmpVector](const boost::python::object & element) {
529 tmpVector.emplace_back(convertPythonObject(element, Value()));
530 return true;
531 });
532 } else {
533 tmpVector.emplace_back(convertPythonObject(pyObject, Value()));
534 }
535 return tmpVector;
536 }

◆ convertPythonObject() [7/7]

Scalar convertPythonObject ( const boost::python::object &  pyObject,
const  Scalar 
)

Convert from Python to given type.

------------— From Python TO C++ Converter ---------------------—

Reads a scalar type (int, double, string, bool) from a python object.

Parameters
pyObjectPython object which stores the scalar type.
ScalarA scalar.
Returns
Scalar type, which holds the value from the python object

Definition at line 500 of file PyObjConvUtils.h.

501 {
502
503 Scalar tmpValue;
504 boost::python::extract<Scalar> valueProxy(pyObject);
505 if (valueProxy.check()) {
506 tmpValue = static_cast<Scalar>(valueProxy);
507 } else {
508 throw std::runtime_error(std::string("Could not convert value: Expected type '") + Type<Scalar>::name() + "' instead of '" +
509 pyObject.ptr()->ob_type->tp_name + "'.");
510 }
511 return tmpValue;
512
513 }

◆ convertToPythonObject() [1/7]

boost::python::object convertToPythonObject ( const boost::variant< Types... > &  variant)

Writes content of a boost::variant to a python object.

Parameters
variantThe variant whose content should be stored in a python object.
Returns
The python object where the the content of the map is stored.

Definition at line 466 of file PyObjConvUtils.h.

467 {
468 return boost::apply_visitor(convertToPythonObjectVisitor(), variant);
469 }
Helper function object to unpack a value from a variant to a python object.

◆ convertToPythonObject() [2/7]

boost::python::object convertToPythonObject ( const Scalar &  value)

------------— From C++ TO Python Converter ---------------------—

Writes a scalar type (int, double, string, bool) to a python object.

Parameters
valueThe scalar who should be converted to a python object.
Returns
python object where the scalar is stored.

Definition at line 352 of file PyObjConvUtils.h.

353 {
354 return boost::python::object(value);
355 }

◆ convertToPythonObject() [3/7]

boost::python::dict convertToPythonObject ( const std::map< Key, Value > &  map)

Writes content of a std::map to a python dict.

Parameters
mapThe map whose items should be stored to a python dict.
Returns
dict The python dict where the the content of the map is stored.

Definition at line 394 of file PyObjConvUtils.h.

395 {
396 boost::python::dict outputDict;
397 for (auto pair : map) {
398 outputDict[convertToPythonObject(pair.first)] = convertToPythonObject(pair.second);
399 }
400 return outputDict;
401 }
boost::python::object convertToPythonObject(const Scalar &value)
------------— From C++ TO Python Converter ---------------------—

◆ convertToPythonObject() [4/7]

boost::python::object convertToPythonObject ( const std::optional< Type > &  optional)

Convert from the given std::optional type to Python.

Convert a std::optional to python.

Parameters
optionalthe optional object to convert
Returns
If the optional is not set return None, otherwise just return the converted type it contains.

Definition at line 479 of file PyObjConvUtils.h.

480 {
481 if (optional) {
482 return convertToPythonObject(*optional);
483 } else {
484 return boost::python::object();
485 }
486 }

◆ convertToPythonObject() [5/7]

boost::python::object convertToPythonObject ( const std::set< Value > &  set)

Convert std::set to a python object.

Write the content of a std::set to a python set.

Parameters
setthe set that should be converted
Returns
python object for the set

Definition at line 378 of file PyObjConvUtils.h.

379 {
380 boost::python::object result(boost::python::handle<>(PySet_New(nullptr)));
381 for (const auto& value : set) {
382 PySet_Add(result.ptr(), convertToPythonObject(value).ptr());
383 }
384 return result;
385 }

◆ convertToPythonObject() [6/7]

boost::python::tuple convertToPythonObject ( const std::tuple< Types... > &  tuple)

Writes content of a std::tuple to a python tuple.

Parameters
tupleThe tuple whose items should be stored to a python tuple.
Returns
The python tuple where the content of the map is stored.

Definition at line 438 of file PyObjConvUtils.h.

439 {
440 boost::python::list outputList;
441 GetTuple(tuple, outputList);
442 boost::python::tuple outputTuple(outputList);
443 return outputTuple;
444 }
void GetTuple(const TupleType &tuple, boost::python::list &pyList)
TMP (Template Meta Programming ) The given python list is filled, and later converted into a python t...

◆ convertToPythonObject() [7/7]

boost::python::list convertToPythonObject ( const std::vector< Value > &  vector)

Writes content of a std::vector to a python list.

Parameters
vectorThe vector whose items should be stored to a python list.
Returns
object python list where the vector is stored.

Definition at line 364 of file PyObjConvUtils.h.

365 {
366 boost::python::list outputList;
367 for (const auto& value : vector) {
368 outputList.append(convertToPythonObject(value));
369 }
370 return outputList;
371 }

◆ GetTuple() [1/3]

void GetTuple ( const TupleType &  ,
boost::python::list &  ,
SizeT< 0 >   
)
inline

Break recursion when all members are already appended to the list.

Definition at line 419 of file PyObjConvUtils.h.

419{ }

◆ GetTuple() [2/3]

void GetTuple ( const TupleType &  tuple,
boost::python::list &  pyList 
)
inline

TMP (Template Meta Programming ) The given python list is filled, and later converted into a python tuple (in convertToPythonObject).

To fill the python list from the C++ std::tuple we need again TMP methods. The variadic template std::tuple is copied by the recursive defined template function GetTuple, the overloaded argument (type SizeT<>) of the function serves as a counter for the recursion depth.

Definition at line 412 of file PyObjConvUtils.h.

413 {
414 GetTuple(tuple, pyList, SizeT<std::tuple_size<TupleType>::value>());
415 }

◆ GetTuple() [3/3]

void GetTuple ( const TupleType &  tuple,
boost::python::list &  pyList,
SizeT< N >   
)
inline

Append elements of the tuple to pyList by recursively calling this function with the element index as argument type.

Definition at line 424 of file PyObjConvUtils.h.

425 {
426 GetTuple(tuple, pyList, SizeT < N - 1 > ());
427 pyList.append(convertToPythonObject(std::get < N - 1 > (tuple)));
428 }

◆ iteratePythonObject()

bool iteratePythonObject ( const boost::python::object &  pyObject,
Functor  function 
)

Helper function to loop over a python object that implements the iterator concept and call a functor with each element as argument.

Parameters
pyObjectthe python object to iterate over
functionany functor object which accepts a boost::python::object as its only argument and returns true if the iteration should be continued
Returns
true if the iteration was completed over all elements

Definition at line 255 of file PyObjConvUtils.h.

256 {
257 boost::python::object iterator(boost::python::handle<>(PyObject_GetIter(pyObject.ptr())));
258 PyObject* item{nullptr};
259 // ok, loop over the iterator and check all elements
260 while ((item = PyIter_Next(iterator.ptr()))) {
261 boost::python::object obj{boost::python::handle<>(item)}; // to make sure we properly decref
262 if (not function(obj)) return false;
263 }
264 return true;
265 }

◆ SetTuple() [1/3]

void SetTuple ( TupleType &  ,
const boost::python::tuple &  ,
SizeT< 0 >   
)
inline

Break recursion once all elements have been set.

Definition at line 599 of file PyObjConvUtils.h.

599{ }

◆ SetTuple() [2/3]

void SetTuple ( TupleType &  tuple,
const boost::python::tuple &  pyTuple 
)
inline

TMP (Template Meta Programming ) The given python tuple is written into the given c++ tuple.

To fill the C++ std::tuple we need again TMP methods. The variadic template std::tuple is filled by the recursive defined template function SetTuple, the overloaded argument (type SizeT<>) of the function serves as a counter for the recursion depth.

Definition at line 586 of file PyObjConvUtils.h.

587 {
588 static const unsigned N = std::tuple_size<TupleType>::value;
589 if ((unsigned)boost::python::len(pyTuple) != N) {
590 throw std::runtime_error(std::string("Given python tuple has length ") +
591 std::to_string(boost::python::len(pyTuple)) +
592 ", expected " + std::to_string(N));
593 }
594 SetTuple(tuple, pyTuple, SizeT<N>());
595 }

◆ SetTuple() [3/3]

void SetTuple ( TupleType &  tuple,
const boost::python::tuple &  pyTuple,
SizeT< N >   
)
inline

Set the elements of the tuple recursively by calling with the function with the next tuple index and converting one element.

Definition at line 604 of file PyObjConvUtils.h.

605 {
606 SetTuple(tuple, pyTuple, SizeT < N - 1 > ());
607 std::get < N - 1 > (tuple) = convertPythonObject(pyTuple[N - 1], std::get < N - 1 > (tuple));
608 }

◆ SetVariant() [1/2]

void SetVariant ( boost::variant< Types... > &  ,
const boost::python::object &  pyObject,
SizeT< 0 >   
)
inline

TMP (Template Meta Programming ) The given python object is written into the given c++ boost variant.

To fill the C++ boost::variant we need again TMP methods. The variadic template boost::variant is filled by the recursive defined template function SetVariant, the overloaded argument (type SizeT<>) of the function serves as a counter for the recursion depth.

Definition at line 635 of file PyObjConvUtils.h.

636 {
637 throw std::runtime_error(std::string("Could not set module parameter: Expected type '") +
638 Type<boost::variant<Types...> >::name() + "' instead of '" +
639 pyObject.ptr()->ob_type->tp_name + "'.");
640 }

◆ SetVariant() [2/2]

void SetVariant ( boost::variant< Types... > &  variant,
const boost::python::object &  pyObject,
SizeT< N >   
)
inline

Recursively go through all possible types of the variant and check which one is a possible conversion.

Definition at line 644 of file PyObjConvUtils.h.

645 {
646 using Scalar = typename std::tuple_element < N - 1, std::tuple<Types...> >::type;
647 if (checkPythonObject(pyObject, Scalar())) {
648 try {
649 Scalar value = convertPythonObject(pyObject, Scalar());
650 variant = value;
651 return;
652 } catch (std::runtime_error& e) {
653 }
654 }
655 // Conversion failed - try next type
656 SetVariant(variant, pyObject, SizeT < N - 1 > ());
657 }