Belle II Software light-2607-kasei
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 argument 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 T>
std::shared_ptr< T > convertPythonObject (const boost::python::object &pyObject, const std::shared_ptr< T > &)
 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 sentinel 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 203 of file PyObjConvUtils.h.

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

◆ checkPythonObject() [2/13]

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.

Definition at line 338 of file PyObjConvUtils.h.

339 {
340 return CheckVariant(variant, pyObject, SizeT<sizeof...(Types)>());
341 }
bool CheckVariant(const VariantType &, const boost::python::object &, SizeT< 0 >)
Recursion sentinel 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]

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.

Definition at line 245 of file PyObjConvUtils.h.

246 {
247 if (not PyDict_Check(pyObject.ptr())) return false;
248 const boost::python::dict& pyDict = static_cast<const boost::python::dict&>(pyObject);
249 boost::python::list keys = pyDict.keys();
250 boost::python::list values = pyDict.values();
251 for (int i = 0; i < boost::python::len(keys); ++i) {
252 if (not checkPythonObject(keys[i], Key())) return false;
253 if (not checkPythonObject(values[i], Value())) return false;
254 }
255 return true;
256 }
bool checkPythonObject(const boost::python::object &pyObject, bool)
check if the python object can be converted to the given type.

◆ checkPythonObject() [4/13]

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

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 347 of file PyObjConvUtils.h.

348 {
349 return pyObject.is_none() or checkPythonObject(pyObject, Type());
350 }
Converts a template argument into a string for corresponding Python type.

◆ checkPythonObject() [5/13]

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

Definition at line 289 of file PyObjConvUtils.h.

290 {
291 if (not PyAnySet_Check(pyObject.ptr())) return false;
292 return iteratePythonObject(pyObject, [](const boost::python::object & element) {
293 return checkPythonObject(element, Value());
294 });
295 }
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 238 of file PyObjConvUtils.h.

239 {
240 return PyUnicode_Check(pyObject.ptr());
241 }

◆ checkPythonObject() [7/13]

template<typename... Types>
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 307 of file PyObjConvUtils.h.

308 {
309 if (not PyTuple_Check(pyObject.ptr())) return false;
310 const boost::python::tuple& pyTuple = static_cast<const boost::python::tuple&>(pyObject);
311 return CheckTuple(tuple, pyTuple, std::index_sequence_for<Types ...>());
312 }
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]

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

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

Definition at line 280 of file PyObjConvUtils.h.

281 {
282 if (not PyList_Check(pyObject.ptr())) return false;
283 return iteratePythonObject(pyObject, [](const boost::python::object & element) {
284 return checkPythonObject(element, Value());
285 });
286 }

◆ 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 232 of file PyObjConvUtils.h.

233 {
234 return PyFloat_CheckExact(pyObject.ptr());
235 }

◆ 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 226 of file PyObjConvUtils.h.

227 {
228 return PyFloat_CheckExact(pyObject.ptr());
229 }

◆ 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 209 of file PyObjConvUtils.h.

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

◆ 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 214 of file PyObjConvUtils.h.

215 {
216 return PyLong_CheckExact(pyObject.ptr());
217 }

◆ 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 220 of file PyObjConvUtils.h.

221 {
222 return PyLong_CheckExact(pyObject.ptr());
223 }

◆ CheckTuple()

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.

Definition at line 299 of file PyObjConvUtils.h.

301 {
302 return (... && checkPythonObject(pyTuple[Is], std::get<Is>(tuple)));
303 }

◆ CheckVariant() [1/2]

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.

Definition at line 325 of file PyObjConvUtils.h.

328 {
329 using Scalar = typename std::tuple_element < N - 1, std::tuple<Types...> >::type;
330 if (checkPythonObject(pyObject, Scalar())) {
331 return true;
332 }
333 return CheckVariant(variant, pyObject, SizeT < N - 1 > ());
334 }

◆ CheckVariant() [2/2]

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

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

Definition at line 316 of file PyObjConvUtils.h.

319 {
320 return false;
321 }

◆ convertPythonObject() [1/8]

template<typename... Types>
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 704 of file PyObjConvUtils.h.

705 {
706 boost::variant<Types...> tmpVariant;
707 SetVariant(tmpVariant, pyObject, SizeT<sizeof...(Types)>());
708 return tmpVariant;
709 }
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/8]

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.

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 599 of file PyObjConvUtils.h.

600 {
601 std::map<Key, Value> tmpMap;
602 const boost::python::dict& pyDict = static_cast<const boost::python::dict&>(pyObject);
603 boost::python::list keys = pyDict.keys();
604
605 for (int i = 0; i < boost::python::len(keys); ++i) {
606 Key key = convertPythonObject(keys[boost::python::object(i)], Key());
607 Value value = convertPythonObject(pyDict[key], Value());
608 tmpMap.insert(std::pair<Key, Value>(key, value));
609 }
610 return tmpMap;
611 }
Scalar convertPythonObject(const boost::python::object &pyObject, Scalar)
Convert from Python to given type.

◆ convertPythonObject() [3/8]

template<typename Type>
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 718 of file PyObjConvUtils.h.

719 {
720 std::optional<Type> tmpOptional = std::nullopt;
721 if (!pyObject.is_none()) {
722 tmpOptional = convertPythonObject(pyObject, Type());
723 }
724 return tmpOptional;
725 }

◆ convertPythonObject() [4/8]

template<typename Value>
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 577 of file PyObjConvUtils.h.

578 {
579 std::set<Value> result;
580 if (PyAnySet_Check(pyObject.ptr())) {
581 iteratePythonObject(pyObject, [&result](const boost::python::object & element) {
582 result.emplace(convertPythonObject(element, Value()));
583 return true;
584 });
585 } else {
586 result.emplace(convertPythonObject(pyObject, Value()));
587 }
588 return result;
589 }

◆ convertPythonObject() [5/8]

template<typename T>
std::shared_ptr< T > convertPythonObject ( const boost::python::object & pyObject,
const std::shared_ptr< T > &  )

Convert from Python to given type.

Reads a std::shared_ptr<T> from a python object.

Parameters
pyObjectPython object which stores the shared pointer.
std::shared_ptr<T>A shared pointer.
Returns
std::shared_ptr<T>, which holds the value from the python object.

Definition at line 535 of file PyObjConvUtils.h.

536 {
537
538 std::shared_ptr<T> tmpValue;
539 boost::python::extract<std::shared_ptr<T>> valueProxy(pyObject);
540 if (valueProxy.check()) {
541 tmpValue = valueProxy();
542 } else {
543 throw std::runtime_error(
544 std::string("Could not convert value: Expected type '") + Type<std::shared_ptr<T>>::name() + "' instead of '" +
545 pyObject.ptr()->ob_type->tp_name + "'."
546 );
547 }
548 return tmpValue;
549
550 }

◆ convertPythonObject() [6/8]

template<typename... Types>
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 655 of file PyObjConvUtils.h.

656 {
657 std::tuple<Types...> tmpTuple;
658 const boost::python::tuple& pyTuple = static_cast<const boost::python::tuple&>(pyObject);
659 SetTuple(tmpTuple, pyTuple);
660 return tmpTuple;
661 }
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() [7/8]

template<typename Value>
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 560 of file PyObjConvUtils.h.

561 {
562
563 std::vector<Value> tmpVector;
564 if (PyList_Check(pyObject.ptr()) or PyGen_Check(pyObject.ptr())) {
565 iteratePythonObject(pyObject, [&tmpVector](const boost::python::object & element) {
566 tmpVector.emplace_back(convertPythonObject(element, Value()));
567 return true;
568 });
569 } else {
570 tmpVector.emplace_back(convertPythonObject(pyObject, Value()));
571 }
572 return tmpVector;
573 }

◆ convertPythonObject() [8/8]

template<typename Scalar>
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 511 of file PyObjConvUtils.h.

512 {
513
514 Scalar tmpValue;
515 boost::python::extract<Scalar> valueProxy(pyObject);
516 if (valueProxy.check()) {
517 tmpValue = static_cast<Scalar>(valueProxy);
518 } else {
519 throw std::runtime_error(std::string("Could not convert value: Expected type '") + Type<Scalar>::name() + "' instead of '" +
520 pyObject.ptr()->ob_type->tp_name + "'.");
521 }
522 return tmpValue;
523
524 }
static std::string name()
type name.

◆ convertToPythonObject() [1/7]

template<typename... Types>
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 477 of file PyObjConvUtils.h.

478 {
479 return boost::apply_visitor(convertToPythonObjectVisitor(), variant);
480 }
Helper function object to unpack a value from a variant to a python object.

◆ convertToPythonObject() [2/7]

template<typename Scalar>
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 363 of file PyObjConvUtils.h.

364 {
365 return boost::python::object(value);
366 }

◆ convertToPythonObject() [3/7]

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.

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 405 of file PyObjConvUtils.h.

406 {
407 boost::python::dict outputDict;
408 for (auto pair : map) {
409 outputDict[convertToPythonObject(pair.first)] = convertToPythonObject(pair.second);
410 }
411 return outputDict;
412 }
boost::python::object convertToPythonObject(const Scalar &value)
------------— From C++ TO Python Converter ---------------------—

◆ convertToPythonObject() [4/7]

template<typename Type>
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 490 of file PyObjConvUtils.h.

491 {
492 if (optional) {
493 return convertToPythonObject(*optional);
494 } else {
495 return boost::python::object();
496 }
497 }

◆ convertToPythonObject() [5/7]

template<typename Value>
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 389 of file PyObjConvUtils.h.

390 {
391 boost::python::object result(boost::python::handle<>(PySet_New(nullptr)));
392 for (const auto& value : set) {
393 PySet_Add(result.ptr(), convertToPythonObject(value).ptr());
394 }
395 return result;
396 }

◆ convertToPythonObject() [6/7]

template<typename... Types>
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 449 of file PyObjConvUtils.h.

450 {
451 boost::python::list outputList;
452 GetTuple(tuple, outputList);
453 boost::python::tuple outputTuple(outputList);
454 return outputTuple;
455 }
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]

template<typename Value>
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 375 of file PyObjConvUtils.h.

376 {
377 boost::python::list outputList;
378 for (const auto& value : vector) {
379 outputList.append(convertToPythonObject(value));
380 }
381 return outputList;
382 }
STL class.

◆ GetTuple() [1/3]

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

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

Definition at line 430 of file PyObjConvUtils.h.

430{ }

◆ GetTuple() [2/3]

template<typename TupleType>
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 423 of file PyObjConvUtils.h.

424 {
425 GetTuple(tuple, pyList, SizeT<std::tuple_size<TupleType>::value>());
426 }

◆ GetTuple() [3/3]

template<typename TupleType, size_t N>
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 435 of file PyObjConvUtils.h.

436 {
437 GetTuple(tuple, pyList, SizeT < N - 1 > ());
438 pyList.append(convertToPythonObject(std::get < N - 1 > (tuple)));
439 }

◆ iteratePythonObject()

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.

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 266 of file PyObjConvUtils.h.

267 {
268 boost::python::object iterator(boost::python::handle<>(PyObject_GetIter(pyObject.ptr())));
269 PyObject* item{nullptr};
270 // ok, loop over the iterator and check all elements
271 while ((item = PyIter_Next(iterator.ptr()))) {
272 boost::python::object obj{boost::python::handle<>(item)}; // to make sure we properly decref
273 if (not function(obj)) return false;
274 }
275 return true;
276 }

◆ SetTuple() [1/3]

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

Break recursion once all elements have been set.

Definition at line 636 of file PyObjConvUtils.h.

636{ }

◆ SetTuple() [2/3]

template<typename TupleType>
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 623 of file PyObjConvUtils.h.

624 {
625 static const unsigned N = std::tuple_size<TupleType>::value;
626 if ((unsigned)boost::python::len(pyTuple) != N) {
627 throw std::runtime_error(std::string("Given python tuple has length ") +
628 std::to_string(boost::python::len(pyTuple)) +
629 ", expected " + std::to_string(N));
630 }
631 SetTuple(tuple, pyTuple, SizeT<N>());
632 }

◆ SetTuple() [3/3]

template<typename TupleType, size_t N>
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 641 of file PyObjConvUtils.h.

642 {
643 SetTuple(tuple, pyTuple, SizeT < N - 1 > ());
644 std::get < N - 1 > (tuple) = convertPythonObject(pyTuple[N - 1], std::get < N - 1 > (tuple));
645 }

◆ SetVariant() [1/2]

template<typename... Types>
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 672 of file PyObjConvUtils.h.

673 {
674 throw std::runtime_error(std::string("Could not set module parameter: Expected type '") +
675 Type<boost::variant<Types...> >::name() + "' instead of '" +
676 pyObject.ptr()->ob_type->tp_name + "'.");
677 }

◆ SetVariant() [2/2]

template<typename... Types, size_t N>
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 681 of file PyObjConvUtils.h.

682 {
683 using Scalar = typename std::tuple_element < N - 1, std::tuple<Types...> >::type;
684 if (checkPythonObject(pyObject, Scalar())) {
685 try {
686 Scalar value = convertPythonObject(pyObject, Scalar());
687 variant = value;
688 return;
689 } catch (const std::runtime_error& e) {
690 }
691 }
692 // Conversion failed - try next type
693 SetVariant(variant, pyObject, SizeT < N - 1 > ());
694 }