Most Pythonic way to store (small) configuration

Rustom Mody rustompmody at gmail.com
Wed Aug 5 09:37:04 EDT 2015


On Wednesday, August 5, 2015 at 6:58:01 PM UTC+5:30, Tim Chase wrote:
> On 2015-08-02 12:11, Cecil Westerhof wrote:
> > There are a lot of ways to store configuration information:
> > - conf file
> > - xml file
> > - database
> > - json file
> > - and possible a lot of other ways
> > 
> > I want to write a Python program to display cleaned log files. I do
> > not think I need a lot of configuration to be stored:
> > - some things relating to the GUI
> > - default behaviour
> > - default directory
> > - log files to display, including some info
> >   - At least until where it was displayed
> > 
> > Because of this I think a human readable file would be best.
> 
> Yet another mostly-built-in option is to just have a simple file of
> key/value pairs, optionally with comments.  This can be read with
> something like
> 
>   config = {}
>   with open('config.ini') as f:
>     for row in f:
>       row = row.strip()
>       if not row or row.startswith(('#', ';')):
>         continue
>       k, _, v = row.partition('=')
>       config[k.strip().upper()] = v.lstrip()
> 
> which is pretty straight-forward and easy format to edit.
> 
> -tkc

JSON handles basic types like this:
>>> from json import loads
>>> loads("""{"anInt":1, "aString":"2"}""")
{'aString': '2', 'anInt': 1}



More information about the Python-list mailing list