Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #75944
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Subject | Re: The "right" way to use config files |
| Date | 2014-08-09 22:17 +1000 |
| References | <ls51q0$4ea$1@speranza.aioe.org> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.12792.1407586666.18130.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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