[Discuss] Application config files

Derek Martin invalid at pizzashack.org
Fri Jul 12 17:16:15 EDT 2019


On Fri, Jul 12, 2019 at 02:07:20PM -0400, Steve Litt wrote:
> On Fri, 12 Jul 2019 13:40:01 -0400
> Dan Ritter <dsr at randomstring.org> wrote:
> 
> > Jerry Feldman wrote: 
> > > I have an application that I wrote where I am using a .ini style
> > > file for config. I chose that as an afterthought but maybe JSON,
> > > YAML, or TOML might be better formats.

> JSON is out from the get-go: It's not meant to be read and written by
> humans.

I largely disagree.  If it's currently in INI format the equivalent
JSON should generally be perfectly human-readable, e.g.:

{
    "thingy": {
        "name":  "foo",
        "ip":    "10.1.2.3",
        "value": 1,
        "live":  true,
        "notes": null
    }
}

Here, "thingy" would be equivalent to your [section] and then its
fields are the name-value pairs of that section.

The advantages of JSON are that it's pretty ubiquitous, there are
parsers to read and write the file available already in pretty much
every language, so it's less code you have to write and test yourself,
and if you ever want to switch languages this is a non-issue.  Plus,
the code needed to parse the file is generally trivial, especially in
Python.

    >>> import json
    >>> data = '''
    ... {
    ...     "thingy": {
    ...         "name":  "foo",
    ...         "ip":    "10.1.2.3",
    ...         "value": 1,
    ...         "live":  true,
    ...         "notes": null
    ...     }   
    ... }
    ... '''
    >>> parsed = json.loads(data)
    >>> parsed
    {u'thingy': {u'notes': None, u'ip': u'10.1.2.3', u'live': True, u'name': u'foo', u'value': 1}}
    >>> print parsed["thingy"]["name"]
    foo

Of course, Python can do any of the mentioned options, so if you're
sure you'll stick with that, it probably really doesn't matter.

-- 
Derek D. Martin    http://www.pizzashack.org/   GPG Key ID: 0xDFBEAD02
-=-=-=-=-
This message is posted from an invalid address.  Replying to it will result in
undeliverable mail due to spam prevention.  Sorry for the inconvenience.



More information about the Discuss mailing list