Python configuration file: Any file format recommendation? INI format still appropriate? Seems quite old school?

Consider using plain Python files as configuration files.

Consider using plain Python files as configuration files. An example (example. Conf): # use normal python comments value1 = 32 value2 = u"A unicode value" value3 = "A plain string value" value4 = "lists", "are", "handy" value5 = {"and": "so", "are": "dictionaries"} In your program, load the config file using execfile (2.7 docs): if __name__ == "__main__": config = {} execfile("example.

Conf", config) # python 3: exec(open("example. Conf").read(), config) print config"value1" print config"value5" I like this approach, for the following reasons: In the simple case, the format is as easy to author as an INI-style config file. It also shares an important characteristic with INI files: it is very suitable for version control (this is less true for XML and maybe also for JSON) I like the flexibility that comes with having the config file in an actual programming language.

The approach is widely used, a few examples: A Django site's settings lives inside settings.py. Django does not use execfile, it uses import to read/execute settings. Py AFAIK, but the end result is the same: the code inside the settings file is executed.

The bash shell reads and executes ~/. Bashrc on startup. The Python interpreter imports (and executes) site.Py on startup.

– codeape Nov 22 at 11:44 2 If the program runs with root privileges, the config file should have root-only write permissions. – codeape Nov 22 at 11:57 4 This also makes it tricky if you later want to be able to change the settings from a GUI, or set them programmatically in any way. Modifying a declarative context file is much simpler than modifying a script.

– Thomas K Nov 22 at 13:13 1 Kugel: And it's a pain to manage. It doesn't even read/exec the file.It imports it which makes it necessary to configure the sys. Path manually and all other sorts of hassles.

A true nightmare from an ops perspective.It's probably the point about Django which I like the least. – Noufal Ibrahim Nov 22 at 16:20.

INI is till totally OK and as other said, the format of your config file really depends from how you are going to use it. Personally I am a fan of YAML: concise, readable, flexible. Google seems to share my enthusiasm, as they use it too in the Google App Engine.

The python parser is here.

2 +1 for YAML. Readable, parseable and easy to write. – Noufal Ibrahim Nov 22 at 12:27 3 The downside of YAML is that you need an extra dependency to parse it.

– Thomas K Nov 22 at 13:19.

This entirely depends on your requirements. If (as you say) all you need is key/value pairs, ini files (or other "plain" config files) will perfectly suit you. No, they are not outdated, as they are still in use.

XML/JSON is perfect if you have hierarchical structures and also want to use more sophisticated methods (e. G: XML file validation, namespaces, etc. ).

3 I would recommend against using JSON, since (at least with Python's json parser) you cannot have comments inside JSON. – codeape Nov 22 at 11:28.

It depends on how the config file will be used. One of the advantages of INI files is that they are really easy to read and understand. It's much easier to make a mistake in JSON or XML file if you edit config by hand.

PHP still uses INI files. However, if you your config is not meant to be edited by hand, go with any format you like, because INI is not the easiest one to parse.

Dictionaries are pretty popular as well. Basically a hash table. {"one": 1, "two": 2} is an example, kind of looks like json.

Then you can call it up like mydict"one", which would return 1. Then you can use shelve to save the dictionary to a file: mydict = shelve. Open(filename) # then you can call it from there, like mydict"one" So, it's somewhat easier then an ini file.

You can add stuff just like a list or change options pretty easily and then once you close it, it will write it back out. Heres a simple example of what I mean: import shelve def main(): mydict = shelve. Open("testfile") mydict"newKey" = value("some comment", 5) print(mydict"newKey".

Value) print(mydict"newKey". Comment) mydict.close() class value(): def __init__(self, comment, value): self. Comment = comment self.

Value = value if __name__ == '__main__': main().

I guess the comment made for json is here valid as well: No comments possible?! – gecco Nov 22 at 12:12 @gecoo not entirely. You can store an object into a dictionary.

So, you can always store the value as an object and have a comment in there. So it's not exactly like json. – Matt Nov 22 at 12:13 @geeco I updated my answer with some simple sample code.

This isn't going to be in plaintext, so if you wanted to look at it, it won't be easy to read. Really just depends what you are looking for. – Matt Nov 22 at 12:26 I do not like having configuration settings in a binary format.

For two reasons: 1. You need a custom program to view and edit the settings (vs. a text editor for text-based formats) 2. The binary configuration file can not be easily version-controlled or compared with other configuration files.

– codeape Nov 22 at 12:44 4 "Registry"? Gah! Get the garlic and holy water!

– Paul McGuire Nov 22 at 13:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions