体面配置文件格式

体面配置文件格式

本文介绍了Python:体面配置文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用支持键值对和可嵌套,可重复结构的配置文件格式,以及尽可能轻松的语法。我想象下的东西:

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 { }

$ b

JSON需要太多明确的语法规则(引号,逗号,等等。)。 YAML实际上相当不错,但是需要将这些作业定义为一个YAML列表,我觉得使用起来有点尴尬。

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.

推荐答案

p>我认为YAML是伟大的这一目的,实际上:

I think YAML is great for this purpose, actually:

jobs:
 - name: my-media
   ...

 - name: something else
   ...

或者,作为dict而不是列表:

Or, as a dict instead of list:

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

你可能没有考虑的另一件事情是使用Python源代码进行配置。您可以以非常可读的方式嵌套Python字典和列表,它提供了多个意想不到的好处。例如,Django使用Python源代码来设置它的设置文件。

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.

这篇关于Python:体面配置文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 08:43