Default Section Values in ConfigParser

Discussion in 'Python' started by mwt, Mar 1, 2006.

  1. mwt

    mwt Guest

    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.
    mwt, Mar 1, 2006
    #1
    1. Advertising

  2. 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
    Terry Hancock, Mar 1, 2006
    #2
    1. Advertising

  3. mwt

    mwt Guest

    Thanks, Terry. That's an interesting way to go about it.
    mwt, Mar 1, 2006
    #3
  4. mwt

    Fuzzyman Guest

    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.
    Fuzzyman, Mar 1, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S.Ramaswamy
    Replies:
    3
    Views:
    489
    Miki Tebeka
    Jan 19, 2004
  2. Porky Pig Jr
    Replies:
    5
    Views:
    407
    Porky Pig Jr
    May 3, 2004
  3. Replies:
    3
    Views:
    407
  4. geoffbache
    Replies:
    2
    Views:
    381
    geoffbache
    Sep 11, 2008
  5. RedBaron
    Replies:
    2
    Views:
    327
    Jon Clements
    Dec 8, 2010
Loading...

Share This Page