mirror of
https://github.com/python/cpython.git
synced 2025-09-29 11:45:57 +00:00
The example for configparser was weird.
This commit is contained in:
parent
6f0d59bad3
commit
02dd70be5c
1 changed files with 29 additions and 22 deletions
|
@ -1454,34 +1454,41 @@ duplicates are not allowed in a single configuration source.
|
||||||
|
|
||||||
Config parsers gained a new API based on the mapping protocol::
|
Config parsers gained a new API based on the mapping protocol::
|
||||||
|
|
||||||
>>> parser = ConfigParser()
|
>>> parser = ConfigParser()
|
||||||
>>> parser.read_string("""
|
>>> parser.read_string("""
|
||||||
[DEFAULT]
|
[DEFAULT]
|
||||||
monty = python
|
location = upper left
|
||||||
|
visible = yes
|
||||||
|
editable = no
|
||||||
|
color = blue
|
||||||
|
|
||||||
[phrases]
|
[main]
|
||||||
the = who
|
title = Main Menu
|
||||||
full = metal jacket
|
color = green
|
||||||
""")
|
|
||||||
>>> parser['phrases']['full']
|
|
||||||
'metal jacket'
|
|
||||||
>>> section = parser['phrases']
|
|
||||||
>>> section['the']
|
|
||||||
'who'
|
|
||||||
>>> section['british'] = '%(the)s %(full)s %(monty)s!'
|
|
||||||
>>> parser['phrases']['british']
|
|
||||||
'who metal jacket python!'
|
|
||||||
>>> 'british' in section
|
|
||||||
True
|
|
||||||
|
|
||||||
The new API is implemented on top of the classical API so custom parser
|
[options]
|
||||||
|
title = Options
|
||||||
|
""")
|
||||||
|
>>> parser['main']['color']
|
||||||
|
'green'
|
||||||
|
>>> parser['main']['editable']
|
||||||
|
'no'
|
||||||
|
>>> section = parser['options']
|
||||||
|
>>> section['title']
|
||||||
|
'Options'
|
||||||
|
>>> section['title'] = 'Options (editable: %(editable)s)'
|
||||||
|
>>> section['title']
|
||||||
|
'Options (editable: no)'
|
||||||
|
|
||||||
|
The new API is implemented on top of the classical API, so custom parser
|
||||||
subclasses should be able to use it without modifications.
|
subclasses should be able to use it without modifications.
|
||||||
|
|
||||||
The INI file structure accepted by config parsers can now be customized. Users
|
The INI file structure accepted by config parsers can now be customized. Users
|
||||||
can specify alternative option/value delimiters and comment prefixes, change the
|
can specify alternative option/value delimiters and comment prefixes, change the
|
||||||
name of the *DEFAULT* section or switch the interpolation syntax. Along with
|
name of the *DEFAULT* section or switch the interpolation syntax.
|
||||||
support for pluggable interpolation, an additional interpolation handler
|
|
||||||
:class:`~configparser.ExtendedInterpolation` was introduced::
|
The is support for pluggable interpolation including an additional interpolation
|
||||||
|
handler :class:`~configparser.ExtendedInterpolation`::
|
||||||
|
|
||||||
>>> parser = ConfigParser(interpolation=ExtendedInterpolation())
|
>>> parser = ConfigParser(interpolation=ExtendedInterpolation())
|
||||||
>>> parser.read_dict({'buildout': {'directory': '/home/ambv/zope9'},
|
>>> parser.read_dict({'buildout': {'directory': '/home/ambv/zope9'},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue