Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94904

Re: Most Pythonic way to store (small) configuration

Date 2015-08-03 08:49 +1000
From Cameron Simpson <cs@zip.com.au>
Subject Re: Most Pythonic way to store (small) configuration
References <87fv41r5vv.fsf@Equus.decebal.nl>
Newsgroups comp.lang.python
Message-ID <mailman.1160.1438555783.3674.python-list@python.org> (permalink)

Show all headers | View raw


On 02Aug2015 18:51, Cecil Westerhof <Cecil@decebal.nl> wrote:
>On Sunday  2 Aug 2015 13:54 CEST, Ben Finney wrote:
>> Cecil Westerhof <Cecil@decebal.nl> writes:
>>> Because of this I think a human readable file would be best.
>>
>> I agree with that criterion; in the absence of compelling reasons
>> otherwise, human-readable and -editable text is a good default.
[...]
>> The “INI” format as handled by the Python ‘configparser’ module is
>> what I would recommend for a simple flat configuration file. It is
>> more intuitive to edit, and has a conventional commenting format.
>
>Well, I would use nested data. (A file will have extra fields besides
>the name.) That is why I was thinking about json. But I will look into
>it.

Like others, I also recommend an INI file. You can always move to something 
more complex (and harder to edit) if it doesn't work out.

Note that "nested data" does not rule out an INI file unless you mean 
"recursive" data or quite a deep nesting of structure. Shallow structure (one 
or two levels) is very easy to embed in the INI format.

Eg:

  [main]
  this = that
  size = 10
  directory = /path/to/default/dir

  [gui]
  foreground = green
  style = baroque

  [*.log]
  stuff about log files...

  [/path/to/file1]
  title = something
  param1 = 5
  param2 = 6
  fields = column2, column7

  [column2:/path/to/file1]
  format = numeric
  scale = logarithmic

  [column7:/path/to/file1]
  format = roman
  scale = linear

See that by being a little tricky about the section names you can incorporate a 
fair degree of structure? I would not advocating going too fair down that 
rabbit hole, but for basic stuff it works well.

And of course you can put structure within a section:

  [foo]
  complexity = {'blah': 'blah blah', 'frib': {'frob': 1, 'frab': 2}}
  format_width = 10
  format_style = right-justified
  format_dialect = quoted

i.e. you could put (small) JSON snippet in some fields, or present a few 
parameters to describe "format".

There are several ways to do this kind of thing before reaching for more 
complex syntaxes. Just don't let it get too weird - that would probably be a 
sign you do want to reach for JSON or YAML.

But start with INI. Simple and easy.

Cheers,
Cameron Simpson <cs@zip.com.au>

A squealing tire is a happy tire.
        - Bruce MacInnes, Skip Barber Driving School instructor

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Most Pythonic way to store (small) configuration Cecil Westerhof <Cecil@decebal.nl> - 2015-08-02 12:11 +0200
  Re: Most Pythonic way to store (small) configuration Chris Angelico <rosuav@gmail.com> - 2015-08-02 20:49 +1000
  Re: Most Pythonic way to store (small) configuration Ben Finney <ben+python@benfinney.id.au> - 2015-08-02 21:54 +1000
    Re: Most Pythonic way to store (small) configuration Cecil Westerhof <Cecil@decebal.nl> - 2015-08-02 18:51 +0200
      Re: Most Pythonic way to store (small) configuration Lele Gaifax <lele@metapensiero.it> - 2015-08-02 22:02 +0200
      Re: Most Pythonic way to store (small) configuration Cameron Simpson <cs@zip.com.au> - 2015-08-03 08:49 +1000
      Re: Most Pythonic way to store (small) configuration Ben Finney <ben+python@benfinney.id.au> - 2015-08-03 11:16 +1000
  Re: Most Pythonic way to store (small) configuration Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-02 16:12 +0100
  Re: Most Pythonic way to store (small) configuration Tim Chase <python.list@tim.thechases.com> - 2015-08-02 16:11 -0500
    Re: Most Pythonic way to store (small) configuration Dan Sommers <dan@tombstonezero.net> - 2015-08-03 04:02 +0000
      Re: Most Pythonic way to store (small) configuration Steven D'Aprano <steve@pearwood.info> - 2015-08-03 23:38 +1000
        Re: Most Pythonic way to store (small) configuration Chris Angelico <rosuav@gmail.com> - 2015-08-03 23:46 +1000
        Re: Most Pythonic way to store (small) configuration Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-03 15:37 +0100
  Re: Most Pythonic way to store (small) configuration marco.nawijn@colosso.nl - 2015-08-04 07:53 -0700
    Re: Most Pythonic way to store (small) configuration Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2015-08-04 19:06 +0200
      Re: Most Pythonic way to store (small) configuration marco.nawijn@colosso.nl - 2015-08-04 11:37 -0700
    Re: Most Pythonic way to store (small) configuration Ben Finney <ben+python@benfinney.id.au> - 2015-08-05 05:59 +1000
      Re: Most Pythonic way to store (small) configuration Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-08-05 18:32 +1000
        Re: Most Pythonic way to store (small) configuration Chris Angelico <rosuav@gmail.com> - 2015-08-05 20:01 +1000
    Re: Most Pythonic way to store (small) configuration Michael Torrie <torriem@gmail.com> - 2015-08-04 19:32 -0600
      Re: Most Pythonic way to store (small) configuration Grant Edwards <invalid@invalid.invalid> - 2015-08-05 14:00 +0000
    Re: Most Pythonic way to store (small) configuration random832@fastmail.us - 2015-08-04 22:44 -0400
    Re: Most Pythonic way to store (small) configuration Michael Torrie <torriem@gmail.com> - 2015-08-04 22:48 -0600
      Re: Most Pythonic way to store (small) configuration Rustom Mody <rustompmody@gmail.com> - 2015-08-04 21:55 -0700
        Re: Most Pythonic way to store (small) configuration Lele Gaifax <lele@metapensiero.it> - 2015-08-05 08:54 +0200
  Re: Most Pythonic way to store (small) configuration Tim Chase <python.list@tim.thechases.com> - 2015-08-05 08:18 -0500
    Re: Most Pythonic way to store (small) configuration Rustom Mody <rustompmody@gmail.com> - 2015-08-05 06:37 -0700
      Re: Most Pythonic way to store (small) configuration Tim Chase <python.list@tim.thechases.com> - 2015-08-05 15:55 -0500
        Re: Most Pythonic way to store (small) configuration Marko Rauhamaa <marko@pacujo.net> - 2015-08-06 00:47 +0300
          Re: Most Pythonic way to store (small) configuration Tim Chase <python.list@tim.thechases.com> - 2015-08-05 18:43 -0500
          Re: Most Pythonic way to store (small) configuration Chris Angelico <rosuav@gmail.com> - 2015-08-06 10:07 +1000
            Re: Most Pythonic way to store (small) configuration Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-08-06 17:33 +1000
              Re: Most Pythonic way to store (small) configuration Chris Angelico <rosuav@gmail.com> - 2015-08-06 17:51 +1000
        Re: Most Pythonic way to store (small) configuration Rustom Mody <rustompmody@gmail.com> - 2015-08-05 18:01 -0700
          Re: Most Pythonic way to store (small) configuration Rustom Mody <rustompmody@gmail.com> - 2015-08-05 18:06 -0700
  Re: Most Pythonic way to store (small) configuration Rustom Mody <rustompmody@gmail.com> - 2015-08-05 06:46 -0700
    Re: Most Pythonic way to store (small) configuration Steven D'Aprano <steve@pearwood.info> - 2015-08-06 00:08 +1000
      Re: Most Pythonic way to store (small) configuration Rustom Mody <rustompmody@gmail.com> - 2015-08-05 07:25 -0700

csiph-web