不同配置格式的优缺点?

发布于 2024-09-06 06:44:28 字数 394 浏览 11 评论 0原文

我见过人们使用 *.cfg (Python Buildout)、*.xml (Gnome)、*.json (Chrome 扩展)、*.yaml (Google App Engine)、*.ini 甚至 *.py 作为应用程序配置文件(如姜戈)。

我的问题是:为什么有这么多不同的配置文件格式?我可以看到 xml 与 json 方法(更不那么冗长)或 Python 方法(有时你有一个 Python 应用程序,并且不想仅使用特定模块来解析配置文件)的优势,但另一种呢?方法?

我知道还有比我举例说明的配置文件更多的格式。与彼此相比,它们的真正优势是什么?历史原因?不同系统的兼容性?

如果您要启动一个应用程序来读取某种配置文件(带有插件生态系统),您会使用哪一个?

我举的例子中哪些是最古老的?你知道它的历史吗?

I've seen people using *.cfg (Python Buildout), *.xml (Gnome), *.json (Chrome extension), *.yaml (Google App Engine), *.ini and even *.py for app configuration files (like Django).

My question is: why there are so many different configuration file formats? I can see an advantage from a xml vs json approach (much less verbose) or a Python one (sometimes you have a Python app and don't want to use a specific module just to parse a config file), but what about the other approaches?

I know there are even more formats than those configuration files I exemplified. What are really their advantages in comparison to each other? Historical reasons? Compatibility with different systems?

If you would start an application to read some kind of configuration files (with a plugin ecosystem), which one would you use?

Which ones that I gave as example are the oldest ones? Do you know it's history?

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

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

发布评论

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

评论(6

给不了的爱 2024-09-13 06:44:28

这主要是个人喜好、目的和可用的库。我个人认为 xml 对于配置文件来说太冗长了,但它很流行并且有很棒的库。

.cfg、.ini 是运行良好的旧格式,许多语言都有一个包含的库可以读取它们。我在 Java、Python、C++ 中使用过它,没有任何问题。它实际上并不能作为数据交换格式,如果我传递数据,我可能会使用相同的格式进行配置和数据交换。

yaml和json位于xml和cfg/ini之间。您可以在两者中定义许多数据结构,也可以是像 cfg 一样的简单键值。这两种格式在 python 中都有很棒的库,我假设许多其他语言也有库。我相信 json 是 yaml 的子集。

我从未使用过 python 文件作为配置,但它似乎对于 django 来说效果很好。它确实允许您在配置中包含一些可能有用的代码。

上次我选择格式时选择了 yaml。它很简单,但有一些不错的功能,而且 python 库很容易安装,而且非常好。 Json 紧随其后,由于 yaml 库解析 json,我选择了 yaml。

It's mostly personal preference, purpose, and available libraries. Personally I think xml is way too verbose for config files, but it is popular and has great libraries.

.cfg, .ini are legacy formats that work well and many languages have an included library that reads them. I've used it in Java, Python, C++ without issues. It doesn't really work as a data interchange format and if I am passing data I will probably use the same format for config and data interchange.

yaml, and json are between xml and cfg/ini. You can define many data structures in both, or it can be a simple key-value like with cfg. Both of these formats have great libraries in python and I'm assuming many other languages have libraries as well. I believe json is subset of yaml.

I've never used a python file as config, but it does seem to work well for django. It does allow you to have some code in the config which might be useful.

Last time I was choosing a format I chose yaml. It's simple but has some nice features, and the python library was easy to install and really good. Json was a close second and since the yaml library parsed json I chose yaml over it.

潜移默化 2024-09-13 06:44:28

另一个可行的选择是 toml,这是另一种“介于 ini 和 xml”格式。

如今,toml 在 Python 中已经成熟 - 从 tomllib 上的 Python 3.11 开始,tomllib 已包含在 Python 中标准库。它是一个只读解析器,但官方文档提到了外部读写库。

toml 有什么好处?

# Toml prioritize human-readability, allowing comments.

[ section-1 ]
key = "value pair" # is basic.
"Thus looks" = "like ini."

list = ['And', 'arrays', 'are supported, too' ]
dict = { "json-like": "dict type is",
         "available": {
             "too.": "But this is",
             "very": "readable."
         }
       }

[ types ]
number.int = 123
numbers.hex = 0xABCD
numbers.float = 3.14e-16

local.date = 2023-06-28
local.time = 12:34:56

[ others ]
longline = '''Lorem
Ipsum Foo
Bar baz'''

comment = '''This could've been specified as others.comment.
See the official document for the details.'''

Another viable choice is toml, which is another "between ini and xml" format.

Today toml is mature in Python - from Python 3.11 on tomllib is included in the Python Standard Library. It's a read-only parser, but the offical doc mentions external read-write libraries.

What's good with toml?

# Toml prioritize human-readability, allowing comments.

[ section-1 ]
key = "value pair" # is basic.
"Thus looks" = "like ini."

list = ['And', 'arrays', 'are supported, too' ]
dict = { "json-like": "dict type is",
         "available": {
             "too.": "But this is",
             "very": "readable."
         }
       }

[ types ]
number.int = 123
numbers.hex = 0xABCD
numbers.float = 3.14e-16

local.date = 2023-06-28
local.time = 12:34:56

[ others ]
longline = '''Lorem
Ipsum Foo
Bar baz'''

comment = '''This could've been specified as others.comment.
See the official document for the details.'''
〃温暖了心ぐ 2024-09-13 06:44:28

请注意,这纯粹是我的观点和猜测,但我怀疑格式过多的最大原因可能是由于缺乏现成的、无所不在的配置文件解析库。如果缺乏这一点,大多数程序必须编写自己的解析器,因此通常会在配置结构需要的复杂程度(分层与扁平、纯数据与嵌入式逻辑(如 if 语句等)、开发人员需要付出多少努力之间进行平衡)我们愿意花在编写配置文件解析器上,以及这对最终用户来说应该是多么痛​​苦。然而,您列出的和可能想到的每一个原因都可能是一两个项目选择其格式的动机。

对于我自己的项目,我倾向于使用 .ini,只是因为 Python 中已经内置了一个出色的解析器,并且它对于我的大多数用例来说“足够好”。在这两种情况下,这是不够的,我再次使用了基于 XML 的配置文件,因为实现相对简单。

Note, this is pure opinion and speculation on my part but I suspect that the single biggest reason for the plethora of formats is likely due to the lack of a readily available, omnipresent configuration file parsing library. Lacking that, most programs have to write their own parsers so it would often come down to balance between how complex the configuration structure needs to be (hierarchical vs flat, purely data vs embedded logic like if statements, etc), how much effort the developers were willing to spend on writing a configuration file parser, and how much of a pain it should be for the end user. However, probably every reason you've listed and could think has likely been the motivation for a project or two in choosing their format.

For my own projects I tend to use .ini simply because there's an excellent parser already built in to Python and it's been "good enough" for most of my use cases. In the couple of cases it's been insufficient, I've used an XML based configuration file due, again, to the relative simplicity of implementation.

不…忘初心 2024-09-13 06:44:28

大多数配置文件格式都非常复杂,因为它们支持太多的数据类型。例如,2 可以解释为整数、实数和字符串,并且由于大多数配置语言将值转换为这些基础数据类型,因此像 2 这样的值必须以同时传达值和类型的方式编写。因此,2 通常被解释为整数,2.0 被解释为实数,"2" 被解释为字符串。这使得这些语言对于不了解这些约定的非程序员来说很难使用。它还增加了语言的复杂性和混乱。对于字符串来说尤其如此。字符串是任意字符的序列,因此有效字符串可能包含引号字符。为了区分嵌入的引号字符和终止字符串的引号字符,必须对嵌入的引号字符进行转义。这会导致更多的复杂性和混乱。

JSON 和 Python 是明确的,并且可以很好地处理层次结构,但由于类型提示、引用和转义而变得混乱。

YAML 使引用成为可选,这反过来又使格式变得不明确。

INI 将所有值视为字符串,这要求最终应用程序根据需要转换为预期的数据类型。这消除了对类型提示、引用和转义的需要。但是INI本身并不支持层次结构,并且没有标准化,并且不同的实现与其对文件的解释不一致。

现在可以使用一种与 INI 类似的新格式,因为所有叶值都是字符串,但它是标准化的(它具有明确的规范和全面的测试套件)并且自然地处理层次结构。 NestedText 产生非常简单的配置格式,适合程序员和非程序员。它应该被视为所有新应用程序的配置文件和结构化数据文件格式,特别是对于临时用户读取、写入或修改文件的应用程序。

Most configuration file formats inherit significant complexity because they support too many data types. For example, 2 can be interpreted as an integer, a real number, and a string, and since most configuration languages convert values into these underlying data types, a value like 2 must be written in such a way to convey both the value and the type. Thus, 2 is generally interpreted as an integer, 2.0 is interpreted as a real number, and "2" is interpreted as a string. This makes these languages hard to use for non-programmers who do not know these conventions. It also adds complexity and clutter to the language. This is particularly true for strings. Strings are a sequence of arbitrary characters, and so valid strings may contain the quote character. To distinguish between an embedded quote character and the quote character that terminates the string, the embedded quote character must be escaped. This leads to more complexity and clutter.

JSON and Python are unambiguous and handle hierarchy well, but are cluttered by type cues, quoting and escaping.

YAML makes the quoting optional, which in turn makes the format ambiguous.

INI takes all values as strings, which requires the end application to convert to the expected data type if required. This eliminates the need for type cues, quoting and escaping. But INI does not naturally support hierarchy, and is not standardized and different implementations are inconsistent with their interpretation of files.

A new format is now available that is similar to INI in that all leaf values are strings, but it is standardized (it has an unambiguous spec and a comprehensive test suite) and naturally handles hierarchy. NestedText results in a very simple configuration format that is suitable for both programmers and non-programmers. It should be considered as the configuration file and structured data file formats for all new applications, particularly for those where the files are to be read, written, or modified by casual users.

遗弃M 2024-09-13 06:44:28

这实际上取决于配置文件的读取器/写入器是否是或可以是非程序员,或者是否具有严格的编程访问权限。

XML、JSON 等完全不适合人类使用,而 INI 格式对人类来说还算合理。

如果配置文件仅具有编程访问权限(程序员偶尔进行编辑),则任何受支持的格式(XML、JSON 或 INI)均适用。 INI 文件不适合结构化参数,其中 XML 和 JSON 可以支持数组、结构/对象等。

It really depends on whether the reader/writer of the configuration file is, or can be, a non-programmer human or if it has strictly programmatic access.

XML, JSON, etc., are totally unsuitable for human consumption, while the INI format is half-way reasonable for humans.

If the configuration file has only programmatic access (with occasional editing by a programmer), then any of the supported formats (XML, JSON, or INI) are suitable. The INI file is not suitable for structured parameters, where XML and JSON can support arrays, structs/objects, etc.

何止钟意 2024-09-13 06:44:28

根据Python官方文档,“配置功能的未来增强将
添加到 dictConfig()'。

所以任何可以与 dictconfig() 一起使用的配置文件都应该没问题。

According to Python official docs, 'future enhancements to configuration functionality will
be added to dictConfig()'.

So any config file which can be used with dictconfig() should be fine.

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