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

196 {
197 return PyBool_Check(pyObject.ptr());
198 }

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

331 {
332 return CheckVariant(variant, pyObject, SizeT<sizeof...(Types)>());
333 }
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 237 of file PyObjConvUtils.h.

238 {
239 if (not PyDict_Check(pyObject.ptr())) return false;
240 const boost::python::dict& pyDict = static_cast<const boost::python::dict&>(pyObject);
241 boost::python::list keys = pyDict.keys();
242 boost::python::list values = pyDict.values();
243 for (int i = 0; i < boost::python::len(keys); ++i) {
244 if (not checkPythonObject(keys[i], Key())) return false;
245 if (not checkPythonObject(values[i], Value())) return false;
246 }
247 return true;
248 }
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 339 of file PyObjConvUtils.h.

340 {
341 return pyObject.is_none() or checkPythonObject(pyObject, Type());
342 }
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 281 of file PyObjConvUtils.h.

282 {
283 if (not PyAnySet_Check(pyObject.ptr())) return false;
284 return iteratePythonObject(pyObject, [](const boost::python::object & element) {
285 return checkPythonObject(element, Value());
286 });
287 }
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 230 of file PyObjConvUtils.h.

231 {
232 return PyUnicode_Check(pyObject.ptr());
233 }

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

300 {
301 if (not PyTuple_Check(pyObject.ptr())) return false;
302 const boost::python::tuple& pyTuple = static_cast<const boost::python::tuple&>(pyObject);
303 return CheckTuple(tuple, pyTuple, std::index_sequence_for<Types ...>());
304 }
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 272 of file PyObjConvUtils.h.

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

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

225 {
226 return PyFloat_CheckExact(pyObject.ptr());
227 }

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

219 {
220 return PyFloat_CheckExact(pyObject.ptr());
221 }

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

202 {
203 return PyLong_CheckExact(pyObject.ptr());
204 }

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

207 {
208 return PyLong_CheckExact(pyObject.ptr());
209 }

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

213 {
214 return PyLong_CheckExact(pyObject.ptr());
215 }

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

293 {
294 return (... && checkPythonObject(pyTuple[Is], std::get<Is>(tuple)));
295 }

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

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

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

311 {
312 return false;
313 }

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

697 {
698 boost::variant<Types...> tmpVariant;
699 SetVariant(tmpVariant, pyObject, SizeT<sizeof...(Types)>());
700 return tmpVariant;
701 }
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 591 of file PyObjConvUtils.h.

592 {
593 std::map<Key, Value> tmpMap;
594 const boost::python::dict& pyDict = static_cast<const boost::python::dict&>(pyObject);
595 boost::python::list keys = pyDict.keys();
596
597 for (int i = 0; i < boost::python::len(keys); ++i) {
598 Key key = convertPythonObject(keys[boost::python::object(i)], Key());
599 Value value = convertPythonObject(pyDict[key], Value());
600 tmpMap.insert(std::pair<Key, Value>(key, value));
601 }
602 return tmpMap;
603 }
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 710 of file PyObjConvUtils.h.

711 {
712 std::optional<Type> tmpOptional = std::nullopt;
713 if (!pyObject.is_none()) {
714 tmpOptional = convertPythonObject(pyObject, Type());
715 }
716 return tmpOptional;
717 }

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

570 {
571 std::set<Value> result;
572 if (PyAnySet_Check(pyObject.ptr())) {
573 iteratePythonObject(pyObject, [&result](const boost::python::object & element) {
574 result.emplace(convertPythonObject(element, Value()));
575 return true;
576 });
577 } else {
578 result.emplace(convertPythonObject(pyObject, Value()));
579 }
580 return result;
581 }

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

528 {
529
530 std::shared_ptr<T> tmpValue;
531 boost::python::extract<std::shared_ptr<T>> valueProxy(pyObject);
532 if (valueProxy.check()) {
533 tmpValue = valueProxy();
534 } else {
535 throw std::runtime_error(
536 std::string("Could not convert value: Expected type '") + Type<std::shared_ptr<T>>::name() + "' instead of '" +
537 pyObject.ptr()->ob_type->tp_name + "'."
538 );
539 }
540 return tmpValue;
541
542 }

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

648 {
649 std::tuple<Types...> tmpTuple;
650 const boost::python::tuple& pyTuple = static_cast<const boost::python::tuple&>(pyObject);
651 SetTuple(tmpTuple, pyTuple);
652 return tmpTuple;
653 }
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 552 of file PyObjConvUtils.h.

553 {
554
555 std::vector<Value> tmpVector;
556 if (PyList_Check(pyObject.ptr()) or PyGen_Check(pyObject.ptr())) {
557 iteratePythonObject(pyObject, [&tmpVector](const boost::python::object & element) {
558 tmpVector.emplace_back(convertPythonObject(element, Value()));
559 return true;
560 });
561 } else {
562 tmpVector.emplace_back(convertPythonObject(pyObject, Value()));
563 }
564 return tmpVector;
565 }

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

504 {
505
506 Scalar tmpValue;
507 boost::python::extract<Scalar> valueProxy(pyObject);
508 if (valueProxy.check()) {
509 tmpValue = static_cast<Scalar>(valueProxy);
510 } else {
511 throw std::runtime_error(std::string("Could not convert value: Expected type '") + Type<Scalar>::name() + "' instead of '" +
512 pyObject.ptr()->ob_type->tp_name + "'.");
513 }
514 return tmpValue;
515
516 }
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 469 of file PyObjConvUtils.h.

470 {
471 return boost::apply_visitor(convertToPythonObjectVisitor(), variant);
472 }
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 355 of file PyObjConvUtils.h.

356 {
357 return boost::python::object(value);
358 }

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

398 {
399 boost::python::dict outputDict;
400 for (auto pair : map) {
401 outputDict[convertToPythonObject(pair.first)] = convertToPythonObject(pair.second);
402 }
403 return outputDict;
404 }
STL class.
STL class.
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 482 of file PyObjConvUtils.h.

483 {
484 if (optional) {
485 return convertToPythonObject(*optional);
486 } else {
487 return boost::python::object();
488 }
489 }

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

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

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

442 {
443 boost::python::list outputList;
444 GetTuple(tuple, outputList);
445 boost::python::tuple outputTuple(outputList);
446 return outputTuple;
447 }
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 367 of file PyObjConvUtils.h.

368 {
369 boost::python::list outputList;
370 for (const auto& value : vector) {
371 outputList.append(convertToPythonObject(value));
372 }
373 return outputList;
374 }
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 422 of file PyObjConvUtils.h.

422{ }

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

416 {
417 GetTuple(tuple, pyList, SizeT<std::tuple_size<TupleType>::value>());
418 }

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

428 {
429 GetTuple(tuple, pyList, SizeT < N - 1 > ());
430 pyList.append(convertToPythonObject(std::get < N - 1 > (tuple)));
431 }

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

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

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

628{ }

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

616 {
617 static const unsigned N = std::tuple_size<TupleType>::value;
618 if ((unsigned)boost::python::len(pyTuple) != N) {
619 throw std::runtime_error(std::string("Given python tuple has length ") +
620 std::to_string(boost::python::len(pyTuple)) +
621 ", expected " + std::to_string(N));
622 }
623 SetTuple(tuple, pyTuple, SizeT<N>());
624 }

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

634 {
635 SetTuple(tuple, pyTuple, SizeT < N - 1 > ());
636 std::get < N - 1 > (tuple) = convertPythonObject(pyTuple[N - 1], std::get < N - 1 > (tuple));
637 }

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

665 {
666 throw std::runtime_error(std::string("Could not set module parameter: Expected type '") +
667 Type<boost::variant<Types...> >::name() + "' instead of '" +
668 pyObject.ptr()->ob_type->tp_name + "'.");
669 }

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

674 {
675 using Scalar = typename std::tuple_element < N - 1, std::tuple<Types...> >::type;
676 if (checkPythonObject(pyObject, Scalar())) {
677 try {
678 Scalar value = convertPythonObject(pyObject, Scalar());
679 variant = value;
680 return;
681 } catch (std::runtime_error& e) {
682 }
683 }
684 // Conversion failed - try next type
685 SetVariant(variant, pyObject, SizeT < N - 1 > ());
686 }