Reputation: 295
I'm writing a daemon script in Python and would like to be able to specify settings in a file that the program expects to read (like .conf files). The standard library has configparser and xdrlib file formats, but neither seems pythonic; the former being a Microsoft standard and the latter a Sun Microsystems standard.
I could write my own, but I'd rather stick to a well-known and documented standard, than reinvent the wheel.
Upvotes: 6
Views: 291
Reputation: 11
I started with configparser and optionsparser as well but for me the most fluent way is just to define dictionaries in a .py file and import it as a module. You can then access the configurations directly without any futher processing.
#
# example.py
#
config = {
"dialect" : "sqlite",
"driver" : "",
"database" : "testdb",
"host" : "localhost",
"port" : "0"
}
wherever you need access just do:
from example import config
print config["database"]
Upvotes: 1
Reputation: 11614
IMHO : Do NEVER touch the ConfigParser Module, its outdated (reads: obviously not brought up to date) and has quite some quirks. API for comments? Forget it! Access to the DEFAULT-Section? LOL! Wondering about not parsed Config-Fields? Well guess what: ConfigParser iterates over all your config files and fails silently (glossing over) to maybe EVENTUALLY do a condensed Exception-Trace over ALL Errors. It's not easy to find out which error belongs to which file. No nested config-sections...
Rather CSV then ConfigParser!
IMO: Use Json for config-files. Very flexible, direct mapping between python-data structures and Json-String representation is possible and still human readable. Btw. interchange between different languages is quite easy.
Curly-Braces in the file aren't that pretty, but you can't have it all! ;-)
Upvotes: 2
Reputation: 6507
Unless you have any particularly complex needs, there's nothing wrong with using configparser. It's a simple, flat file format that's easily understood and familiar to many people. If that really rubs you the wrong way, there's always the option of using JSON or YAML config but those are somewhat more heavyweight formats.
A third popular option is to just use Python for your configuration file. This gives you more flexibility but also increases the odds of introducing strange bugs by 'just' modifying your config file.
Upvotes: 5