ConfigParser - setting the order of options in a section

Discussion in 'Python' started by S.Ramaswamy, Jan 19, 2004.

  1. S.Ramaswamy

    S.Ramaswamy Guest

    I am trying unsuccessfully to set the order of options using the
    set(section,option,value) method ( Python 2.2.2) and writing to a
    file. But the options always appear in a random order. Before each
    option I am writing a comment line using set(section,"#",value) - a
    one line explanation for the option that follows - but the comments
    get re-ordered randomly.

    ------------------------------------------------------------------------------------
    cfg.add_section("COOL")
    cfg.set("COOL","#","This is option A001")
    cfg.set("COOL","A001","1")
    cfg.set("COOL","#","This is option B001")
    cfg.set("COOL","B001","2")
    cfg.set("COOL","#","This is option C001")
    cfg.set("COOL","C001","3")
    cfg.write(file)
    -------------------------------------------------------------------------------------


    The comments for the options get jumbled up - I want them to be
    written before the option=value line. Instead the comments for options
    A001,B001,C001 all get ordered randomly.

    Any help in getting the sections to be written in order will be
    appreciated.

    srsy

    -
    S.Ramaswamy, Jan 19, 2004
    #1
    1. Advertising

  2. On Mon, 2004-01-19 at 09:24, S.Ramaswamy wrote:
    > I am trying unsuccessfully to set the order of options [...]


    Sections and options are internally stored by ConfigParser in
    dictionaries. I think the idea is that, as far as ConfigParser is
    concerned, the order of sections and options within a section is
    unimportant. So if it *is* important to you, you're going to have to do
    the ordering yourself.

    Also, in your code, you seem to treat comments as if they were options,
    setting them with set(). As far as I can tell, ConfigParser doesn't
    provide anyway for you to associate a comment with an option.

    What I'd do is provide your own write function or writer object that
    does what you want it to do.

    Cheers,

    // m
    Mark McEahern, Jan 19, 2004
    #2
    1. Advertising

  3. > I am trying unsuccessfully to set the order of options using the
    > set(section,option,value) method ( Python 2.2.2) and writing to a
    > file. But the options always appear in a random order. Before each
    > option I am writing a comment line using set(section,"#",value) - a
    > one line explanation for the option that follows - but the comments
    > get re-ordered randomly.


    There is a module which was originally written for the SpamBayes
    project which probably does what you want. Search Google Groups
    for 'UpdatableConfigParser' for details.
    Paramjit Oberoi, Jan 19, 2004
    #3
  4. S.Ramaswamy

    Miki Tebeka Guest

    Hello,

    > I am trying unsuccessfully to set the order of options using the
    > set(section,option,value) method ( Python 2.2.2) and writing to a
    > file. But the options always appear in a random order.

    I had the same problem. Just hand written the file myself, the syntax
    of the .cfg files is *very* simple and by iterating over the
    sections/values it's very easy to generate the "right" output file.

    HTH.
    Miki
    Miki Tebeka, Jan 19, 2004
    #4
    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. Matthew Wilson
    Replies:
    1
    Views:
    869
    Jarek Zgoda
    Nov 23, 2003
  2. mwt
    Replies:
    3
    Views:
    425
    Fuzzyman
    Mar 1, 2006
  3. Replies:
    3
    Views:
    429
  4. geoffbache
    Replies:
    2
    Views:
    393
    geoffbache
    Sep 11, 2008
  5. RedBaron
    Replies:
    2
    Views:
    343
    Jon Clements
    Dec 8, 2010
Loading...

Share This Page