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.







--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to