ConfigParser - setting the order of options in a section

S

S.Ramaswamy

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

-
 
M

Mark McEahern

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
 
P

Paramjit Oberoi

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.
 
M

Miki Tebeka

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top