Python:不错的配置文件格式
我想使用一种支持键值对和可嵌套、可重复结构的配置文件格式,并且语法尽可能简单。我正在想象这样的事情:
cachedir = /var/cache
mail_to = [email protected]
job {
name = my-media
frequency = 1 day
source {
from = /home/michael/Images
source { }
source { }
}
job { }
我也会对使用重要空白的东西感到满意。
JSON 需要太多显式语法规则(引用、逗号等)。 YAML 实际上相当不错,但需要将作业定义为 YAML 列表,我觉得使用起来有点尴尬。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为 YAML 非常适合这个目的,实际上:
或者,作为字典而不是列表:
另一件事要考虑,你可能没有,是使用 Python 源进行配置。您可以以一种非常易读的方式嵌套 Python 字典和列表,它提供了多种意想不到的好处。例如,Django 使用 Python 源代码作为其设置文件。
I think YAML is great for this purpose, actually:
Or, as a dict instead of list:
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的内置
configparser
模块似乎不支持嵌套部分,我首先尝试ConfigObj。 (请参阅此处的介绍性教程)。根据其主页,这是一组值得一提的功能:ConfigObj 由 Bazaar、Trac、IPython、matplotlib 等使用其他大型Python项目,所以对我来说它看起来相当成熟和稳定(尽管我自己从未使用过它)。
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: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).
我认为你应该检查 libconfig 库 http://www.hyperrealm.com/libconfig/。
应该有一个 python 绑定它的地方。
另一种解决方案是使用python本身已经提供的json格式。搜索 JSON 模块的文档。
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.
为什么要重新发明轮子?您可以使用:
http://docs.python.org/library/configparser.html< /a>
Why re-invent the wheel? You can make use of:
http://docs.python.org/library/configparser.html
如果您的需求超出了这些其他选项,您还可以考虑 Jsonnet。 Jsonnet 是 JSON 的扩展,乍一看它添加了注释,放宽了逗号规则,并且消除了如此多的引用。但如果你深入观察,你会发现它确实提供了一种完整的函数式编程语言,并且支持通过 mixins、文件导入等进行模板扩展。它有一个 Python 绑定,但它的实际实现是 C++。
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++.