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: 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 Subject: Re: The "right" way to use config files Date: Sat, 09 Aug 2014 22:17:25 +1000 References: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 Fabien 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