Design considerations and trade-offs

Configuration files can simplify running application programs or starting servers. This can put all the relevant parameters in one easy-to-read and easy-to-modify file. We can put these files under the configuration control, track change history, and generally use them to improve the software's quality.

We have several alternative formats for these files, all of which are reasonably human-friendly to edit. They vary in how easy they are to parse and any limitations on the Python data that can be encoded:

  • INI files: These files are easy to parse and are limited to strings and numbers.
  • Python code (PY files): We can use the main script for the configuration; in this case, there will be no additional parsing and no limitations. We can also use exec() to process a separate file; this makes it trivial to parse and, again, there are no limitations.
  • JSON or YAML files: These files are easy to parse. They support strings, numbers, dicts, and lists. YAML can encode Python, but then why not just use Python?
  • Properties files: These files require a special parser. They are limited to strings.
  • XML files:
    • PLIST files: These files are easy to parse. They support strings, numbers, dicts, and lists.
    • Customized XML: These files require a special parser. They are limited to strings, but a mapping to a Python object allows a variety of conversions to be performed by the class.

Coexistence with other applications or servers will often determine a preferred format for the configuration files. If we have other applications that use PLIST or INI files, then our Python applications should make choices that are more comfortable for users to work with.

Viewed from the breadth of objects that can be represented, we have four broad categories of configuration files:

  • Simple files with only strings: Custom XML and properties files.
  • Simple files with Python literals: INI files.
  • More complex files with Python literals, lists, and dicts: JSON, YAML, PLIST, and XML.
  • Anything that is Python: We can use YAML for this, but it seems silly when Python has a clearer syntax than YAML. Providing configuration values through Python class definitions is very simple and leads to a pleasant hierarchy of default and override values.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset