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


Groups > comp.lang.python > #75944

Re: The "right" way to use config files

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'parser': 0.07; 'imported': 0.09; 'parameter': 0.09; 'parsing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:files': 0.09; 'def': 0.12; 'attributes.': 0.16; 'finney': 0.16; "module's": 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'thereby': 0.16; 'elements': 0.16; 'module': 0.19; 'input': 0.22; 'import': 0.22; 'putting': 0.22; 'creating': 0.23; 'header :User-Agent:1': 0.23; 'config': 0.24; 'module,': 0.24; 'define': 0.26; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'rest': 0.29; 'everywhere': 0.31; 'idea,': 0.31; 'writes:': 0.31; 'file': 0.32; 'skip:d 20': 0.34; 'could': 0.34; 'there': 0.35; 'done': 0.36; 'responsible': 0.36; 'should': 0.36; 'application': 0.37; 'ben': 0.38; 'to:addr:python-list': 0.38; 'bad': 0.39; 'subject:" ': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'simply': 0.61; 'information': 0.63; 'subject:The': 0.64; 'god': 0.65; 'watching': 0.68; 'configparser': 0.84; 'i\xe2\x80\x99ll': 0.84; 'received:125': 0.84; 'technique.': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: The "right" way to use config files
Date Sat, 09 Aug 2014 22:17:25 +1000
References <ls51q0$4ea$1@speranza.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
Cancel-Lock sha1:MpSReLw2qyEkWotHBMea+MWYzSw=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.12792.1407586666.18130.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1407586666 news.xs4all.nl 2969 [2001:888:2000:d::a6]:56963
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:75944

Show key headers only | View raw


Fabien <fabien.maussion@gmail.com> writes:

> So I had the idea to define a super-object which parses the config
> file and input data and is given as a single parameter to the
> processing functions, and the functions take the information they need
> from it.

That's not a bad idea, you could do that without embarrassment.

A better technique, though, is to make use of modules as namespaces.
Have one module of your application be responsible for the configuration
of the application::

    # app/config.py

    import configparser

    parser = configparser.ConfigParser()
    parser.read("app.conf")

and import that module everywhere else that needs it::

    # app/wibble.py

    from . import config

    def frobnicate():
        do_something_with(config.foo)

By using an imported module, the functions don't need to be
parameterised by application-wide configuration; they can simply access
the module from the global scope and thereby get access to that module's
attributes.

> To get to the point: is it good practice to give all elements of a
> program access to the configfile and if yes, how is it done
> "properly"?

There should be an encapsulation of the responsibility for parsing and
organising the configuration options, and the rest of the application
should access it only via that encapsulation.

Putting that encapsuation in a module is an appropriately Pythonic
technique.

-- 
 \          “Now Maggie, I’ll be watching you too, in case God is busy |
  `\       creating tornadoes or not existing.” —Homer, _The Simpsons_ |
_o__)                                                                  |
Ben Finney

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


Thread

The "right" way to use config files Fabien <fabien.maussion@gmail.com> - 2014-08-09 13:48 +0200
  Re: The "right" way to use config files Ben Finney <ben+python@benfinney.id.au> - 2014-08-09 22:17 +1000
    Re: The "right" way to use config files Fabien <fabien.maussion@gmail.com> - 2014-08-09 14:33 +0200
      Re: The "right" way to use config files Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-08-09 12:16 -0400
        Re: The "right" way to use config files Fabien <fabien.maussion@gmail.com> - 2014-08-09 19:17 +0200
  Re: The "right" way to use config files Tim Chase <python.list@tim.thechases.com> - 2014-08-09 12:08 -0500
  Re: The "right" way to use config files Terry Reedy <tjreedy@udel.edu> - 2014-08-09 13:29 -0400
    Re: The "right" way to use config files Fabien <fabien.maussion@gmail.com> - 2014-08-09 20:14 +0200
      Re: The "right" way to use config files Terry Reedy <tjreedy@udel.edu> - 2014-08-09 18:30 -0400
        Re: The "right" way to use config files Fabien <fabien.maussion@gmail.com> - 2014-08-10 10:33 +0200

csiph-web