要实施什么类型的设置文件?

发布于 2024-09-10 13:34:09 字数 438 浏览 0 评论 0原文

我正在Python中制作自动存档/备份之类的东西(http://sourceforge.net/projects/冻结目录/)。到目前为止,它使用普通的文本文件来存储少量的设置(但不断增长)。我正在考虑将其更改为 XML 文件,以便它更加灵活/可扩展,从而从长远来看更易于使用。然而,当我问朋友时,他建议我将其保存在 python 文件中,例如 settings.py,并在需要时从需要设置的文件中使用“导入设置”。他声称它占用的空间更少,我只需要编写代码来写入设置文件,而不用担心读取和写入。

他的观点很有说服力,但让我想知道为什么其他大型程序没有使用他推荐的技术。

无论如何,我只是想知道你们的想法。我应该使用 XML、.py 还是其他格式?提前致谢。

I am making an auto-archiver/backup sort of thing in Python(http://sourceforge.net/projects/frozendirectory/). As of now it uses an ordinary text file to store its small amount of settings(but ever growing). I was thinking of changing it to an XML file so that it would be more flexible/scalable and thus easier to work with in the long run. However, when I asked a friend he recommended that I just keep it in a python file, such as settings.py and just use 'import settings' from the file that needs the settings when needed. He claimed that it takes less space and I would only have to write code to write to the settings file as opposed to worrying about both reading and writing.

His points were convincing but made me wonder why no other large programs used the technique he is recommending.

So anyways, I just wanted to know what you guys think. Should I go with XML, .py, or something else? Thanks in advance.

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

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

发布评论

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

评论(6

无可置疑 2024-09-17 13:34:09

JSON 是 XML 的一个很好的替代品,它也很容易用 Python 读取和编写。

JSON is a good alternative to XML, it is also easy to read and write in Python.

深居我梦 2024-09-17 13:34:09

我发现 ConfigObj 对此非常有用。它类似于 Windows 上常见的“ini”格式,但更灵活。

您可以设置配置标志的类型和约束。您还可以将默认设置与用户设置合并,即。用户的配置文件中可能缺少某些设置,这表明应使用默认设置。这使得升级非常无缝,除非您需要对现有设置进行一些神奇的重新解释。

RabbitVCS 有一个 ConfigObj 规范的示例是实现它的代码。

I find ConfigObj really useful for this. It's similar to the "ini" format commmon on Windows, but more flexible.

You can set types and constraints for configuration flags. You can also merge default settings with user settings, ie. some settings can be missing from the user's config file, indicating that the defaults should be used. This makes upgrades pretty seamless, unless you need some magical reinterpretation of existing settings.

RabbitVCS has an example of a ConfigObj specification and this is the code that implements it.

ㄖ落Θ余辉 2024-09-17 13:34:09

他的观点很有说服力,但也让我感到疑惑
为什么其他大型程序没有使用该技术
他正在推荐。

(Django使用settings.py)

对于我自己的程序...我通常要么使用像你提到的Python文件,要么使用ConfigParser

这取决于配置文件的受众是谁……Python 程序员还是非程序员。

His points were convincing but made me wonder
why no other large programs used the technique
he is recommending.

(Django uses settings.py)

For my own programs... I generally either use a Python file like you mentioned, or else I use ConfigParser.

It depends on who my audience is for the configuration file... a fellow python programmer or a non-programmer.

夏雨凉 2024-09-17 13:34:09

谁说大型程序不使用这种技术? :-) 当有意义的时候,他们会这样做。

为什么有使用 XML 的趋势?好吧,这是为了解决涉及所提供数据的验证的问题,确保多个数据生成器不必相互了解,确保多个读者将以相同的方式解释数据,将数据的性质封装在数据文件本身中。等等……

为什么你的朋友告诉你要避免它?因为对于您的应用程序来说可能太麻烦了:-)

Who says large programs don't use this technique? :-) They do, when it makes sense.

Why is there a trend to use XML? well that's to address issues involving validation of data being supplied, ensuring that multiple data generators don't have to learn about each other, ensuring that multiple readers will interpret the data identically, to encapsulate the nature of the data in the datafile itself... and on and on and on...

Why did your friend tell you to avoid it? because for your application it may be way too cumbersome :-)

一身骄傲 2024-09-17 13:34:09

你的朋友可能会给你最好的主意......取决于你的情况。它会很快,对你和程序来说都很容易,不需要调用额外的库(或者天堂不允许,为此安装一个库)。

反对对设置文件使用 import 的最佳论点是,假设“邪恶的黑客”可能会向文件中添加一些代码来干扰程序的工作。话又说回来,某个足够邪恶的人可以用其他东西替换你的整个程序,所以......取决于决心的水平。您需要看看这是否与您的计划目标有关。

如果您认为这不合适,我认为您可以将您的设置保留在纯文本文件中,如果它们足够简单(毕竟阅读 INI 样式设置只是一行,a-la

settings = dict(s.split('=',1) for s in open('somefile'))

如果您的结构更加结构化,则 JSON 或 YAML 应该另请参阅:

XML...但这对人类不友好。

You friend might have given you the best idea... depends on your situation. It will be fast, it will be easy on you and the program, won't require summoning additional libraries (or heaven forbid, install one just for that).

The best argument against using import for settings file is that an "evil hacker" hypothetically may add some code to the file to interfere with the work of your program. Then again, somebody evil enough can replace your whole program with something else so... depends on the level of determination. You need to see if that is concern for whatever your program is targeted.

If you consider that unsuitable, I think you can keep your settings in plain text file if they are simple enough (after all reading INI-style settings is a one liner, a-la

settings = dict(s.split('=',1) for s in open('somefile'))

If your structures are more structured, either JSON or YAML should do. See also:

XML you can do... but that ain't human-friendly.

半边脸i 2024-09-17 13:34:09

我想说这取决于复杂性以及所存储设置的对象或目的。

如果是我,我会使用以下内容作为基于需求的初始评估的基础,然后从那里开始。

A) 我的数据是否需要多个嵌套层?

  • 是的 ---> INI 文件JSONYAML

  • NO ---> INI 文件JSONYAML

B) 我是否需要存储值数组或其他复杂数据类型?

  • 是的 ---> INI 文件JSONYAML

  • NO ---> INI 文件、JSON、YAML

C) 我需要将参数映射到实时流吗?

  • 是的 ---> INI 文件JSONYAML

  • 否 ---> INI 文件JSONYAML

D) 我想在文件中插入或交叉引用键值吗?

  • 是的 ---> INI 文件JSONYAML

  • NO ---> INI 文件JSONYAML

最后,

INI 文件应该很简单,插入参数很容易,但很难获得更多当你需要的时候很复杂。

JSON 各方面都很好,对嵌套键值或数组对象以及数据类型支持有很好的支持。适合 Web 开发或 JS 对象集成。插值困难,很难在不替换整个对象的情况下主动调整嵌套键值。

YAML 也很好,我真的很喜欢对 python 对象和模块的内部规范的支持。有些数据类型可能有点奇怪,或者不适用于某些版本的 python。数组的嵌套有点奇怪,并且具有大量键的嵌套键值对象的概述可能很乏味。

您的选择取决于您的需求,但通常我通常会远离 xml,主要是因为键无法以与 JSON 或 YAML 相同的方式提供信息。

更新:

我刚刚查看 YAML 文档,我遇到了以下信息,我我想你可能会发现它有用。

因此,YAML 可以被视为 JSON 的自然超集,提供改进的人类可读性和更完整的信息
化模型。实践中也是如此;每个 JSON 文件也是一个有效的 YAML 文件。这使得从 JSON 迁移变得容易
如果/当需要附加功能时,转换为 YAML

很酷吧?不管怎样,考虑到这些信息,我可能会默认使用 JSON,然后如果我需要转到 YAML。

I would say that it depends on the complexity and the object or purpose of the settings being stored.

If it were me - I would use the following as the basis for an initial needs based assessment and go from there.

A) Does my data require multiple nested layers?

  • YES ---> INI File, JSON, YAML

  • NO ---> INI File, JSON, YAML

B) Do I need to store value arrays or other complex datatypes?

  • YES ---> INI File, JSON, YAML

  • NO ---> INI File, JSON, YAML

C) Will I need to map the arguments to a live stream?

  • YES ---> INI File, JSON,YAML

  • No ---> INI File, JSON, YAML

D) Do I want to interpolate or cross-reference key-values within the file?

  • YES ---> INI File, JSON, YAML

  • NO ---> INI File, JSON, YAML

In the end,

INI Files are meant to be simple and interpolating arguments is easy but it is difficult to get more complex when you need to.

JSON good all around, nice support for nested key-value or array objects and data type support. Good for web development or JS object integration. Interpolation difficult, hard to actively adjust nested key values without replacing the whole object.

YAML is good too, I really like the support for internal specification of python objects and modules. Some data types can be a bit weird or are not available for certain versions of python. Nesting of arrays is a little strange and nested key-value objects with a lot of keys can be tedious to outline.

What you choose depends on what your requirements are but as a rule I usually stay away from xml mainly because the keys cant provide information in the same way as JSON or YAML.

UPDATES:

I was just reviewing the YAML docs and I ran across the following information and I thought you might find it useful.

YAML can therefore be viewed as a natural superset of JSON, offering improved human readability and a more complete inform-
ation model. This is also the case in practice; every JSON file is also a valid YAML file. This makes it easy to migrate from JSON
to YAML if/when the additional features are required

Pretty cool huh? Anyway, given this information I would likely default to JSON and then if I needed to go over to YAML.

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