ConfigObj/ConfigParser 与使用 YAML 作为 Python 设置文件

发布于 2024-09-13 12:53:32 字数 189 浏览 2 评论 0原文

对于为Python程序创建设置文件,内置模块(ConfigParser)或独立项目(ConfigObj),还是使用YAML数据序列化格式,哪个更好?我听说 ConfigObj 比 ConfigParser 更容易使用,尽管它不是内置库。我还了解到 PyYAML 很容易使用,尽管 YAML 需要一些时间来使用。除了易于实施之外,哪个是创建设置/配置文件的最佳选择?

Which is better for creating a settings file for Python programs, the built-in module (ConfigParser) or the independent project (ConfigObj), or using the YAML data serialization format? I have heard that ConfigObj is easier to use than ConfigParser, even though it is not a built-in library. I have also read that PyYAML is easy to use, though YAML takes a bit of time to use. Ease of implementation aside, which is the best option for creating a settings/configuration file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

抱猫软卧 2024-09-20 12:53:32

这取决于您想要在配置文件中存储的内容以及如何使用它们

  • 如果您进行往返 (yaml→code→yaml) 并希望保留注释,则不能使用 PyYAML配置解析器

  • 如果您想保留键的顺序(例如,当您签入配置文件时),PyYAML 不会执行此操作,除非您指定 !!omap (这使得它比普通映射更不容易更新)

  • 如果您想要具有包含映射/字典的未命名元素列表的复杂结构,那么ConfigParserConfigObj将不会'这对您没有帮助,因为 INI 文件键值对必须进入各个部分,并且列表只能是值。

YAML 阅读器的 ruamel.yaml 实现支持所有上述 1。我长期以来一直使用 fuzzyman 出色的 ConfigObj 来保存往返注释,并使用 PyYAML 来保存更复杂的结构,这结合了两全其美。 ruamel.yaml 包含 yaml 实用程序,可以将 ConfigObj INI 文件转换为 YAML


1 ruamel.yaml 是一个支持 YAML 的 YAML 库1.2(我建议使用它,但我是该包的作者)。 PyYAML 仅支持(大部分)YAML 1.1。

It depends on what you want to store in your config files and how you use them

  • If you do round tripping (yaml→code→yaml) and want comments preserved you cannot use PyYAML or ConfigParser.

  • If you want to preserve the order of your keys (e.g. when you check in your config files), PyYAML doesn't do that unless you specify !!omap (which makes it less easy to update than a normal mapping)

  • If you want to have complex structures with lists of unnamed elements containing mappings/dictionaries, then ConfigParser and ConfigObj won't help you as the INI files key-value pairs have to go into sections and lists can only be values.

The ruamel.yaml implementation of the YAML reader supports all of the above ¹. I have used fuzzyman's excellent ConfigObj for round trip comment preservation for a long time, as well as PyYAML for more complex structures and this combines best of both worlds. ruamel.yaml includes the yaml utility that can convert ConfigObj INI files to YAML


¹ ruamel.yaml is a YAML library that supports YAML 1.2 (I recommend using that, but then I am the author of the package). PyYAML only supports (most of) YAML 1.1.

南巷近海 2024-09-20 12:53:32

使用 ConfigObj 至少非常简单,并且一般中的 ini 文件比 YAML 简单得多(并且使用更广泛)。对于更复杂的情况,包括验证、默认值和类型,ConfigObj 提供了一种通过 configspec 验证来实现此目的的方法。

使用 ConfigObj 读取 ini 文件的简单代码:

from configobj import ConfigObj

conf = ConfigObj('filename.ini')
section = conf['section']
value = section['value']

它自动为您处理列表值并允许多行值。如果您修改配置文件,您可以将其写回,同时保留顺序和注释。

有关详细信息,请参阅这些文章,包括类型验证的示例:

Using ConfigObj is at least very straightforward and ini files in general are much simpler (and more widely used) than YAML. For more complex cases, including validation, default values and types, ConfigObj provides a way to do this through configspec validation.

Simple code to read an ini file with ConfigObj:

from configobj import ConfigObj

conf = ConfigObj('filename.ini')
section = conf['section']
value = section['value']

It automatically handles list values for you and allows multiline values. If you modify the config file you can write it back out whilst preserving order and comments.

See these articles for more info, including examples of the type validation:

巷子口的你 2024-09-20 12:53:32

ConfigParser 有一个非常糟糕的 API,ConfigObj 应该很好,但我从未使用过它,对于 ini 文件我通常实现自己的解析器。

然而,类似 ini 的格式无论如何都不能很好地处理不同的类型、序列或递归映射,所以我只使用 yaml。使用编辑器很容易阅读和编辑,有一个解析这些文件的标准,并且您没有提到的类似 ini 格式的缺点。

ConfigParser has a really bad API, ConfigObj is supposed to be good but I have never used it, for ini files I usually implement my own parser.

However ini-like formats don't handle different types, sequences or recursive mappings well anyway so I would just use yaml. It's easy to read and edit with an editor, there is a standard for parsing those files and you have none of the mentioned downsides ini-like formats have.

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