Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #75955
| From | Fabien <fabien.maussion@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: The "right" way to use config files |
| Date | 2014-08-09 20:14 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <ls5odq$qn9$1@speranza.aioe.org> (permalink) |
| References | <ls51q0$4ea$1@speranza.aioe.org> <mailman.12797.1407605431.18130.python-list@python.org> |
On 09.08.2014 19:29, Terry Reedy wrote:
> If possible, functions should *return* their results, or yield their
> results in chunks (as generators). Let the driver function decide where
> to put results. Aside from separating concerns, this makes testing much
> easier.
I see. But then this is also true for parameters, right? And yet we
return to my original question ;-)
Let's say my configfile looks like this:
-----------------
### app/config.cfg
# General params
output_dir = '..'
input_file = '..'
# Func 1 params
[func1]
enable = True
threshold = 0.1
maxite = 1
-----------------
And I have a myconfig module which looks like:
-----------------
### app/myconfig.py
import ConfigObj
parser = obj() # parser will be instanciated by initialize
def initialize(cfgfile=None):
global parser
parser = ConfigObj(cfgfile, file_error=True)
-----------------
My main program could look like this:
-----------------
### app/mainprogram_1.py
import myconfig
def func1():
# the params are in the cfg
threshold = myconfig.parser['func1'].as_float('threshold')
maxite = myconfig.parser['func1'].as_long('maxite')
# dummy operations
score = 100.
ite = 1
while (score > threshold) and (ite < maxite):
score /= 10
ite += 1
# dummy return
return score
def main():
myconfig.initialize(sys.argv[1])
if myconfig.parser['func1'].as_bool('enable'):
results = func1()
if __name__ == '__main__':
main()
-----------------
Or like this:
-----------------
### app/mainprogram_2.py
import myconfig
def func1(threshold=None, maxite=None):
# dummy operations
score = 100.
ite = 1
while (score > threshold) and (ite < maxite):
score /= 10
ite += 1
# dummy return
return score
def main():
myconfig.initialize(sys.argv[1])
if myconfig.parser['func1'].as_bool('enable'):
# the params are in the cfg
threshold = myconfig.parser['func1'].as_float('threshold')
maxite = myconfig.parser['func1'].as_long('maxite')
results = func1(threshold=threshold, maxite=maxite)
if __name__ == '__main__':
main()
-----------------
In this case, program2 is easier to test/understand, but if the
parameters become numerous it could be a pain...
As always, I guess I'l have to decide on a case by case basis what is best.
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