--- configparser.rst 2016-12-02 23:11:23.161377313 +0100 +++ configparser_new.rst 2016-12-02 23:05:01.445371124 +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,7 +120,7 @@ 'no' >>> topsecret['Port'] '50022' - >>> for key in config['bitbucket.org']: print(key) + >>> for key in config['bitbucket.org']: print(key) # doctest: +SKIP ... user compressionlevel @@ -469,9 +473,9 @@ ... 'bar': 'y', ... 'baz': 'z'} ... }) - >>> parser.sections() + >>> parser.sections() # doctest: +SKIP ['section3', 'section2', 'section1'] - >>> [option for option in parser['section3']] + >>> [option for option in parser['section3']] # doctest: +SKIP ['baz', 'foo', 'bar'] 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()