diff -r 16350c1f81a1 Doc/library/configparser.rst --- a/Doc/library/configparser.rst Sun Dec 04 10:22:36 2016 +0200 +++ b/Doc/library/configparser.rst Sun Dec 04 10:12:10 2016 +0100 @@ -68,6 +68,10 @@ :mod:`configparser` classes can read and write such files. Let's start by creating the above configuration file programmatically. +.. testsetup:: + + import configparser + .. doctest:: >>> import configparser @@ -116,13 +120,13 @@ 'no' >>> topsecret['Port'] '50022' - >>> for key in config['bitbucket.org']: print(key) - ... + >>> for key in sorted(config['bitbucket.org']): + ... print(key) + compression + compressionlevel + forwardx11 + serveraliveinterval user - compressionlevel - serveraliveinterval - compression - forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes' @@ -469,10 +473,10 @@ ... 'bar': 'y', ... 'baz': 'z'} ... }) - >>> parser.sections() - ['section3', 'section2', 'section1'] - >>> [option for option in parser['section3']] - ['baz', 'foo', 'bar'] + >>> sorted(parser.sections()) + ['section1', 'section2', 'section3'] + >>> sorted(option for option in parser['section3']) + ['bar', 'baz', 'foo'] In these operations you need to use an ordered dictionary as well: @@ -514,8 +518,6 @@ .. doctest:: - >>> import configparser - >>> sample_config = """ ... [mysqld] ... user = mysql @@ -571,7 +573,9 @@ values with characters used as comment prefixes. When in doubt, avoid setting *inline_comment_prefixes*. In any circumstances, the only way of storing comment prefix characters at the beginning of a line in multiline - values is to interpolate the prefix, for example:: + values is to interpolate the prefix, for example: + + .. doctest:: >>> from configparser import ConfigParser, ExtendedInterpolation >>> parser = ConfigParser(interpolation=ExtendedInterpolation()) @@ -597,11 +601,11 @@ ... line #3 ... """) >>> print(parser['hashes']['shebang']) - + #!/usr/bin/env python # -*- coding: utf-8 -*- >>> print(parser['hashes']['extensions']) - + enabled_extension another_extension yet_another_extension @@ -755,6 +759,7 @@ .. doctest:: + >>> import re >>> config = """ ... [Section 1] ... option = value @@ -762,11 +767,11 @@ ... [ Section 2 ] ... another = val ... """ - >>> typical = ConfigParser() + >>> typical = configparser.ConfigParser() >>> typical.read_string(config) >>> typical.sections() ['Section 1', ' Section 2 '] - >>> custom = ConfigParser() + >>> custom = configparser.ConfigParser() >>> custom.SECTCRE = re.compile(r"\[ *(?P
[^]]+?) *\]") >>> custom.read_string(config) >>> custom.sections()