Python:不错的配置文件格式

发布于 2024-09-04 18:31:56 字数 492 浏览 10 评论 0原文

我想使用一种支持键值对和可嵌套、可重复结构的配置文件格式,并且语法尽可能简单。我正在想象这样的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

秋风の叶未落 2024-09-11 18:31:56

我认为 YAML 非常适合这个目的,实际上:

jobs:
 - name: my-media
   ...

 - name: something else
   ...

或者,作为字典而不是列表:

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

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

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.

就像说晚安 2024-09-11 18:31:56

由于Python的内置configparser模块似乎不支持嵌套部分,我首先尝试ConfigObj。 (请参阅此处的介绍性教程)。根据其主页,这是一组值得一提的功能:

  • 嵌套节(子节),任意级别
  • 列表值
  • 多行值
  • 字符串插值(替换)
  • 与强大的验证系统集成
    • 包括自动类型检查/转换
    • 重复部分
    • 并允许默认值
  • 编写配置文件时,ConfigObj 保留所有注释以及成员和部分的顺序
  • 许多用于处理配置文件的有用方法和选项(例如“重新加载”方法) )
  • 完整的 Unicode 支持

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:

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

-黛色若梦 2024-09-11 18:31:56

我认为你应该检查 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.

剩余の解释 2024-09-11 18:31:56

为什么要重新发明轮子?您可以使用:

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

你不是我要的菜∠ 2024-09-11 18:31:56

如果您的需求超出了这些其他选项,您还可以考虑 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++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文