Skip to main content

Updated configparser from Python 3.7 for Python 2.6+.

Project description

https://img.shields.io/pypi/v/configparser.svg https://img.shields.io/pypi/pyversions/configparser.svg https://img.shields.io/travis/jaraco/configparser/master.svg Code style: Black https://readthedocs.org/projects/configparser/badge/?version=latest https://tidelift.com/badges/package/pypi/configparser

The ancient ConfigParser module available in the standard library 2.x has seen a major update in Python 3.2. This is a backport of those changes so that they can be used directly in Python 2.6 - 3.5.

To use the configparser backport instead of the built-in version on both Python 2 and Python 3, simply import it explicitly as a backport:

from backports import configparser

If you’d like to use the backport on Python 2 and the built-in version on Python 3, use that invocation instead:

import configparser

For detailed documentation consult the vanilla version at http://docs.python.org/3/library/configparser.html.

Why you’ll love configparser

Whereas almost completely compatible with its older brother, configparser sports a bunch of interesting new features:

  • full mapping protocol access (more info):

    >>> parser = ConfigParser()
    >>> parser.read_string("""
    [DEFAULT]
    location = upper left
    visible = yes
    editable = no
    color = blue
    
    [main]
    title = Main Menu
    color = green
    
    [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)'
  • there’s now one default ConfigParser class, which basically is the old SafeConfigParser with a bunch of tweaks which make it more predictable for users. Don’t need interpolation? Simply use ConfigParser(interpolation=None), no need to use a distinct RawConfigParser anymore.

  • the parser is highly customizable upon instantiation supporting things like changing option delimiters, comment characters, the name of the DEFAULT section, the interpolation syntax, etc.

  • you can easily create your own interpolation syntax but there are two powerful implementations built-in (more info):

    • the classic %(string-like)s syntax (called BasicInterpolation)

    • a new ${buildout:like} syntax (called ExtendedInterpolation)

  • fallback values may be specified in getters (more info):

    >>> config.get('closet', 'monster',
    ...            fallback='No such things as monsters')
    'No such things as monsters'
  • ConfigParser objects can now read data directly from strings and from dictionaries. That means importing configuration from JSON or specifying default values for the whole configuration (multiple sections) is now a single line of code. Same goes for copying data from another ConfigParser instance, thanks to its mapping protocol support.

  • many smaller tweaks, updates and fixes

A few words about Unicode

configparser comes from Python 3 and as such it works well with Unicode. The library is generally cleaned up in terms of internal data storage and reading/writing files. There are a couple of incompatibilities with the old ConfigParser due to that. However, the work required to migrate is well worth it as it shows the issues that would likely come up during migration of your project to Python 3.

The design assumes that Unicode strings are used whenever possible [1]. That gives you the certainty that what’s stored in a configuration object is text. Once your configuration is read, the rest of your application doesn’t have to deal with encoding issues. All you have is text [2]. The only two phases when you should explicitly state encoding is when you either read from an external source (e.g. a file) or write back.

Versioning

This project uses semver to communicate the impact of various releases while periodically syncing with the upstream implementation in CPython. The changelog serves as a reference indicating which versions incorporate which upstream functionality.

Prior to the 4.0.0 release, another scheme was used to associate the CPython and backports releases.

Maintenance

This backport was originally authored by Łukasz Langa, the current vanilla configparser maintainer for CPython and is currently maintained by Jason R. Coombs:

Security Contact

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

Conversion Process

This section is technical and should bother you only if you are wondering how this backport is produced. If the implementation details of this backport are not important for you, feel free to ignore the following content.

configparser is converted using python-future. The project takes the following branching approach:

  • the 3.x branch holds unchanged files synchronized from the upstream CPython repository. The synchronization is currently done by manually copying the required files and stating from which CPython changeset they come from.

  • the master branch holds a version of the 3.x code with some tweaks that make it independent from libraries and constructions unavailable on 2.x. Code on this branch still must work on the corresponding Python 3.x but will also work on Python 2.6 and 2.7 (including PyPy). You can check this running the supplied unit tests with tox.

The process works like this:

  1. In the 3.x branch, run pip-run -- sync-upstream.py, which downloads the latest stable release of Python and copies the relevant files from there into their new locations here and then commits those changes with a nice reference to the relevant upstream commit hash.

  2. I check for new names in __all__ and update imports in configparser.py accordingly. I run the tests on Python 3. Commit.

  3. I merge the new commit to master. I run tox. Commit.

  4. If there are necessary changes, I do them now (on master). Note that the changes should be written in the syntax subset supported by Python 2.6.

  5. I run tox. If it works, I update the docs and release the new version. Otherwise, I go back to point 3. I might use pasteurize to suggest me required changes but usually I do them manually to keep resulting code in a nicer form.

Footnotes

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

configparser-4.0.2.tar.gz (72.5 kB view details)

Uploaded Source

Built Distribution

configparser-4.0.2-py2.py3-none-any.whl (22.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file configparser-4.0.2.tar.gz.

File metadata

  • Download URL: configparser-4.0.2.tar.gz
  • Upload date:
  • Size: 72.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.1

File hashes

Hashes for configparser-4.0.2.tar.gz
Algorithm Hash digest
SHA256 c7d282687a5308319bf3d2e7706e575c635b0a470342641c93bea0ea3b5331df
MD5 35926cc4b9133f1f9ca70a1fd2fdf237
BLAKE2b-256 164f48975536bd488d3a272549eb795ac4a13a5f7fcdc8995def77fbef3532ee

See more details on using hashes here.

File details

Details for the file configparser-4.0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: configparser-4.0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.1

File hashes

Hashes for configparser-4.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 254c1d9c79f60c45dfde850850883d5aaa7f19a23f13561243a050d5a7c3fe4c
MD5 7f64f596556950d557e9da5255da81d7
BLAKE2b-256 7a2a95ed0501cf5d8709490b1d3a3f9b5cf340da6c433f896bbe9ce08dbe6785

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page