LDMX Software
|
python execution and parameter extraction More...
Classes | |
class | Parameters |
Class encapsulating parameters for configuring a processor. More... | |
Functions | |
Parameters | run (const std::string &root_object, const std::string &pythonScript, char *args[], int nargs) |
run the python script and extract the parameters | |
static std::string | getPyString (PyObject *pyObj) |
Turn the input python string object into a std::string. | |
std::string | repr (PyObject *obj) |
Get a C++ string representation of the input python object. | |
PyObject * | extractDictionary (PyObject *obj) |
extract the dictionary of attributes from the input python object | |
static Parameters | getMembers (PyObject *object) |
Extract members from a python object. | |
python execution and parameter extraction
this namespace is focused on holding the necessary functions to run and extract the configuration parameters from a python script. The documentation here is focused on the C++-side of the configuration - i.e. the translation of Python objects into their C++ counterparts.
PyObject * framework::config::extractDictionary | ( | PyObject * | obj | ) |
extract the dictionary of attributes from the input python object
This is separated into its own function to isolate the code that depends on the python version. Since memory-saving measures were integrated into Python between 3.6.9 and 3.10.6, we need to alter how we were using the C API to access an objects dict attribute. We are now using a "hidden" Python C API function which has only been added to the public C API documentation in Python 3.11. It has been manually tested for the two different Python versions we use by running the tests (some of which test the ConfigurePython) and running a basic simulation.
Exception | if object does not have a dict and isn't a dict |
obj | pointer to python object to extract from |
This was developed for Python3.10 when upgrading to Ubuntu 22.04 in the development container image. A lot of memory-saving measures were taken which means we have to explicitly ask Python to construct the dict object for us so that it knows to "waste" the memory on it.
https://docs.python.org/3/c-api/object.html
We use _PyObject_GetDictPtr because that will not set an error if the dict does not exist.
Definition at line 91 of file Python.cxx.
References repr().
Referenced by getMembers().
|
static |
Extract members from a python object.
Iterates through the object's dictionary and translates the objects inside of it into the type-specified C++ equivalents, then puts these objects into an instance of the Parameters class.
This function is recursive. If a non-base type is encountered, we pass it back along to this function to translate it's own dictionary.
We rely completely on python being awesome. For all higher level class objects, python keeps track of all of its member variables in the member dictionary __dict__
.
No Py_DECREF calls are made because all of the members of an object are borrowed references, meaning that when we destory that object, it handles the other members. We destroy the one Python object owning all of these references at the end of this function.
{}
.This recursive extraction method is able to handle the following cases.
__dict__
member) are extracted to Parametersdict
objects are extracted to Parametersstr
are extracted to std::stringint
are extracted to C++ int
bool
are extracted to C++ bool
float
are extracted to C++ double
Known design flaws include
[in] | object | Python object to get members from |
Definition at line 161 of file Python.cxx.
References framework::config::Parameters::add(), extractDictionary(), getMembers(), and getPyString().
Referenced by getMembers(), and run().
|
static |
Turn the input python string object into a std::string.
Helpful to condense down the multi-line nature of the python3 code.
[in] | pyObj | python object assumed to be a string python object |
Definition at line 50 of file Python.cxx.
Referenced by getMembers(), and repr().
std::string framework::config::repr | ( | PyObject * | obj | ) |
Get a C++ string representation of the input python object.
This is replicating the repr(obj)
syntax of Python.
[in] | obj | python object to get repr for |
Definition at line 66 of file Python.cxx.
References getPyString().
Referenced by extractDictionary().
Parameters framework::config::run | ( | const std::string & | root_object, |
const std::string & | pythonScript, | ||
char * | args[], | ||
int | nargs ) |
run the python script and extract the parameters
This method contains all the parsing and execution of the python script.
Exception | if the python script does not exit properly |
Exception | if any necessary components of the python configuration are missing. e.g. The Process class or the different members of the lastProcess object. |
The basic premise of this function is to execute the python configuration script. Then, after the script has been executed, all of the parameters for the Process are gathered from python. The fact that the script has been executed means that the user can get up to a whole lot of shenanigans that can help them make their work more efficient.
[in] | full | pythonic path to the object to kickoff extraction |
[in] | pythonScript | Filename location of the python script. |
[in] | args | Commandline arguments to be passed to the python script. |
[in] | nargs | Number of commandline arguments, assumed to be >= 0 |
Definition at line 292 of file Python.cxx.
References getMembers().