By default, validate() performs type conversion. If a check like integer succeeds, the value in the ConfigObj instance is converted to a Python integer. When writing to a file, these values are converted back to strings using str().
Controlling Type Conversion
To prevent validation from changing the type of the values (leaving them as strings), set the stringify_ attribute to False. Note that if stringify_ is False, attempting to set a value to a non-string will raise an error.
Setting Default Values
You can specify a default value in your configspec. If a value is missing from the config file, the default is used.
- If no default is provided, a missing value results in an error returned by
validate(). - To implement optional values, set
default=None. This sets the value to the Python None object (or '' if stringify_ is False). - Note: Default values must pass the specified check.
List Defaults
To use a list as a default value, use the list() constructor to avoid syntax confusion with commas and quotes:
checkname(default=list('val1', 'val2', 'val3'))
# Example configspec definitions
port = integer(0, 100, default=80)
user = string(max=25, default=0)
mode = option('quiet', 'loud', 'silent', default='loud')
nick = string(default=None)
# List as a default
items = string(default=list('item1', 'item2'))