473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ConfigParser - setting the order of options in a section

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

-
Jul 18 '05 #1
3 3580
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

Jul 18 '05 #2
> 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.
Jul 18 '05 #3
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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin Maney | last post by:
Summary: with batteries like these, get a longer extension cord? <transcript from interactive session> Python 2.2.1 (#1, Sep 7 2002, 14:34:30) on linux2 Type "help", "copyright",...
1
by: David Rysdam | last post by:
I have a file test.cfg: option1=foo option2=bar I have a program testconfig.py: import ConfigParser
1
by: Robin Munn | last post by:
I've been loving SQLObject. The ability to set up a database connection and then completely *forget* about it and just manipulate Python objects has been great. But I'm running into a problem, and...
3
by: Sergey | last post by:
Is there an alternative to standard module ConfigParser, which can use delimitier symbol other than ":" and "=", preferaby just space? I need to parse such configs: 2:5020/758 xxxx 2:5020/794...
3
by: mwt | last post by:
I want to set default values for a ConfigParser. So far, its job is very small, so there is only one section heading, . Reading the docs, I see that in order to set default values in a...
1
by: Alexandre CONRAD | last post by:
Hello list ! I'm using the ConfigParser module to use configuration files (what else would it be for ?). But I have a dilema: I'd like to setup multiple "update server" for my application with...
12
by: Florian Lindner | last post by:
Hello, since ConfigParser does not seem to support multiple times the same option name, like: dir="/home/florian" dir="/home/john" dir="/home/whoever" (only the last one is read in)
1
by: pipehappy | last post by:
Hello everyone: I came across the module ConfigParser and can use it correctly. import ConfigParser fp = open('test.cfg','w+') config = ConfigParser.ConfigParser() config.readfp(fp)...
3
by: derf912 | last post by:
I am working with ConfigParser in python and the ini file that I am reading in has a section titled and under that section I have multiple options. All the options have the same name "customer =" ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.