Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > ConfigParser - setting the order of options in a section

Reply
Thread Tools

ConfigParser - setting the order of options in a section

 
 
S.Ramaswamy
Guest
Posts: n/a
 
      01-19-2004
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

-
 
Reply With Quote
 
 
 
 
Mark McEahern
Guest
Posts: n/a
 
      01-19-2004
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



 
Reply With Quote
 
 
 
 
Paramjit Oberoi
Guest
Posts: n/a
 
      01-19-2004
> 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.
 
Reply With Quote
 
Miki Tebeka
Guest
Posts: n/a
 
      01-19-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Case Sensitive Section names configparser RedBaron Python 2 12-08-2010 11:11 AM
Persuading ConfigParser to give me the section elements in the sameorder as the file geoffbache Python 2 09-11-2008 11:43 AM
ConfigParser: Can I read(ConfigParser.get()) a configuration file anduse it to call a funciton? jamitwidme@gmail.com Python 3 06-26-2008 08:01 PM
Default Section Values in ConfigParser mwt Python 3 03-01-2006 06:56 PM
ConfigParser lower-cases options automatically? Matthew Wilson Python 1 11-23-2003 06:56 PM



Advertisments