• 15
• 15
• 11
• 9
• 10

# Using python as a configuration file

This topic is 2732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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:
//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?

##### Share on other sites
One choice would be not to use the Python C API directly, but use a wrapper like boost::python.

##### Share on other sites
I'd also suggest that Lua would be a much easier choice than Python.

##### Share on other sites
Quote:
 Original post by PromitI'd also suggest that Lua would be a much easier choice than Python.

Just curious, why do you say that? Because the Lua language itself is more convenient for this, or because it's easier to embed?

##### Share on other sites
I would guess because it's easier to embed (its a lot easier to embed).

However, once you have embedded it you can use it in general for a general-purpose scripting language, and if you are considering doing this, then python is a much better syntax imho.

##### Share on other sites
Lua is a heck of a lot easier to embed, and it's really easy to get data out. If you only need to get a few primitive types in and out, it would just be a few calls to set up the interpreter, run the input file, and grab globals. Extracting specific values from tables, which serve as both lists and dictionaries, is only slightly more work.

In defense of Lua, its syntax is tough to beat for simplicity, which is good for simple scripting tasks. I would only go with Python if Lua's features were really inadequate.

##### Share on other sites
I've used Lua scripts as settings files in my application but one thing not quite good about that is that if there is an error in just one line of the file then it rejects the whole file.

So when I have default settings (hard-coded in C++) and run a script to over-ride them and if there is an error in the script then only the default settings are used.

It would be better if it would only reject the setting from the one line that had an error on it. I don't know how to implement this with Lua yet.

And in order to fix the error in that one line I would need to be able to identify exactly which setting was it that was wrong. I wouldn't want to over-write all of the settings the user has created with the default settings. That would fix the error but the user would lose all of his changes to the settings.

I guess using an exception handler of some sort could help? Maybe someone dealt with this problem already?

I can imagine the same problem could happen with Python too.

[Edited by - reptor on September 30, 2010 10:42:47 AM]

##### Share on other sites
Thanks for the suggestions. I'll take a look at boost and lua.

##### Share on other sites
javascript hasn't gotten much cred in the gaming world thus far, but I would not ignore it as an option. I believe the Wolfire guys are using Google's engine. I like JSON a lot for data description, prototypal inheritance is ridiculously useful in scripting language, and the C-like syntax feels like home.