Sign in to follow this  

Using python as a configuration file

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

If you intended to correct an error in the post then please contact us.

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 once
PyRun_SimpleString("import config");
PyObject* module = PyImport_AddModule("__main__");

//these three lines can be used for any amount of nesting
PyRun_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 this post


Link to post
Share on other sites
Quote:
Original post by Promit
I'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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
The advantage to Lua is that Lua was originally designed as a configuration language. It has various syntactic sugar to help with this, and its API is well-designed for modular readout and querying of hierarchical data.

Incidentally, decide very early on what sort of computation you want going on in your "configuration" file. If you use Lua or Python for data, there's a great temptation to start doing processing in them; it starts with a couple conditionals, then you start adding loops, and before you know it a significant part of your game is housed in user-config.cfg. This is especially likely if you aren't exposing the same language for scripting in the application. Beware. (JSON doesn't suffer from this problem, because you pick your JSON reader based on what execution capabilities you want.)

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this