views:

1060

answers:

8

Django uses real Python files for settings, Trac uses a .ini file, and some other pieces of software uses XML files to hold this information.

Are one of these approaches blessed by Guido and/or the Python community more than another?

+3  A: 

I am not sure that there is an 'official' way (it is not mentioned in the Zen of Python :) )- I tend to use the Config Parser module myself and I think that you will find that pretty common. I prefer that over the python file approach because you can write back to it and dynamically reload if you want.

Shane C. Mason
A: 

why would Guido blessed something that is out of his scope? No there is nothing particular blessed.

SilentGhost
upvoted, because you actually answered OPs question ("No there is nothing particular blessed"). The downvotes might come from the first part of your answer: Having a community shared "best practice" can really help getting started in a new language / new project...
Daren Thomas
I understand the role of Guido's rulings in relation to core, stdlib development, etc., but the question asks about basic use where good practice can hardly be defined.
SilentGhost
A: 

It is more of convenience. There is no official way per say. But using XML files would make sense as they can be manipulated by various other applications/libraries.

Checksum
the same can be said about json, ini files, etc.
SilentGhost
Humans aren't supposed to read/write XML.
Lakshman Prasad
Reading XML with the python standard library is just a pain...
Daren Thomas
@Darren: Not true if you are using Python 2.5 or higher (because of ElementTree)
Imran
@becomingGuru: XML Design Goal 6: "XML documents should be human-legible and reasonably clear."
S.Lott
@S. Lott - Sure they should but that does not mean that they ever really are. You must admit that it is much easier to edit an ini or properties file than an xml doc.
Shane C. Mason
@Shane C. Mason: Humans *are* supposed to read/write XML. As a practical matter, this is really hard, and I don't use XML much because of it. I use JSON or CSV files -- much easier to edit. But @becomingGuru's comment is -- in a narrow technical sense -- incorrect.
S.Lott
+9  A: 

Depends on the predominant intended audience.

If it is programmers who change the file anyway, just use python files like settings.py

If it is end users then, think about ini files.

Lakshman Prasad
+1  A: 

It depends largely on how complicated your configuration is. If you're doing a simple key-value mapping and you want the capability to edit the settings with a text editor, I think ConfigParser is the way to go.

If your settings are complicated and include lists and nested data structures, I'd use XML or JSON and create a configuration editor.

For really complicated things where the end user isn't expected to change the settings much, or is more trusted, just create a set of Python classes and evaluate a Python script to get the configuration.

Kamil Kisiel
+1  A: 

Don't know if this can be considered "official", but it is in standard library: 14.2. ConfigParser — Configuration file parser.

This is, obviously, not an universal solution, though. Just use whatever feels most appropriate to the task, without any necessary complexity (and — especially — Turing-completeness! Think about automatic or GUI configurators).

drdaeman
json is in standard library too, along with all kinds of xml-processing modules
SilentGhost
+2  A: 

Just one more option, PyQt. Qt has a platform independent way of storing settings with the QSettings class. Underneath the hood, on windows it uses the registry and in linux it stores the settings in a hidden conf file. QSettings works very well and is pretty seemless.

jhufford
+4  A: 

As many have said, there is no "offical" way. There are, however, many choices. There was a talk at PyCon this year about many of the available options:

http://us.pycon.org/2009/conference/schedule/event/5/

brianz