8000 [3.12] gh-96765: Update ConfigParser.read() docs with multi-file read… · python/cpython@e4c8d89 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit e4c8d89

Browse files
miss-islingtontimonviolaambv
authored
[3.12] gh-96765: Update ConfigParser.read() docs with multi-file read example (GH-121664) (GH-121688)
(cherry picked from commit fc21781) Co-authored-by: Timon Viola <44016238+timonviola@users.noreply.github.com> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
1 parent 377ff9d commit e4c8d89

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

Doc/library/configparser.rst

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,28 @@ case-insensitive and stored in lowercase [1]_.
147147
It is possible to read several configurations into a single
148148
:class:`ConfigParser`, where the most recently added configuration has the
149149
highest priority. Any conflicting keys are taken from the more recent
150-
configuration while the previously existing keys are retained.
150+
configuration while the previously existing keys are retained. The example
151+
below reads in an ``override.ini`` file, which will override any conflicting
152+
keys from the ``example.ini`` file.
153+
154+
.. code-block:: ini
155+
156+
[DEFAULT]
157+
ServerAliveInterval = -1
151158
152159
.. doctest::
153160

154-
>>> another_config = configparser.ConfigParser()
155-
>>> another_config.read('example.ini')
156-
['example.ini']
157-
>>> another_config['topsecret.server.example']['Port']
158-
'50022'
159-
>>> another_config.read_string("[topsecret.server.example]\nPort=48484")
160-
>>> another_config['topsecret.server.example']['Port']
161-
'48484'
162-
>>> another_config.read_dict({"topsecret.server.example": {"Port": 21212}})
163-
>>> another_config['topsecret.server.example']['Port']
164-
'21212'
165-
>>> another_config['topsecret.server.example']['ForwardX11']
166-
'no'
161+
>>> config_override = configparser.ConfigParser()
162+
>>> config_override['DEFAULT'] = {'ServerAliveInterval': '-1'}
163+
>>> with open('override.ini', 'w') as configfile:
164+
... config_override.write(configfile)
165+
...
166+
>>> config_override = configparser.ConfigParser()
167+
>>> config_override.read(['example.ini', 'override.ini'])
168+
['example.ini', 'override.ini']
169+
>>> print(config_override.get('DEFAULT', 'ServerAliveInterval'))
170+
-1
171+
167172

168173
This behaviour is equivalent to a :meth:`ConfigParser.read` call with several
169174
files passed to the *filenames* parameter.
@@ -958,6 +963,31 @@ ConfigParser Objects
958963
converter gets its own corresponding :meth:`!get*()` method on the parser
959964
object and section proxies.
960965

966+
It is possible to read several configurations into a single
967+
:class:`ConfigParser`, where the most recently added configuration has the
968+
highest priority. Any conflicting keys are taken from the more recent
969+
configuration while the previously existing keys are retained. The example
970+
below reads in an ``override.ini`` file, which will override any conflicting
971+
keys from the ``example.ini`` file.
972+
973+
.. code-block:: ini
974+
975+
[DEFAULT]
976+
ServerAliveInterval = -1
977+
978+
.. doctest::
979+
980+
>>> config_override = configparser.ConfigParser()
981+
>>> config_override['DEFAULT'] = {'ServerAliveInterval': '-1'}
982+
>>> with open('override.ini', 'w') as configfile:
983+
... config_override.write(configfile)
984+
...
985+
>>> config_override = configparser.ConfigParser()
986+
>>> config_override.read(['example.ini', 'override.ini'])
987+
['example.ini', 'override.ini']
988+
>>> print(config_override.get('DEFAULT', 'ServerAliveInterval'))
989+
-1
990+
961991
.. versionchanged:: 3.1
962992
The default *dict_type* is :class:`collections.OrderedDict`.
963993

0 commit comments

Comments
 (0)
0