[3.12] gh-96765: Update ConfigParser.read() docs with multi-file read example (GH-121664) (GH-121688)

(cherry picked from commit fc21781175)

Co-authored-by: Timon Viola <44016238+timonviola@users.noreply.github.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
Miss Islington (bot) 2024-07-13 15:07:30 +02:00 committed by GitHub
parent 377ff9d584
commit e4c8d89186
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -147,23 +147,28 @@ case-insensitive and stored in lowercase [1]_.
It is possible to read several configurations into a single It is possible to read several configurations into a single
:class:`ConfigParser`, where the most recently added configuration has the :class:`ConfigParser`, where the most recently added configuration has the
highest priority. Any conflicting keys are taken from the more recent highest priority. Any conflicting keys are taken from the more recent
configuration while the previously existing keys are retained. configuration while the previously existing keys are retained. The example
below reads in an ``override.ini`` file, which will override any conflicting
keys from the ``example.ini`` file.
.. code-block:: ini
[DEFAULT]
ServerAliveInterval = -1
.. doctest:: .. doctest::
>>> another_config = configparser.ConfigParser() >>> config_override = configparser.ConfigParser()
>>> another_config.read('example.ini') >>> config_override['DEFAULT'] = {'ServerAliveInterval': '-1'}
['example.ini'] >>> with open('override.ini', 'w') as configfile:
>>> another_config['topsecret.server.example']['Port'] ... config_override.write(configfile)
'50022' ...
>>> another_config.read_string("[topsecret.server.example]\nPort=48484") >>> config_override = configparser.ConfigParser()
>>> another_config['topsecret.server.example']['Port'] >>> config_override.read(['example.ini', 'override.ini'])
'48484' ['example.ini', 'override.ini']
>>> another_config.read_dict({"topsecret.server.example": {"Port": 21212}}) >>> print(config_override.get('DEFAULT', 'ServerAliveInterval'))
>>> another_config['topsecret.server.example']['Port'] -1
'21212'
>>> another_config['topsecret.server.example']['ForwardX11']
'no'
This behaviour is equivalent to a :meth:`ConfigParser.read` call with several This behaviour is equivalent to a :meth:`ConfigParser.read` call with several
files passed to the *filenames* parameter. files passed to the *filenames* parameter.
@ -958,6 +963,31 @@ ConfigParser Objects
converter gets its own corresponding :meth:`!get*()` method on the parser converter gets its own corresponding :meth:`!get*()` method on the parser
object and section proxies. object and section proxies.
It is possible to read several configurations into a single
:class:`ConfigParser`, where the most recently added configuration has the
highest priority. Any conflicting keys are taken from the more recent
configuration while the previously existing keys are retained. The example
below reads in an ``override.ini`` file, which will override any conflicting
keys from the ``example.ini`` file.
.. code-block:: ini
[DEFAULT]
ServerAliveInterval = -1
.. doctest::
>>> config_override = configparser.ConfigParser()
>>> config_override['DEFAULT'] = {'ServerAliveInterval': '-1'}
>>> with open('override.ini', 'w') as configfile:
... config_override.write(configfile)
...
>>> config_override = configparser.ConfigParser()
>>> config_override.read(['example.ini', 'override.ini'])
['example.ini', 'override.ini']
>>> print(config_override.get('DEFAULT', 'ServerAliveInterval'))
-1
.. versionchanged:: 3.1 .. versionchanged:: 3.1
The default *dict_type* is :class:`collections.OrderedDict`. The default *dict_type* is :class:`collections.OrderedDict`.