summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorTimon Viola <44016238+timonviola@users.noreply.github.com>2024-07-13 12:47:05 (GMT)
committerGitHub <noreply@github.com>2024-07-13 12:47:05 (GMT)
commitfc2178117538c161471711073887f34bcd464cc1 (patch)
tree50b72cd200e2eb7cb808be53f94ec1907637aa6b /Doc/library
parent4b9e10d0ea352592049c1f2a00318d7274143fa4 (diff)
downloadcpython-fc2178117538c161471711073887f34bcd464cc1.zip
cpython-fc2178117538c161471711073887f34bcd464cc1.tar.gz
cpython-fc2178117538c161471711073887f34bcd464cc1.tar.bz2
gh-96765: Update ConfigParser.read() docs with multi-file read example (#121664)
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/configparser.rst58
1 files changed, 44 insertions, 14 deletions
diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst
index e84fb51..6fae03c 100644
--- a/Doc/library/configparser.rst
+++ b/Doc/library/configparser.rst
@@ -147,23 +147,28 @@ case-insensitive and stored in lowercase [1]_.
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.
+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::
- >>> another_config = configparser.ConfigParser()
- >>> another_config.read('example.ini')
- ['example.ini']
- >>> another_config['topsecret.server.example']['Port']
- '50022'
- >>> another_config.read_string("[topsecret.server.example]\nPort=48484")
- >>> another_config['topsecret.server.example']['Port']
- '48484'
- >>> another_config.read_dict({"topsecret.server.example": {"Port": 21212}})
- >>> another_config['topsecret.server.example']['Port']
- '21212'
- >>> another_config['topsecret.server.example']['ForwardX11']
- 'no'
+ >>> 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
+
This behaviour is equivalent to a :meth:`ConfigParser.read` call with several
files passed to the *filenames* parameter.
@@ -984,6 +989,31 @@ ConfigParser Objects
converter gets its own corresponding :meth:`!get*()` method on the parser
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
The default *dict_type* is :class:`collections.OrderedDict`.