473,396 Members | 1,771 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.

Default Section Values in ConfigParser

mwt
I want to set default values for a ConfigParser. So far, its job is
very small, so there is only one section heading, ['Main']. Reading the
docs, I see that in order to set default values in a ConfigParser, you
initialize it with a dictionary or defaults. However, I'm not quite
sure of the syntax to add the section headings in to the dictionary of
defaults. For now, I'm doing it like this:

default_values = {'username' : 'put username here',
'teamnumber': 'team number here',
'update_interval' : 'update interval'}
self.INI = ConfigParser.ConfigParser(default_values)
self.INI.add_section('Main')

This works, but clearly won't last beyond the adding of a second
section. What is the correct way to initialize it with a full
dictionary of defaults, including section names?

Thanks.

Mar 1 '06 #1
3 11666
On 28 Feb 2006 17:05:32 -0800
"mwt" <mi*********@gmail.com> wrote:
I want to set default values for a ConfigParser. So far,
its job is very small, so there is only one section
heading, ['Main']. Reading the docs, I see that in order
to set default values in a ConfigParser, you initialize it
with a dictionary or defaults.


You know, I never noticed that, but there is another way.

I used a multi-line string constant with the same syntax
as the original file, and just fed that in as the first
source (this is particularly easy, so I think it must have
been intended). Slightly snipped version of my code for
this:

default_cfg = StringIO("""\
[VARIMAGE]
V1_COMPATIBILITY: ON

[SECURITY]
MAX_OPERATORS: 0
""")

# Look for varimage.cfg in 3 possible locations:
config = ConfigParser.ConfigParser()
config.readfp(default_cfg)
config.read(['/etc/varimage.cfg',
os.path.expandvars('${INSTANCE_HOME}/varimage.cfg'),
os.path.join(pkghome, 'varimage.cfg') ])
--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Mar 1 '06 #2
mwt
Thanks, Terry. That's an interesting way to go about it.

Mar 1 '06 #3

mwt wrote:
I want to set default values for a ConfigParser. So far, its job is
very small, so there is only one section heading, ['Main']. Reading the
docs, I see that in order to set default values in a ConfigParser, you
initialize it with a dictionary or defaults. However, I'm not quite
sure of the syntax to add the section headings in to the dictionary of
defaults. For now, I'm doing it like this:

default_values = {'username' : 'put username here',
'teamnumber': 'team number here',
'update_interval' : 'update interval'}
self.INI = ConfigParser.ConfigParser(default_values)
self.INI.add_section('Main')

This works, but clearly won't last beyond the adding of a second
section. What is the correct way to initialize it with a full
dictionary of defaults, including section names?

An alternative approach is to use `ConfigObj
<http://www.voidspace.org.uk/python/configobj.html>`_. It has two ways
of specifying default values.

The first way is to provide a configspec. This is a schema (that looks
very like a config file itself) that specifies the type (and paramater
bounds if you want) for each member. It can also include a default
value.

Simpler (although without the benefit of validation and type
conversion) is to use the ``merge`` method which is a recursive update.
(Although for config files that are a maximum of one section deep, the
dictionary method ``update`` will do the same job).

default_values = {'username' : 'put username here',
'teamnumber': 'team number here',
'update_interval' : 'update interval'}
user_values = ConfigObj(filename)
cfg = ConfigObj(default_values)
cfg.merge(user_values)

Note that for a config file with only a few values in it, ConfigObj
doesn't force you to use a section if it's not needed. To put your
default values into a 'Main' section you would actually do :

default_values = { 'Main': {'username' : 'put username here',
'teamnumber': 'team number here',
'update_interval' : 'update interval'}
}
#
user_values = ConfigObj(filename)
cfg = ConfigObj(default_values)
cfg.merge(user_values)

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Thanks.


Mar 1 '06 #4

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

Similar topics

3
by: Greg Krohn | last post by:
I'm trying to subclass ConfigParser so I can use a custom __read method (the custom format doesn't conform to RFC 822) when needed. Needless to say, it's not working as expected. In the...
2
by: Roy H. Berger | last post by:
If I want to subclass ConfigParser and changed the optionxform method to not return things in lower case wouldn't I just need the following code in my subclasss module? from ConfigParser import...
3
by: S.Ramaswamy | last post by:
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...
2
by: rzed | last post by:
I am working with PythonCard in one of my apps. For its purposes, it uses an .ini file that is passed to ConfigParser. For my app, I also need configuration information, but for various reasons,...
10
by: Terry Carroll | last post by:
It looks like ConfigParser will accept a list to be writing to the *.ini file; but when reading it back in, it treats it as a string. Example: ############################### import...
4
by: Danil Dotsenko | last post by:
Wrote a little "user-friedly" wrapper for ConfigParser for a KDE's SuperKaramba widget. (http://www.kde-look.org/content/show.php?content=32185) I was using 2.4.x python docs as reference and...
0
by: Phoe6 | last post by:
Hi All, I am able to use urlib2 through proxy. I give proxy credentials and use # Set the Proxy Address proxy_ip = "10.0.1.1:80" proxy_user = 'senthil_or' proxy_password_orig='password'
4
by: Phoe6 | last post by:
Hi, I have a configfile, in fact, I am providing a configfile in the format: Name: Foo Author: Bar Testcases: tct123
4
by: Hamish Moffatt | last post by:
SafeConfigParser is supposed to be safer than ConfigParser, but calling set with a string value containing '%' generates exceptions when you get() it back. Python 2.5.1 (r251:54863, Apr 25 2007,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.