Monthly Archives: October 2011

Accessing Configurations and Settings in Python

The Problem

When coding in Python, we need a way to store configurations and
settings in a file, then be able to access them in the code. Our
requirements for accessing these settings are:

  • Simple to learn and use — We would like to target novice coders
  • Easy to understand
  • Hierarchical of settings

References

The Options

Currently, there are a few options for accessing configurations files,
some of them are:

  • INI file format
    • ConfigParser module
    • ConfigObj
    • INITools
    • cfgparse
  • Straight Python code, via the import mechanism
  • YAML file format with yaml module
  • HierConfig
  • JSON
  • Vinay Sajip’s config module
  • tconfpy

In the following sections, we are going to discuss the advantages and
disadvantages of some of these.

ConfigParser

Advantages

  • Library comes with Python installation, no need to install it
  • Ability to specify a default section
  • Low learning curve

Disadvantages

  • Not zero learning-curve as with straight Python code method
  • No hierarchical of data: the default data layout is just one layer
    deep: A configuration file consists of one or more sections and each
    section contains one or more settings.

ConfigObj

Advantages

  • Simple to learn: the configuration is a two-dimensional array: one for
    each section, and one for the settings
  • Round-trip: read/write from/to files
  • Many other features

Disadvantages

  • Need installation

INITools

This tool is abandoned, so we only mention here for completeness.

cfgparse

Advantages

  • Round-trip read and write
  • Many features
  • Ability to specify a default section

Disadvantages

  • Need installation
  • Not simple to use
  • Higher learning curve than other INI solutions
  • INI file can without section header, which is non-standard

Straight Python Code

Advantages

  • Natively supported: We don’t need any module to parse
  • Low learning curve: The users do not need to learn another
    configuration file format
  • Versatile: This file format supports all kind of data types: integer,
    string, dictionary, array, …
  • Simple usage: The users only need to make sure that the settings file
    is in the same directory as the script

Disadvantages

  • No “default” mechanism as in INI
  • Cannot merge different settings files into one
  • No simple mechanism for dynamically load configuration files. For
    example, the ability to specify different configuration files from the
    command line

Conclusion

Of these solutions, we prefer the the native, straight Python code for
its simplicity, ease of use, and zero learning curve. In the next posts,
I will discuss the straight python code solution in details.

Advertisements