Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Decent config file format

I'd like to use a configuration file format which supports key value pairs and nestable, repeatable structures, and which is as light on syntax as possible. I'm imagining something along the lines of:

cachedir = /var/cache
mail_to = [email protected]

job {
   name = my-media
   frequency = 1 day
   source {
      from = /home/michael/Images

   source { }
   source { }       
}

job { }

I'd be happy with something using significant-whitespace as well.

JSON requires too many explicit syntax rules (quoting, commas, etc.). YAML is actually pretty good, but would require the jobs to be defined as a YAML list, which I find slightly awkward to use.

like image 876
miracle2k Avatar asked Jun 09 '10 16:06

miracle2k


People also ask

What is the best config file format?

Simple configuration languages, such as JSON, work for many applications, but when you need proper validation, schema, and namespace support, XML is often best.

Does Python have a config file?

Python can have config files with all settings needed by the application dynamically or periodically. Python config files have the extension as . ini. We'll use VS Code (Visual Studio Code) to create a main method that uses config file to read the configurations and then print on the console.


3 Answers

I think YAML is great for this purpose, actually:

jobs:
 - name: my-media
   ...

 - name: something else
   ...

Or, as a dict instead of list:

jobs:
  my-media:
    frequency: 1 day
    ...
  something-else:
    frequency: 2 day
    ...

Another thing to consider, which you might not have, is using Python source for the configuration. You can nest Python dicts and lists in a very readable manner and it provides multiple unexpected benefits. Django uses Python source for its settings files, for example.

like image 81
Eli Bendersky Avatar answered Oct 05 '22 01:10

Eli Bendersky


As Python's built-in configparser module does not seem to support nested sections, I'd first try ConfigObj. (See an introductory tutorial here). According to its homepage, this is the set of features worth mentioning:

  • Nested sections (subsections), to any level
  • List values
  • Multiple line values
  • String interpolation (substitution)
  • Integrated with a powerful validation system
    • including automatic type checking/conversion
    • repeated sections
    • and allowing default values
  • When writing out config files, ConfigObj preserves all comments and the order of members and sections
  • Many useful methods and options for working with configuration files (like the 'reload' method)
  • Full Unicode support

ConfigObj is used by Bazaar, Trac, IPython, matplotlib and many other large Python projects, so it seems pretty mature and stable to me (although I never used it myself).

like image 40
Tamás Avatar answered Oct 05 '22 03:10

Tamás


I think you should check libconfig library http://www.hyperrealm.com/libconfig/. There should be somewhere python bindings for it.

Another solution is to use json format which is already provided by python itself. Search documentation for JSON module.

like image 31
Zuljin Avatar answered Oct 05 '22 02:10

Zuljin