best way to read a configuration file

Discussion in 'Python' started by Karthikesh Raju, Mar 3, 2004.

  1. Hi All,

    i am wondering about the best way to read in a configuration file that
    goes like:

    ###########

    [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

    ######################

    As of now i use config parser and i get this in a dictionary but

    a) but i have users.na and not users.Na (all the fields are in lower
    case)

    b) all the rhs arguements are string, but i have handled this by
    trying " try eval(rhs) ... except ..." block

    c) Na = 'K+2' though does not work, would like to have Na = 10, but i
    get users.na = 'K+2'

    d) result_file, param_file should actually be should be with pathname
    extensions filled in.

    Have looked in c.l.py none one ofthe suggestions was to use
    splitlines, this cant handle blank lines, other was to have config.py
    ...

    Hope to get some solution for this ..

    with warm regards

    karthik


    --

    -----------------------------------------------------------------------
    Karthikesh Raju, email:
    Researcher, http://www.cis.hut.fi/karthik
    Helsinki University of Technology, Tel: +358-9-451 5389
    Laboratory of Comp. & Info. Sc., Fax: +358-9-451 3277
    Department of Computer Sc.,
    P.O Box 5400, FIN 02015 HUT,
    Espoo, FINLAND
    -----------------------------------------------------------------------
    Karthikesh Raju, Mar 3, 2004
    #1
    1. Advertising

  2. Karthikesh Raju

    Paul McGuire Guest

    "Karthikesh Raju" <> wrote in message
    news:...
    >
    > Hi All,
    >
    > i am wondering about the best way to read in a configuration file that
    > goes like:
    >
    > ###########
    >
    > [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
    >
    > ######################
    >
    > As of now i use config parser and i get this in a dictionary but
    >
    > a) but i have users.na and not users.Na (all the fields are in lower
    > case)
    >
    > b) all the rhs arguements are string, but i have handled this by
    > trying " try eval(rhs) ... except ..." block
    >
    > c) Na = 'K+2' though does not work, would like to have Na = 10, but i
    > get users.na = 'K+2'
    >
    > d) result_file, param_file should actually be should be with pathname
    > extensions filled in.
    >
    > Have looked in c.l.py none one ofthe suggestions was to use
    > splitlines, this cant handle blank lines, other was to have config.py
    > ..
    >
    > Hope to get some solution for this ..
    >
    > with warm regards
    >
    > karthik
    >
    >
    > --
    >
    > -----------------------------------------------------------------------
    > Karthikesh Raju, email:
    > Researcher, http://www.cis.hut.fi/karthik
    > Helsinki University of Technology, Tel: +358-9-451 5389
    > Laboratory of Comp. & Info. Sc., Fax: +358-9-451 3277
    > Department of Computer Sc.,
    > P.O Box 5400, FIN 02015 HUT,
    > Espoo, FINLAND
    > -----------------------------------------------------------------------
    Paul McGuire, Mar 3, 2004
    #2
    1. Advertising

  3. Karthikesh Raju

    Paul McGuire Guest

    "Karthikesh Raju" <> wrote in message
    news:...
    >
    > Hi All,
    >
    > i am wondering about the best way to read in a configuration file that
    > goes like:
    >
    > ###########
    >
    > [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
    >
    > ######################
    >
    > As of now i use config parser and i get this in a dictionary but
    >
    > a) but i have users.na and not users.Na (all the fields are in lower
    > case)
    >
    > b) all the rhs arguements are string, but i have handled this by
    > trying " try eval(rhs) ... except ..." block
    >
    > c) Na = 'K+2' though does not work, would like to have Na = 10, but i
    > get users.na = 'K+2'
    >
    > d) result_file, param_file should actually be should be with pathname
    > extensions filled in.
    >
    > Have looked in c.l.py none one ofthe suggestions was to use
    > splitlines, this cant handle blank lines, other was to have config.py
    > ..
    >
    > Hope to get some solution for this ..
    >
    > with warm regards
    >
    > karthik
    >
    >
    > --
    >
    > -----------------------------------------------------------------------
    > Karthikesh Raju, email:
    > Researcher, http://www.cis.hut.fi/karthik
    > Helsinki University of Technology, Tel: +358-9-451 5389
    > Laboratory of Comp. & Info. Sc., Fax: +358-9-451 3277
    > Department of Computer Sc.,
    > P.O Box 5400, FIN 02015 HUT,
    > Espoo, FINLAND
    > -----------------------------------------------------------------------


    I modified the config parser that is included as an example that ships with
    pyparsing, by adding:

    iniLines = "\n".join( file("karthik.ini").readlines() )
    config = inifile_BNF().parseString(iniLines)
    pprint.pprint( config.asList() )
    for k in config.users.keys():
    print k,":",config.users[k]

    This gives the following output:

    [['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']]]
    comment : 'this is a comment'
    data_dir : '/home/karthik/Projects/data'
    mod_scheme : 'QPSK'
    Na : K+2
    K : 8
    simulate_K : 0
    N : 4000
    res_file : $result_dir/result_file
    result_dir : '/home/karthik/Projects/Results'
    source_dir : '/home/karthik/Projects/python'
    param_file : $result_dir/param_file

    This actually shows the 3 access modes to the results from a pyparsing
    parseString() operation:
    - as a raw list of tokens (optionally grouped into sublists, giving a parse
    tree)
    - as an object with attributes (note reference to 'config.users', also
    notice that keys are not converted to lower case)
    - as a dictionary (access to config.users[k] for each key k)

    The 2nd level keys could also be referenced using the form
    config.users.data_dir, config.users.comment, etc.

    pyparsing also comes with an expression parser and evaluator in its
    examples. With some creative merging of the two, I think you could in
    fairly short order have a config parser that would handle your Na = K+2 and
    res_file = "$result_dir..." config values.

    The pyparsing home page is at http://pyparsing.sourceforge.net.

    HTH,
    -- Paul
    Paul McGuire, Mar 3, 2004
    #3
  4. Karthikesh Raju

    Paul McGuire Guest

    Sorry about that last null post, my fingers tripped over the send button
    before I started typing my response.

    -- Paul
    Paul McGuire, Mar 3, 2004
    #4
  5. Thankx paul, will have to download pyparsing, will do in a short will
    and try your trick, hope it works well :)

    With warm regards

    karthik
    --

    -----------------------------------------------------------------------
    Karthikesh Raju, email:
    Researcher, http://www.cis.hut.fi/karthik
    Helsinki University of Technology, Tel: +358-9-451 5389
    Laboratory of Comp. & Info. Sc., Fax: +358-9-451 3277
    Department of Computer Sc.,
    P.O Box 5400, FIN 02015 HUT,
    Espoo, FINLAND
    -----------------------------------------------------------------------
    Karthikesh Raju, Mar 3, 2004
    #5
  6. I kept this sample from an ealier thread or website. I don't have the
    link nor can I give credit to the author. If you have some lattitude
    on the exact format of the config file, this may do the trick for you
    though:

    Python App Macro Lang


    class App:
    def __init__(self, name):
    self._name = name
    def name(self):
    return self._name

    i=123

    macrosource = """
    print app.name()
    i=1
    if i==2:
    server="internal.blah.com"
    else:
    server="external.blah.com"

    """

    code = compile(macrosource, '<string>', "exec")
    anApp = App('Global App Object')
    context = {}

    # populate context with fun things
    context["app"] = anApp
    exec code in context

    print "The server is:" + context["server"]
    if i != 123:
    raise "local variable 'i' changed"

    output = """
    c:\cygwin\bin\sh -c "python PyAsMacroLang.py"
    Global App Object
    The server is:external.blah.com
    """
    Kevin Dahlhausen, Mar 3, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ESPN Lover

    Which way is best to read from a file?

    ESPN Lover, Oct 12, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    373
    ESPN Lover
    Oct 13, 2004
  2. Alan Silver
    Replies:
    8
    Views:
    3,913
    Scott Allen
    Dec 8, 2005
  3. Replies:
    4
    Views:
    372
  4. Allan Ebdrup
    Replies:
    5
    Views:
    442
    Steven Cheng[MSFT]
    Apr 4, 2007
  5. Bret Schuhmacher

    Best way to read an XML properties file?

    Bret Schuhmacher, Jul 10, 2007, in forum: Java
    Replies:
    1
    Views:
    661
    Stefan Ram
    Jul 10, 2007
Loading...

Share This Page