[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:

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')


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') :
      <ITEM>'this is a comment'</ITEM>

More information about the Python-list mailing list