Python: Decent config file format

2019-04-06 20:05发布

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 = me@example.org

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.

6条回答
一纸荒年 Trace。
2楼-- · 2019-04-06 20:13

You can also consider Jsonnet if your needs exceed these other options. Jsonnet is an extension of JSON that at first glance adds comments, relaxes comma rules, and removes the need for so much quoting. But if you look deeper you see it really provides a full functional programming language and has support for template extension via mixins, file imports, etc. There is a Python binding for it, but its actual implementation is C++.

查看更多
我想做一个坏孩纸
3楼-- · 2019-04-06 20:22

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).

查看更多
狗以群分
4楼-- · 2019-04-06 20:23

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.

查看更多
我命由我不由天
5楼-- · 2019-04-06 20:25

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.

查看更多
Juvenile、少年°
6楼-- · 2019-04-06 20:28

Why re-invent the wheel? You can make use of:

http://docs.python.org/library/configparser.html

查看更多
Ridiculous、
7楼-- · 2019-04-06 20:34

You can use the config system of red-dove.

http://www.red-dove.com/config-doc/

查看更多
登录 后发表回答