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


Groups > comp.lang.python > #75958

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!newsfeed.xs4all.nl!newsfeed2.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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; ';-)': 0.03; 'algorithm': 0.04; 'parameters': 0.04; 'float': 0.07; 'initialize': 0.07; 'parser': 0.07; 'see.': 0.07; 'work!': 0.07; '0.1': 0.09; '__name__': 0.09; 'main()': 0.09; 'params': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'run,': 0.09; 'separately': 0.09; 'separating': 0.09; 'subject:files': 0.09; 'testing,': 0.09; 'true)': 0.09; 'wrapper': 0.09; 'def': 0.12; 'jan': 0.12; 'wrote': 0.14; '###': 0.16; "'0.1',": 0.16; "'__main__':": 0.16; '(relatively': 0.16; '0.1)': 0.16; 'chunks': 0.16; 'easier.': 0.16; 'func': 0.16; 'instantiated': 0.16; 'main().': 0.16; 'main():': 0.16; 'name):': 0.16; 'object()': 0.16; 'parameters,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'singleton': 0.16; 'slight': 0.16; 'threshold': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'bit': 0.19; 'module': 0.19; 'trying': 0.19; 'possible,': 0.19; 'skip:p 40': 0.19; 'work,': 0.20; 'import': 0.22; 'putting': 0.22; 'tests': 0.22; 'header:User-Agent:1': 0.23; 'driver': 0.24; 'either.': 0.24; 'module,': 0.24; 'replace': 0.24; 'decide': 0.24; 'fairly': 0.24; 'versions': 0.24; 'looks': 0.24; '(or': 0.24; 'question': 0.24; 'equivalent': 0.26; 'this:': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'testing': 0.29; 'raise': 0.29; 'needed.': 0.30; 'code': 0.31; 'easier': 0.31; 'lines': 0.31; 'assert': 0.31; 'easy,': 0.31; 'forces': 0.31; 'prints': 0.31; 'file': 0.32; 'class': 0.32; 'skip:m 30': 0.32; 'interface': 0.32; 'could': 0.34; 'except': 0.35; 'something': 0.35; 'case,': 0.35; 'operations': 0.35; 'test': 0.35; 'but': 0.35; 'add': 0.35; 'false': 0.36; 'right?': 0.36; 'yield': 0.36; 'possible': 0.36; 'should': 0.36; 'two': 0.37; 'easily': 0.37; 'skip:m 40': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'pm,': 0.38; 'that,': 0.38; 'anything': 0.39; 'does': 0.39; 'aside': 0.39; 'received:71': 0.39; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'above,': 0.60; 'results.': 0.60; 'new': 0.61; 'simple': 0.61; 'times': 0.62; 'subject:The': 0.64; 'become': 0.64; 'within': 0.65; 'close': 0.67; 'results': 0.69; 'inline': 0.74; 'score': 0.74; "'true'": 0.84; 'dict.': 0.84; 'mock': 0.84; 'received:fios.verizon.net': 0.84; 'results,': 0.84; 'numerous': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: The "right" way to use config files
Date Sat, 09 Aug 2014 18:30:17 -0400
References <ls51q0$4ea$1@speranza.aioe.org> <mailman.12797.1407605431.18130.python-list@python.org> <ls5odq$qn9$1@speranza.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-71-175-90-87.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <ls5odq$qn9$1@speranza.aioe.org>
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.12802.1407623446.18130.python-list@python.org> (permalink)
Lines 187
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1407623446 news.xs4all.nl 2965 [2001:888:2000:d::a6]:46805
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:75958

Show key headers only | View raw


On 8/9/2014 2:14 PM, Fabien wrote:
> 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

Try parser = object() to actually run, but the line is not needed. 
Instead put "parser: instantiated by initialize" in the docstring.
>
> 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()
> -----------------

The advantage of TDD is that it forces one to make code testable as you 
do. Old code may not be designed to be so easily testable, as I have 
learned trying to add tests to idlelib. For the above, I would consider

def func1_algo(threshhold, maxite):  # possible separte file
     score = 100.
     ite = 1
     while (score > threshold) and (ite < maxite):
         score /= 10
         ite += 1
     return score

def func1():  # interface wrapper
     threshold = myconfig.parser['func1'].as_float('threshold')
     maxite = myconfig.parser['func1'].as_long('maxite')
     return func1_algo(threshhold, maxite)

This is a slight bit of extra work, but now you can separately test (and 
modify) the algorithm and the interfacing.  Testing the algorithm is 
easy, which encourages testing multiple i/o pairs.

for in, out in iopairs:
   assert func1_algo(in) == out  # or self.assertEqual, or ...

(or close enough for float outputs)

As for the interfacing: you can write and read multiple versions of 
config.cfg (relatively slow), use something like unittest.mock to mock 
the myconfig module, or write something fairly simple (py3 code).

class Entry(dict):
     def as_bool(self, name):
         s = self[name]
         return True if s == 'True' else False if s == 'False' else None
     def as_int(self, name):
         return int(self[name])
     as_long = as_int
     def as_float(self, name):
         return float(self[name])

class Config(object):
     def initialize(self, argv):
         pass
myconfig = Config()  # a module is like a singleton class
myconfig.initialize('a')  # test that does not raise

# In use for testing, uncomment the following two lines
# import mainprogram_1.py as mp1
# mp1.myconfig = myconfig

f1_cfg = Entry({
     'enable': 'True',
     'threshold': '0.1',
     'maxite': '1',
     })
myconfig.parser = {'func1': f1_cfg}

print(myconfig.parser['func1'].as_float('threshold') == 0.1)
print(myconfig.parser['func1'].as_long('maxite') == 1)
print(myconfig.parser['func1'].as_bool('enable') == True)

f1_cfg['maxite'] = 5
print(myconfig.parser['func1'].as_int('maxite') == 5)
# prints True 4 times

Notice that you inject the mock myconfig into the tested module just 
one. After that, you can change anything within parser or replace parser 
with a new dict.

> Or like this:
>
> -----------------
> ### app/mainprogram_2.py
>
> import myconfig
>
> def func1(threshold=None, maxite=None):

These should not have defaults; avoid extra work!

>      # 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...

This is equivalent to what i wrote except for putting the wrapper inline 
in main().  Testing is the same for either.

-- 
Terry Jan Reedy

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