Original Post
Hi, I'm writing a program in C++ and want initialize it with a configuration file. I usually use libconfig but in this case I have many similar configuration settings and in many cases it'd be easier to write the configuration file using basic programming constructs like loops.
So I thought about using python scripts for the configuration files. The file would contain simple dictionaries and lists which may be constructed either by hand or using basic python scripting.
My question is, assuming I have a file called config.py, what's the easiest way to read all of the top level variables (integers, lists, dictionaries) from inside a C++ program?
I know that by using the Python C-API I can access dir(config) and then use PyDict_GetItemString to get different variables. But what if I have something like:
list1[{'a':1}, {'a':2}]
in the config.py file? Is there an easy way to get the value of list1[0]['a'] without having to use multiple specialized C-API calls? I was thinking of something like this:
The problem with this is that it's just a hack. Is there any better way to approach this problem?
So I thought about using python scripts for the configuration files. The file would contain simple dictionaries and lists which may be constructed either by hand or using basic python scripting.
My question is, assuming I have a file called config.py, what's the easiest way to read all of the top level variables (integers, lists, dictionaries) from inside a C++ program?
I know that by using the Python C-API I can access dir(config) and then use PyDict_GetItemString to get different variables. But what if I have something like:
list1[{'a':1}, {'a':2}]
in the config.py file? Is there an easy way to get the value of list1[0]['a'] without having to use multiple specialized C-API calls? I was thinking of something like this:
//these line are run oncePyRun_SimpleString("import config");PyObject* module = PyImport_AddModule("__main__");//these three lines can be used for any amount of nestingPyRun_SimpleString("exec('cVar = config.list1[0][\\'a\\'])"); PyObject* dict = PyModule_GetDict(module);PyObject* result = PyDict_GetItemString(dict, "cVar");The problem with this is that it's just a hack. Is there any better way to approach this problem?