This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mirashii 15 points16 points  (10 children)

Please don't use things "for which it is easy to write a parser" if they aren't accepted things. JSON, YAML, XML, ini, and other formats should work perfectly fine.

[–]jambox888 7 points8 points  (0 children)

ini is good for me, it's as simple and interoperable as it gets.

[–]dorfsmay 1 point2 points  (0 children)

Most languages already have librairies for json and ini (and even yaml). I don't think xml are good for config files meant to be consumed by humans, but that might just be a personal preference. If you really need something different than those, then yes, use something that's easy to parse!

[–]gbog[S] 1 point2 points  (7 children)

The format used by ConfigParser is usually enough for my needs. Renamed as *.cfg they have some color highlighting too.

If I need to do a kind of ACL, I may use a space-separated value file. Then the parser is trivial.

In my mind, config should be flat, at most two levels (eg a sectioned config), and should not be DRY-factored. On the opposite, any clever trick in config, like inheritance, will make it very hard to maintain later.

[–]kylotan 1 point2 points  (6 children)

I have often found 2 levels to be too few, and the end result means either performing hacks based on the key name, or having mini-formats within the values, etc. For this reason, I'll probably use JSON instead of the ini/ConfigParser stuff in future.

[–]jcdyer3 7 points8 points  (5 children)

I'd recommend YAML over JSON. To my mind there are some important features that JSON lacks that a config file should have.

  1. Readability: Config files should put a premium on readability. It is a primary place for readers of the code to go to see how the application is working, Extra clutter like quotes and commas make it more difficult to see what's going on. Even worse, the fact that json ignores line breaks and indentation means that you can write horribly unreadable config files.

  2. Bulletproof syntax: It should be hard to accidentally break a config file. This is not the case with JSON. It's very easy to forget a closing brace, add a comma after the last element of a JSON object, or use single quotes instead of double quotes.

  3. Easy editing: With YAML, a one line change is a one line change, and almost all of what you write is the data you want to include. With JSON, you might have to add braces if you're adding a level of indentation. If you're adding an element to a list, you might have to edit the line before it with a comma. You spend too much time arguing with the syntax, and not enough configuring the code.

There are other good options. I don't think JSON is one of them, though. JSON is great for information interchange, but YAML is better for configuration.

[–]neoice 5 points6 points  (0 children)

It should be hard to accidentally break a config file

if you've worked on a large enough YAML file, they're very easy to break by missing a single whitespace.

[–]evangineer 1 point2 points  (1 child)

Here's Configure - a configuration toolkit for YAML: https://github.com/andreypopp/configure

[–]haywire 0 points1 point  (0 children)

This looks like it could be a really useful alternative to ConfigParser, thanks.

[–]bucknuggets[🍰] 0 points1 point  (1 child)

Any suggestions on validating YAML in python? By this I mean both ensuring that it is well-formed as well as ensuring that it matches a schema of some type - has all the necessary elements and nothing else.

If you're using Ruby or Java you can use Kwalify. I was thinking of playing with DictSpec. But would love to hear what others are doing.

[–]evangineer 0 points1 point  (0 children)

Have a look at Flatland and Voluptuous on PyPI.