Belle II Software  release-08-01-10
Belle2::PyObjConvUtils Namespace Reference

Python object converter utilities namespace. More...

Classes

struct  Type
 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...
 
struct  Type< std::vector< 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::map< A, B > >
 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  Type< int >
 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< float >
 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< std::string >
 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::tuple< Types... > >
 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< std::shared_ptr< Path > >
 Some modules take a path as argument so let's add this as well. More...
 
struct  SizeT
 Helper construct for TMP that provides an index at compile time to recurse through type lists. More...
 
class  convertToPythonObjectVisitor
 Helper function object to unpack a value from a variant to a python object. More...
 

Functions

bool checkPythonObject (const boost::python::object &pyObject, bool)
 check if the python object can be converted to the given type. More...
 
bool checkPythonObject (const boost::python::object &pyObject, float)
 check if the python object can be converted to the given type. More...
 
bool checkPythonObject (const boost::python::object &pyObject, double)
 check if the python object can be converted to the given type. More...
 
bool checkPythonObject (const boost::python::object &pyObject, int)
 check if the python object can be converted to the given an integral number More...
 
bool checkPythonObject (const boost::python::object &pyObject, unsigned int)
 check if the python object can be converted to the given an integral number More...
 
bool checkPythonObject (const boost::python::object &pyObject, unsigned long int)
 check if the python object can be converted to the given an integral number More...
 
bool checkPythonObject (const boost::python::object &pyObject, const std::string &)
 check if the python object can be converted to the given a string More...
 
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 More...
 
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 More...
 
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 More...
 
template<typename Scalar >
Scalar convertPythonObject (const boost::python::object &pyObject, Scalar)
 Convert from Python to given type. More...
 
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. More...
 
template<typename Value >
std::vector< Value > convertPythonObject (const boost::python::object &pyObject, const std::vector< Value > &)
 Convert from Python to given type. More...
 
template<typename Value >
std::set< Value > convertPythonObject (const boost::python::object &pyObject, const std::set< Value > &)
 Convert from Python to given type. More...
 
template<typename... Types>
std::tuple< Types... > convertPythonObject (const boost::python::object &pyObject, const std::tuple< Types... > &)
 Convert from Python to given type. More...
 
template<typename... Types>
boost::variant< Types... > convertPythonObject (const boost::python::object &pyObject, const boost::variant< Types... > &)
 Convert from Python to given type. More...
 
template<typename Type >
std::optional< TypeconvertPythonObject (const boost::python::object &pyObject, const std::optional< Type > &)
 Convert from Python to the given std::optional type. More...
 
template<typename Scalar >
boost::python::object convertToPythonObject (const Scalar &value)
 ------------— From C++ TO Python Converter ---------------------— More...
 
template<typename Value >
boost::python::list convertToPythonObject (const std::vector< Value > &vector)
 Writes content of a std::vector to a python list. More...
 
template<typename Value >
boost::python::object convertToPythonObject (const std::set< Value > &set)
 Convert std::set to a python object. More...
 
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. More...
 
template<typename... Types>
boost::python::tuple convertToPythonObject (const std::tuple< Types... > &tuple)
 Writes content of a std::tuple to a python tuple. More...
 
template<typename... Types>
boost::python::object convertToPythonObject (const boost::variant< Types... > &variant)
 Writes content of a boost::variant to a python object. More...
 
template<typename Type >
boost::python::object convertToPythonObject (const std::optional< Type > &optional)
 Convert from the given std::optional type to Python. More...
 
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. More...
 
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). More...
 
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. More...
 
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. More...
 
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/10]

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/10]

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.

◆ checkPythonObject() [3/10]

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.

◆ checkPythonObject() [4/10]

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.

◆ checkPythonObject() [5/10]

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.

◆ checkPythonObject() [6/10]

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.

◆ checkPythonObject() [7/10]

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.

◆ checkPythonObject() [8/10]

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.

◆ checkPythonObject() [9/10]

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.

◆ checkPythonObject() [10/10]

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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ GetTuple()

void Belle2::PyObjConvUtils::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.

◆ iteratePythonObject()

bool Belle2::PyObjConvUtils::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.

◆ SetTuple()

void Belle2::PyObjConvUtils::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.

◆ SetVariant()

void Belle2::PyObjConvUtils::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.