Changeset 391 for python/trunk/Doc/library/configparser.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/configparser.rst
r2 r391 13 13 14 14 The :mod:`ConfigParser` module has been renamed to :mod:`configparser` in 15 Python 3. 0.The :term:`2to3` tool will automatically adapt imports when16 converting your sources to 3.0.15 Python 3. The :term:`2to3` tool will automatically adapt imports when 16 converting your sources to Python 3. 17 17 18 18 .. index:: … … 32 32 This library does *not* interpret or write the value-type prefixes used in 33 33 the Windows Registry extended version of INI syntax. 34 35 .. seealso:: 36 37 Module :mod:`shlex` 38 Support for a creating Unix shell-like mini-languages which can be used 39 as an alternate format for application configuration files. 40 41 Module :mod:`json` 42 The json module implements a subset of JavaScript syntax which can also 43 be used for this purpose. 34 44 35 45 The configuration file consists of sections, led by a ``[section]`` header and … … 42 52 ``';'`` are ignored and may be used to provide comments. 43 53 54 Configuration files may include comments, prefixed by specific characters (``#`` 55 and ``;``). Comments may appear on their own in an otherwise empty line, or may 56 be entered in lines holding values or section names. In the latter case, they 57 need to be preceded by a whitespace character to be recognized as a comment. 58 (For backwards compatibility, only ``;`` starts an inline comment, while ``#`` 59 does not.) 60 61 On top of the core functionality, :class:`SafeConfigParser` supports 62 interpolation. This means values can contain format strings which refer to 63 other values in the same section, or values in a special ``DEFAULT`` section. 64 Additional defaults can be provided on initialization. 65 44 66 For example:: 45 67 … … 63 85 64 86 65 .. class:: RawConfigParser([defaults[, dict_type ]])87 .. class:: RawConfigParser([defaults[, dict_type[, allow_no_value]]]) 66 88 67 89 The basic configuration object. When *defaults* is given, it is initialized 68 90 into the dictionary of intrinsic defaults. When *dict_type* is given, it will 69 91 be used to create the dictionary objects for the list of sections, for the 70 options within a section, and for the default values. This class does not 92 options within a section, and for the default values. When *allow_no_value* 93 is true (default: ``False``), options without values are accepted; the value 94 presented for these is ``None``. 95 96 This class does not 71 97 support the magical interpolation behavior. 98 99 All option names are passed through the :meth:`optionxform` method. Its 100 default implementation converts option names to lower case. 72 101 73 102 .. versionadded:: 2.3 … … 76 105 *dict_type* was added. 77 106 78 79 .. class:: ConfigParser([defaults[, dict_type]]) 107 .. versionchanged:: 2.7 108 The default *dict_type* is :class:`collections.OrderedDict`. 109 *allow_no_value* was added. 110 111 112 .. class:: ConfigParser([defaults[, dict_type[, allow_no_value]]]) 80 113 81 114 Derived class of :class:`RawConfigParser` that implements the magical … … 87 120 88 121 All option names used in interpolation will be passed through the 89 :meth:`optionxform` method just like any other option name reference. For 90 example, using the default implementation of :meth:`optionxform` (which converts 91 option names to lower case), the values ``foo %(bar)s`` and ``foo %(BAR)s`` are 92 equivalent. 93 94 95 .. class:: SafeConfigParser([defaults[, dict_type]]) 122 :meth:`optionxform` method just like any other option name reference. Using 123 the default implementation of :meth:`optionxform`, the values ``foo %(bar)s`` 124 and ``foo %(BAR)s`` are equivalent. 125 126 .. versionadded:: 2.3 127 128 .. versionchanged:: 2.6 129 *dict_type* was added. 130 131 .. versionchanged:: 2.7 132 The default *dict_type* is :class:`collections.OrderedDict`. 133 *allow_no_value* was added. 134 135 136 .. class:: SafeConfigParser([defaults[, dict_type[, allow_no_value]]]) 96 137 97 138 Derived class of :class:`ConfigParser` that implements a more-sane variant of … … 103 144 104 145 .. versionadded:: 2.3 146 147 .. versionchanged:: 2.6 148 *dict_type* was added. 149 150 .. versionchanged:: 2.7 151 The default *dict_type* is :class:`collections.OrderedDict`. 152 *allow_no_value* was added. 153 154 155 .. exception:: Error 156 157 Base class for all other configparser exceptions. 105 158 106 159 … … 348 401 .. method:: ConfigParser.get(section, option[, raw[, vars]]) 349 402 350 Get an *option* value for the named *section*. All the ``'%'`` interpolations 351 are expanded in the return values, based on the defaults passed into the 352 constructor, as well as the options *vars* provided, unless the *raw* argument 353 is true. 354 403 Get an *option* value for the named *section*. If *vars* is provided, it 404 must be a dictionary. The *option* is looked up in *vars* (if provided), 405 *section*, and in *defaults* in that order. 406 407 All the ``'%'`` interpolations are expanded in the return values, unless the 408 *raw* argument is true. Values for interpolation keys are looked up in the 409 same manner as the option. 355 410 356 411 .. method:: ConfigParser.items(section[, raw[, vars]]) … … 397 452 # mode. SafeConfigParser does not allow such assignments to take place. 398 453 config.add_section('Section1') 399 config.set('Section1', ' int', '15')400 config.set('Section1', ' bool', 'true')401 config.set('Section1', ' float', '3.1415')454 config.set('Section1', 'an_int', '15') 455 config.set('Section1', 'a_bool', 'true') 456 config.set('Section1', 'a_float', '3.1415') 402 457 config.set('Section1', 'baz', 'fun') 403 458 config.set('Section1', 'bar', 'Python') … … 417 472 # getfloat() raises an exception if the value is not a float 418 473 # getint() and getboolean() also do this for their respective types 419 float = config.getfloat('Section1', 'float')420 int = config.getint('Section1', 'int')421 print float +int474 a_float = config.getfloat('Section1', 'a_float') 475 an_int = config.getint('Section1', 'an_int') 476 print a_float + an_int 422 477 423 478 # Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'. 424 479 # This is because we are using a RawConfigParser(). 425 if config.getboolean('Section1', ' bool'):480 if config.getboolean('Section1', 'a_bool'): 426 481 print config.get('Section1', 'foo') 427 482 … … 468 523 else: 469 524 config.remove_option(section1, option) 525 526 Some configuration files are known to include settings without values, but which 527 otherwise conform to the syntax supported by :mod:`ConfigParser`. The 528 *allow_no_value* parameter to the constructor can be used to indicate that such 529 values should be accepted: 530 531 .. doctest:: 532 533 >>> import ConfigParser 534 >>> import io 535 536 >>> sample_config = """ 537 ... [mysqld] 538 ... user = mysql 539 ... pid-file = /var/run/mysqld/mysqld.pid 540 ... skip-external-locking 541 ... old_passwords = 1 542 ... skip-bdb 543 ... skip-innodb 544 ... """ 545 >>> config = ConfigParser.RawConfigParser(allow_no_value=True) 546 >>> config.readfp(io.BytesIO(sample_config)) 547 548 >>> # Settings with values are treated as before: 549 >>> config.get("mysqld", "user") 550 'mysql' 551 552 >>> # Settings without values provide None: 553 >>> config.get("mysqld", "skip-bdb") 554 555 >>> # Settings which aren't specified still raise an error: 556 >>> config.get("mysqld", "does-not-exist") 557 Traceback (most recent call last): 558 ... 559 ConfigParser.NoOptionError: No option 'does-not-exist' in section: 'mysqld'
Note:
See TracChangeset
for help on using the changeset viewer.