Most Pythonic way to store (small) configuration

Tim Chase python.list at tim.thechases.com
Wed Aug 5 16:55:32 EDT 2015


On 2015-08-05 06:37, Rustom Mody wrote:
> >   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}

But requires the person hand-editing the file to make sure that
opening braces close, that quoted text is properly opened/closed, has
peculiarities regarding things following back-slashes, etc.

There's a certain simplicity to simply having key/value pairs
separated by an "=" and then letting the application do whatever it
needs/wants with those key/value strings.

-tkc






More information about the Python-list mailing list