Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Default Section Values in ConfigParser

Reply
Thread Tools

Default Section Values in ConfigParser

 
 
mwt
Guest
Posts: n/a
 
      03-01-2006
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.

 
Reply With Quote
 
 
 
 
Terry Hancock
Guest
Posts: n/a
 
      03-01-2006
On 28 Feb 2006 17:05:32 -0800
"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.


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 ()
Anansi Spaceworks http://www.AnansiSpaceworks.com

 
Reply With Quote
 
 
 
 
mwt
Guest
Posts: n/a
 
      03-01-2006
Thanks, Terry. That's an interesting way to go about it.

 
Reply With Quote
 
Fuzzyman
Guest
Posts: n/a
 
      03-01-2006

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.


 
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
newbie question on Python tutorial example in section 4.7.1 (default arg values) Porky Pig Jr Python 5 05-03-2004 10:15 PM
ConfigParser - setting the order of options in a section S.Ramaswamy Python 3 01-19-2004 10:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57