python 2.7 中的 ConfigParser VS SafeConfigParser
ConfigParser VS SafeConfigParser in python 2.7
ConfigParser
和SafeConfigParser
有什么区别?确切地说,为什么后者更安全? 'unsafe' 与 ConfigParser
有什么关系?我知道 SafeConfigParser
继承了 ConfigParser
,它有什么不同?
根据https://docs.python.org/2/library/configparser.html:
在核心功能之上,SafeConfigParser 支持插值。这意味着值可以包含引用同一部分中其他值的格式字符串,或特殊 DEFAULT 部分中的值。可以在初始化时提供额外的默认值。
SafeConfigParser
是...
Derived class of ConfigParser that implements a
more-sane variant of the magical interpolation feature. This
implementation is more predictable as well. New applications should
prefer this version if they don’t need to be compatible with older
versions of Python.
我觉得SafeConfigParser
好像没有考虑Python版本兼容性。 ConfigParser
在Python3版本中也存在,但是SafeConfigParser
不存在。确切地说,SafeConfigParser
已重命名为 ConfigParser
,而 ConfigParser
已从 3.2
中删除。看到这个 question.
所以我认为 ConfigParser
和 SafeConfigParser
之间的区别在于可用性和版本兼容性。
更新:
SafeConfigParser
只是比 ConfigParser
更安全。这并不是说 ConfigParser
不安全。我试图弄清楚什么更安全。它支持 magical interpolation 更合理的变体,并且比 ConfigParser
.
更严格
那么为什么 SafeConfigParser 是安全的?
答案是SafeConfigParser
更严格。 strict 的示例在@olinox14 的回答中。
事实上 SafeConfigParser
已成为 Python 3 中的默认值 ConfigParser
并不一定意味着您需要将它们分开。
最终, SafeConfigParser
更加严格。并且推荐使用SafeConfigParser
如果该部分不存在,则 SafeConfigParser implements a different set(section, option, value)
method which will raise a NoSectionError,如果 value
不是字符串,则 TypeError
。
这样可以更好地控制解析器的行为,example 来自文档:
try:
config.set(section2, option, config.get(section1, option, 1))
except ConfigParser.NoSectionError:
# Create non-existent section
config.add_section(section2)
opt_move(config, section1, section2, option)
else:
config.remove_option(section1, option)
From documentation: It also support interpolation. This means values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Additional defaults can be provided on initialization.
更新
我刚刚检查了 source code of the SafeConfigParser
, and even if it is true that ConfigParser
also allows interpolation, SafeConfigParser
provides an updated version of it that documentation 描述为 魔法插值功能的更理智和更可预测的变体 。
例如,如果引用错误或“%”字符后出现语法错误,它将引发 InterpolationSyntaxError
。
更新 2
这对于精确 SafeConfigParser
class has been renamed to ConfigParser in Python 3.2 可能很有用。如果您想知道在 python 2.7 中应该使用 SafeConfigParser
还是 ConfigParser
中的哪一个,请使用第一个(除非您有非常具体的理由使用第二个)
您还可以通过以下方式更轻松地过渡到 python 3+,(which should happen soon):
from ConfigParser import SafeConfigParser as ConfigParser
ConfigParser
和SafeConfigParser
有什么区别?确切地说,为什么后者更安全? 'unsafe' 与 ConfigParser
有什么关系?我知道 SafeConfigParser
继承了 ConfigParser
,它有什么不同?
根据https://docs.python.org/2/library/configparser.html:
在核心功能之上,SafeConfigParser 支持插值。这意味着值可以包含引用同一部分中其他值的格式字符串,或特殊 DEFAULT 部分中的值。可以在初始化时提供额外的默认值。
SafeConfigParser
是...
Derived class of ConfigParser that implements a more-sane variant of the magical interpolation feature. This implementation is more predictable as well. New applications should prefer this version if they don’t need to be compatible with older versions of Python.
我觉得SafeConfigParser
好像没有考虑Python版本兼容性。 ConfigParser
在Python3版本中也存在,但是SafeConfigParser
不存在。确切地说,SafeConfigParser
已重命名为 ConfigParser
,而 ConfigParser
已从 3.2
中删除。看到这个 question.
所以我认为 ConfigParser
和 SafeConfigParser
之间的区别在于可用性和版本兼容性。
更新:
SafeConfigParser
只是比 ConfigParser
更安全。这并不是说 ConfigParser
不安全。我试图弄清楚什么更安全。它支持 magical interpolation 更合理的变体,并且比 ConfigParser
.
那么为什么 SafeConfigParser 是安全的?
答案是SafeConfigParser
更严格。 strict 的示例在@olinox14 的回答中。
事实上 SafeConfigParser
已成为 Python 3 中的默认值 ConfigParser
并不一定意味着您需要将它们分开。
最终, SafeConfigParser
更加严格。并且推荐使用SafeConfigParser
如果该部分不存在,则 SafeConfigParser implements a different set(section, option, value)
method which will raise a NoSectionError,如果 value
不是字符串,则 TypeError
。
这样可以更好地控制解析器的行为,example 来自文档:
try:
config.set(section2, option, config.get(section1, option, 1))
except ConfigParser.NoSectionError:
# Create non-existent section
config.add_section(section2)
opt_move(config, section1, section2, option)
else:
config.remove_option(section1, option)
From documentation: It also support interpolation. This means values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Additional defaults can be provided on initialization.
更新
我刚刚检查了 source code of the SafeConfigParser
, and even if it is true that ConfigParser
also allows interpolation, SafeConfigParser
provides an updated version of it that documentation 描述为 魔法插值功能的更理智和更可预测的变体 。
例如,如果引用错误或“%”字符后出现语法错误,它将引发 InterpolationSyntaxError
。
更新 2
这对于精确 SafeConfigParser
class has been renamed to ConfigParser in Python 3.2 可能很有用。如果您想知道在 python 2.7 中应该使用 SafeConfigParser
还是 ConfigParser
中的哪一个,请使用第一个(除非您有非常具体的理由使用第二个)
您还可以通过以下方式更轻松地过渡到 python 3+,(which should happen soon):
from ConfigParser import SafeConfigParser as ConfigParser