4.1 set method

The set() method of the object returned from the add_option() method of the ConfigParser class can be used to modify an option setting in a configuration file.

set( value,[, cfgfile][, keys])

value is a required positional argument and is the new option value.

cfgfile is an optional keyword argument. If omitted or set to None the configuration file will be found. Otherwise the object returned by the add_file() method may be passed in to modify or add a setting to a specific file.

keys is an optional string keyword argument. Use of this argument is dependent on the cfgfile argument and is discussed further below.

If cfgfile argument is omitted or set to None the keys argument will be used to first get the current option setting. The keys are used in the same way as they are in the get() method. If a setting cannot be found in any of the configuration files added, OptionNotFound will be raised. If the setting is found it will be modified and the originating (modified) file object will be returned. Note, if the option is defined in multiple locations, this method will only modify the one found.

If cfgfile argument is set, its set_option() method is called using value. The name, help, and keys arguments of the add_option() method that created the object set() was invoked on are passed on to set_option(). If keys of the set() method was not omitted those keys are used instead.

For example:

[DEFAULT]
# this section applies to all devices
timeout = 10 # in seconds
retries = 3

[DEV0]
# this section if for settings specific to device #0
retries = 5 # overrides default

And script:

import cfgparse,sys
c = cfgparse.ConfigParser()
f = c.add_file('set.ini')
r = c.add_option('retries',type='int',keys='DEV0')
t = c.add_option('timeout',type='int',keys='DEV0',
                 help='Time between retries in seconds.')
r.set(7)
t.set(20)
f.write(sys.stdout)

Results in:

$ python set1.py
[DEFAULT]
# this section applies to all devices
timeout = 20 # in seconds
retries = 3

[DEV0]
# this section if for settings specific to device #0
retries = 7 # overrides default

The timeout option in the [DEFAULT] section was modified. This is due to cfgfile not being specified. The option setting was located using the same methodology as the get() method and that option was modified. If this behavior is not desired, use the cfgfile argument.

For example:

import cfgparse,sys
c = cfgparse.ConfigParser()
f = c.add_file('set.ini')
r = c.add_option('retries',type='int',keys='DEV1',
                 help='Number of times to try again.')
t = c.add_option('timeout',type='int',keys='DEV0',
                 help='Time between retries in seconds.')
r.set(7,f)
t.set(20,f)
f.write(sys.stdout)

Results in:

$ python set2.py
[DEFAULT]
# this section applies to all devices
timeout = 10 # in seconds
retries = 3

[DEV0]
# this section if for settings specific to device #0
retries = 5 # overrides default

# Time between retries in seconds.
timeout = 20

[DEV1]

# Number of times to try again.
retries = 7

The above example also demonstrates the creation of new sections as necessary by modifying the retries option with the DEV1 key.