views:

58

answers:

5

Hey guys, I am making an auto-archiver/backup sort of thing in Python(http://sourceforge.net/projects/frozendirectory/). As of now it uses an ordinary text file to store its small amount of settings(but ever growing). I was thinking of changing it to an XML file so that it would be more flexible/scalable and thus easier to work with in the long run. However, when I asked a friend he recommended that I just keep it in a python file, such as settings.py and just use 'import settings' from the file that needs the settings when needed. He claimed that it takes less space and I would only have to write code to write to the settings file as opposed to worrying about both reading and writing.

His points were convincing but made me wonder why no other large programs used the technique he is recommending.

So anyways, I just wanted to know what you guys think. Should I go with XML, .py, or something else? Thanks in advance.

+1  A: 

JSON is a good alternative to XML, it is also easy to read and write in Python.

too much php
+1  A: 

I find ConfigObj really useful for this. It's similar to the "ini" format commmon on Windows, but more flexible.

You can set types and constraints for configuration flags. You can also merge default settings with user settings, ie. some settings can be missing from the user's config file, indicating that the defaults should be used. This makes upgrades pretty seamless, unless you need some magical reinterpretation of existing settings.

RabbitVCS has an example of a ConfigObj specification and this is the code that implements it.

detly
Why not use the `ConfigParser` module, which is already included in the std.distr?
Nas Banov
I was not aware of that module — the project was using ConfigObj when I joined. Was it new in 2.5?
detly
A: 

Who says large programs don't use this technique? :-) They do, when it makes sense.

Why is there a trend to use XML? well that's to address issues involving validation of data being supplied, ensuring that multiple data generators don't have to learn about each other, ensuring that multiple readers will interpret the data identically, to encapsulate the nature of the data in the datafile itself... and on and on and on...

Why did your friend tell you to avoid it? because for your application it may be way too cumbersome :-)

Elf King
+1  A: 

His points were convincing but made me wonder why no other large programs used the technique he is recommending.

(Django uses settings.py)

For my own programs... I generally either use a Python file like you mentioned, or else I use ConfigParser.

It depends on who my audience is for the configuration file... a fellow python programmer or a non-programmer.

Corey Goldberg
If you want to store the config parameter values as list/tuple or dictionary then using *.py is pretty useful. If not then use ConfigParser, keeping them as text in a file. You can have multiple sets of parameters for different type of users/purposes. For example, you can have settings for different databases stored together in a single file, even the passwords encrypted ;-)
Technofreak
A: 

You friend might have given you the best idea... depends on your situation. It will be fast, it will be easy on you and the program, won't require summoning additional libraries (or heaven forbid, install one just for that).

The best argument against using import for settings file is that an "evil hacker" hypothetically may add some code to the file to interfere with the work of your program. Then again, somebody evil enough can replace your whole program with something else so... depends on the level of determination. You need to see if that is concern for whatever your program is targeted.

If you consider that unsuitable, I think you can keep your settings in plain text file if they are simple enough (after all reading INI-style settings is a one liner, a-la

settings = dict(s.split('=',1) for s in open('somefile'))

If your structures are more structured, either JSON or YAML should do. See also:

XML you can do... but that ain't human-friendly.

Nas Banov
Ok, so if I plan to go the import settings route I would have to implement functionality to write to the settings python file. Would you(or anyone else) happen to know of a python module that is capable of doing this so that I don't have to make one from scratch? And thanks to everyone for all their awesome responses!
Stunner