6.2.2 ConfigSection Objects
- class ConfigSection(name, parent )
For the most part, a ConfigSection is a dict of ConfigOptions. However, when we access section["key"], it doesn’t return the ConfigOption itself, but the value of the option.
The rationale for this setup is that we want to think of a ConfigOption as the value of the option together with some metadata. When trying to read or write the config option, what we want to do is the read or write to the value and ignoring the metadata.
The only exception is when we want to add a new config option to the section, where we would write
section["new_key"] = ConfigOption(...)
name is the name of the section.
parent is the ConfigManager that contains this section.
- data
dictionary that contains the option instances. This is only accessed if you want to retrieve the real option instances. Normally, you would use standard dictionary key access syntax on the section itself to retrieve the option values.
- __getitem__(key )
retrieve the value of an option, raising a KeyError if the option does not exist. This method allows you to use Python’s dictionary syntax on a section as shown below.
# Print the value of the 'optionname' option print(mysection['optionname'])
- __setitem__(key, value )
create a new option or set an existing option with the name key and the value of value . This method allows you to use Python’s dictionary syntax to set options as shown below.
# Create a new option called 'optionname' mysection['optionname'] = IntegerOption("Test", --option="--test", default=0) mysection['optionname'] = 10