[Python-Dev] ConfigParser shootout, preliminary entry

Paul McGuire ptmcg at austin.rr._bogus_.com
Thu Oct 21 05:19:35 CEST 2004


The pyparsing examples directory includes a simple configParse.py config
file parser.  The results are returned as a ParseResults object, which can
be accessed as a list, a dictionary, or an object with attributes, and can
be exported to XML.

I don't propose that configParse is a full-blown config file parser
contender, but I thought the API might offer some other ideas to those of
you considering the ultimate configuration file module.

(BTW, my vote is -10 for any config file format that requires definition of
classes, functions, or other language constructs more complex than
assignment or arithmetic/string expressions.  Config files should be simple
text files for easy reading and editing.)

-- Paul


For example, with this input file:

[users]
source_dir = '/home/karthik/Projects/python'
data_dir   = '/home/karthik/Projects/data'
result_dir = '/home/karthik/Projects/Results'
param_file = $result_dir/param_file
res_file   = $result_dir/result_file
comment    = 'this is a comment'
; a line starting with ';' is a comment
K = 8
simulate_K = 0
N = 4000
mod_scheme = 'QPSK'
Na = K+2

If parsed with configParse.py, the results (returned in a variable, say,
"config") can be accessed as:

    print "config.users :", config.users
    print "config.users.keys() :", config.users.keys()
    print "config.users.N :", config.users.N
    print "config.users['N'] :",config.users['N']
    print "len(config.users) :", len(config.users)
    print "config.users[0] :", config.users[0]
    print "config.asXML('config') :", config.asXML('config')

prints:

config.users : [['source_dir', "'/home/karthik/Projects/python'"],
['data_dir', "'/home/karthik/Projects/data'"], ['result_dir',
"'/home/karthik/Projects/Results'"], ['param_file',
'$result_dir/param_file'], ['res_file', '$result_dir/result_file'],
['comment', "'this is a comment'"], ['K', '8'], ['simulate_K', '0'], ['N',
'4000'], ['mod_scheme', "'QPSK'"], ['Na', 'K+2']]
config.users.keys() : ['comment', 'data_dir', 'mod_scheme', 'Na', 'K',
'simulate_K', 'N', 'res_file', 'result_dir', 'source_dir', 'param_file']
config.users.N : 4000
config.users['N'] : 4000
len(config.users) : 11
config.users[0] : ['source_dir', "'/home/karthik/Projects/python'"]
config.asXML('config') :
<config>
  <users>
    <ITEM>users</ITEM>
    <source_dir>
      <ITEM>source_dir</ITEM>
      <ITEM>'/home/karthik/Projects/python'</ITEM>
    </source_dir>
    <data_dir>
      <ITEM>data_dir</ITEM>
      <ITEM>'/home/karthik/Projects/data'</ITEM>
    </data_dir>
    <result_dir>
      <ITEM>result_dir</ITEM>
      <ITEM>'/home/karthik/Projects/Results'</ITEM>
    </result_dir>
    <param_file>
      <ITEM>param_file</ITEM>
      <ITEM>$result_dir/param_file</ITEM>
    </param_file>
    <res_file>
      <ITEM>res_file</ITEM>
      <ITEM>$result_dir/result_file</ITEM>
    </res_file>
    <comment>
      <ITEM>comment</ITEM>
      <ITEM>'this is a comment'</ITEM>
    </comment>
    <K>
      <ITEM>K</ITEM>
      <ITEM>8</ITEM>
    </K>
    <simulate_K>
      <ITEM>simulate_K</ITEM>
      <ITEM>0</ITEM>
    </simulate_K>
    <N>
      <ITEM>N</ITEM>
      <ITEM>4000</ITEM>
    </N>
    <mod_scheme>
      <ITEM>mod_scheme</ITEM>
      <ITEM>'QPSK'</ITEM>
    </mod_scheme>
    <Na>
      <ITEM>Na</ITEM>
      <ITEM>K+2</ITEM>
    </Na>
  </users>
</config>







More information about the Python-list mailing list