简体   繁体   English

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

[英]ConfigObj/ConfigParser vs. using YAML for Python settings file

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? 哪个更适合为Python程序,内置模块(ConfigParser)或独立项目(ConfigObj)创建设置文件,还是使用YAML数据序列化格式? I have heard that ConfigObj is easier to use than ConfigParser, even though it is not a built-in library. 我听说ConfigObj比ConfigParser更容易使用,即使它不是内置库。 I have also read that PyYAML is easy to use, though YAML takes a bit of time to use. 我还读到PyYAML很容易使用,虽然YAML需要一些时间来使用。 Ease of implementation aside, which is the best option for creating a settings/configuration file? 除了易于实现,这是创建设置/配置文件的最佳选择?

Using ConfigObj is at least very straightforward and ini files in general are much simpler (and more widely used) than YAML. 使用ConfigObj至少非常简单,并且ini文件通常比YAML更简单(并且使用更广泛)。 For more complex cases, including validation, default values and types, ConfigObj provides a way to do this through configspec validation. 对于更复杂的情况,包括验证,默认值和类型,ConfigObj提供了一种通过configspec验证来实现此目的的方法。

Simple code to read an ini file with ConfigObj: 使用ConfigObj读取ini文件的简单代码:

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: 有关更多信息,请参阅这些文章,包括类型验证的示例:

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 . 如果您进行往返(yaml→code→yaml)并希望保留注释,则无法使用PyYAMLConfigParser

  • If you want to preserve the order of your keys (eg 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) 如果你想保留你的密钥的顺序(例如当你签入你的配置文件时), PyYAML不会这样做,除非你指定!!omap (这使得它比普通的映射更容易更新)

  • If you want to have complex structures with lists of unnamed elements containing mappings/dictionaires, 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. 如果你想拥有包含映射/字典的未命名元素列表的复杂结构,那么ConfigParserConfigObj将无法帮助你,因为INI文件的键值对必须进入部分,而列表只能是值。

The ruamel.yaml implementation of the YAML reader supports all of the above ¹. YAML阅读器的ruamel.yaml实现支持上述所有¹。 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. 我已经使用了fuzzyman优秀的ConfigObj来保存往返评论很长一段时间,以及PyYAML用于更复杂的结构,这结合了两者的优点。 ruamel.yaml includes the yaml utility that can convert ConfigObj INI files to YAML ruamel.yaml包含可以将ConfigObj INI文件转换为YAML的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). ¹ruamel.yaml是一个支持YAML 1.2的YAML库(我建议使用它,但后来我是该软件包的作者)。 PyYAML only supports (most of) YAML 1.1. PyYAML仅支持(大部分)YAML 1.1。

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. ConfigParser有一个非常糟糕的API,ConfigObj应该是好的,但我从来没有使用它,对于ini文件我通常实现自己的解析器。

However ini-like formats don't handle different types, sequences or recursive mappings well anyway so I would just use yaml. 但是,类似ini的格式无论如何都不能很好地处理不同的类型,序列或递归映射,因此我只使用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. 使用编辑器很容易阅读和编辑,有一个解析这些文件的标准,你没有提到的类似ini的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM