pytorch-inference
pycpp::py_object Class Reference

#include <py_object.hpp>

Public Member Functions

 py_object ()
 
 py_object (PyObject *obj)
 
 py_object (const std::string &package, const std::string &py_home="")
 
 ~py_object ()
 
PyObject * operator() (const std::string &attr)
 
PyObject * operator() (const std::string &attr, PyObject *args, PyObject *kwargs=NULL)
 
PyObject * operator() (const std::string &attr, std::initializer_list< PyObject *> arg)
 
PyObject * operator() (const std::string &attr, std::initializer_list< PyObject *> arg, std::initializer_list< PyObject *> kwarg)
 
template<typename T >
void operator() (const std::string &attr, T &cpp_retval)
 
template<typename T >
void operator() (const std::string &attr, T &cpp_retval, PyObject *args, PyObject *kwargs=NULL)
 
template<typename T >
void operator() (const std::string &attr, T &cpp_retval, std::initializer_list< PyObject *> arg)
 
template<typename T >
void operator() (const std::string &attr, T &cpp_retval, std::initializer_list< PyObject *> arg, std::initializer_list< PyObject *> kwarg)
 
py_object py_class (const std::string &klass, PyObject *args=NULL, PyObject *kwargs=NULL)
 

Private Member Functions

void check_callable (PyObject *obj)
 

Private Attributes

PyObject * me
 The core Python module. More...
 

Detailed Description

Imports a python module and offers call operators to simplify calling member functions of that module from C++.

Constructor & Destructor Documentation

◆ py_object() [1/3]

pycpp::py_object::py_object ( )
inline

Default constructor - used internally only to make sure everything is working properly.

◆ py_object() [2/3]

pycpp::py_object::py_object ( PyObject *  obj)
inline

Allows for setting a py_object as a regular python object - as an object instead of a module or class. Also adds the object to the global namespace.

Parameters
objThe python object we're tring to set to Python

◆ py_object() [3/3]

pycpp::py_object::py_object ( const std::string &  package,
const std::string &  py_home = "" 
)
inline

Initializes a python module by starting the interpreter and importing the desired module.

Parameters
packageThe module to import.
py_homeThe home directory if it's different from pycpp::python_home (or the same, I don't judge)

◆ ~py_object()

pycpp::py_object::~py_object ( )
inline

Destructor - cleans up the python references and finalizes the interpreter.

Member Function Documentation

◆ check_callable()

void pycpp::py_object::check_callable ( PyObject *  obj)
inlineprivate

Checks if a PyObject * is callable - throws an error if it's not.

Parameters
objPyObject * to check.

◆ operator()() [1/8]

PyObject* pycpp::py_object::operator() ( const std::string &  attr)
inline

Allows us to cleanly call functions that take no arguments.

Parameters
attrString that is exactly the name of a member function of the python module initialized.
Returns
The return value of the function if there is one.

◆ operator()() [2/8]

PyObject* pycpp::py_object::operator() ( const std::string &  attr,
PyObject *  args,
PyObject *  kwargs = NULL 
)
inline

Given a string that is exactly a member function of a python module, calls that function with the given args and kwargs. args is a PyTuple and kwargs is a PyDict - the user can either create their own or use the pycpp functions make_tuple and make_dict provided here.

Parameters
attrString that is exactly the name of a member function of the python module initialized.
argsPyTuple that holds the arguments to pass to the function.
kwargsPyDict that holds the keyword arguments to pass to the function.
Returns
The return value of the function as a PyObject *. It is left to the user to convert that back to C++.

◆ operator()() [3/8]

PyObject* pycpp::py_object::operator() ( const std::string &  attr,
std::initializer_list< PyObject *>  arg 
)
inline

Given a string that is exactly a member function of a python module, calls that function with the given arguments as the function's args tuple. Creates a tuple of the arguments so that the function call can look like this: <py_object>("<function>", {pycpp::to_python("string"), pycpp::to_python(<value>)})

Parameters
attrString that is exactly the name of a member function of the python module initialized.
argsThe initializer list of PyObject * types that make up the args of the function
Returns
The return value of the function as a PyObject *. It is left to the user to convert that back to C++.

◆ operator()() [4/8]

PyObject* pycpp::py_object::operator() ( const std::string &  attr,
std::initializer_list< PyObject *>  arg,
std::initializer_list< PyObject *>  kwarg 
)
inline

Given a string that is exactly a member function of a python module, calls that function with the given arguments as the function's args tuple. Creates a tuple of the arguments so that the function call can look like this: <py_object>("<function>", {pycpp::to_python("string"), pycpp::to_python(<value>)})

Parameters
attrString that is exactly the name of a member function of the python module initialized.
argsThe initializer list of PyObject * types that make up the args of the function
argsThe initializer list of PyObject * types that make up the keyword args of the function
Returns
The return value of the function as a PyObject *. It is left to the user to convert that back to C++.

◆ operator()() [5/8]

template<typename T >
void pycpp::py_object::operator() ( const std::string &  attr,
T &  cpp_retval 
)
inline

Allows us to cleanly call functions that take no arguments.

Template Parameters
TThe C++ type of the return value of the function
Parameters
attrString that is exactly the name of a member function of the python module initialized.
Returns
The return value of the function as its C++ type.

◆ operator()() [6/8]

template<typename T >
void pycpp::py_object::operator() ( const std::string &  attr,
T &  cpp_retval,
PyObject *  args,
PyObject *  kwargs = NULL 
)
inline

Given a string that is exactly a member function of a python module, calls that function with the given args and kwargs. args is a PyTuple and kwargs is a PyDict - the user can either create their own or use the pycpp functions make_tuple and make_dict provided here.

Template Parameters
TC++ type of return value of specified function
Parameters
attrString that is exactly the name of a member function of the python module initialized.
argsPyTuple that holds the arguments to pass to the function.
kwargsPyDict that holds the keyword arguments to pass to the function.
Returns
The return value of the function as its C++ type. Throws errors if the object is not the correct type.

◆ operator()() [7/8]

template<typename T >
void pycpp::py_object::operator() ( const std::string &  attr,
T &  cpp_retval,
std::initializer_list< PyObject *>  arg 
)
inline

Given a string that is exactly a member function of a python module, calls that function with the given arguments as the function's args tuple. Creates a tuple of the arguments so that the function call can look like this: <py_object>("<function>", {pycpp::to_python("string"), pycpp::to_python(<value>)})

Template Parameters
TC++ type of return value of specified function
Parameters
attrString that is exactly the name of a member function of the python module initialized.
argsThe initializer list of PyObject * types that make up the args of the function
Returns
The return value of the function as its C++ type. Throws errors if the object is not the correct type.

◆ operator()() [8/8]

template<typename T >
void pycpp::py_object::operator() ( const std::string &  attr,
T &  cpp_retval,
std::initializer_list< PyObject *>  arg,
std::initializer_list< PyObject *>  kwarg 
)
inline

Given a string that is exactly a member function of a python module, calls that function with the given arguments as the function's args tuple. Creates a tuple of the arguments so that the function call can look like this: <py_object>("<function>", {pycpp::to_python("string"), pycpp::to_python(<value>)}, {pybpp::to_python("kwarg_key"), pycpp::to_python("kwarg_arg")})

Template Parameters
TC++ type of return value of specified function
Parameters
attrString that is exactly the name of a member function of the python module initialized.
argsThe initializer list of PyObject * types that make up the args of the function
kwargsThe initializer list of PyObject *types that make up the keyword arguments of the function
Returns
The return value of the function as its C++ type. Throws errors if the object is not the correct type.

◆ py_class()

py_object pycpp::py_object::py_class ( const std::string &  klass,
PyObject *  args = NULL,
PyObject *  kwargs = NULL 
)
inline

Imports a class from a package so that members of that class can be called upon. Name is under consideration for changes.

Parameters
klassThe class that is being imported - like pandas.DataFrame (for example)
Returns
A new py_object with the subpackage as its base.

Member Data Documentation

◆ me

PyObject* pycpp::py_object::me
private

The core Python module.


The documentation for this class was generated from the following file: