如何设置 SafeConfigParser 的默认值?

发布于 2024-11-09 12:19:30 字数 745 浏览 0 评论 0原文

我有一个配置文件如下:

[job]
mailto=bob
logFile=blahDeBlah.txt

我想使用 SafeConfigParser 读取选项:

values = {} 
config = ConfigParser.SafeConfigParser()
try:
    config.read(configFile)
    jobSection = 'job'

    values['mailto'] = config.get( jobSection, 'mailto' )
    values['logFile'] = config.get( jobSection, 'logFile' )
    # it is not there
    values['nothingThere'] = config.get( jobSection, 'nothingThere' )
.... # rest of code

最后一行当然会抛出错误。如何为 config.get() 方法指定默认值?

再说一次,如果我有一个如下所示的选项文件:

[job1]
mailto=bob
logFile=blahDeBlah.txt

[job2]
mailto=bob
logFile=blahDeBlah.txt

似乎没有办法为 job1 指定与 job2 部分中的默认选项不同的默认选项。

I have a config file as follows:

[job]
mailto=bob
logFile=blahDeBlah.txt

I want to read the options using SafeConfigParser:

values = {} 
config = ConfigParser.SafeConfigParser()
try:
    config.read(configFile)
    jobSection = 'job'

    values['mailto'] = config.get( jobSection, 'mailto' )
    values['logFile'] = config.get( jobSection, 'logFile' )
    # it is not there
    values['nothingThere'] = config.get( jobSection, 'nothingThere' )
.... # rest of code

The last line of course will throw an error. How can I specify a default value for the config.get() method?

Then again, if I have an options file as follows:

[job1]
mailto=bob
logFile=blahDeBlah.txt

[job2]
mailto=bob
logFile=blahDeBlah.txt

There seems to be no way to specify default options for job1 different from the default options in section job2.

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

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

发布评论

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

评论(4

我也只是我 2024-11-16 12:19:30

您还可以使用默认的“.ini”文件并在实际配置文件之前读取它。

default.ini:

[job1]
mailto=jack
logfile=default.log

[job2]
mailto=john
logfile=default.log

config.ini:

[job1]
mailto=sparrow
logfile=blah.log

[job2]
logfile=blah2.log

解析:

import ConfigParser  # For Python 3 use the configparser module instead (all lowercase)

config = ConfigParser.SafeConfigParser()
config.read('default.ini')
config.read('config.ini')

print config.get('job1', 'mailto')
# -> sparrow (from config.ini)

print config.get('job1', 'logfile')
# -> blah.log (from config.ini)

print config.get('job2', 'mailto')
# -> john (from default.ini)

print config.get('job2', 'logfile')
# -> blah2.log (from config.ini)

You can also use a default ".ini" file and read it before your actual config file.

default.ini:

[job1]
mailto=jack
logfile=default.log

[job2]
mailto=john
logfile=default.log

config.ini:

[job1]
mailto=sparrow
logfile=blah.log

[job2]
logfile=blah2.log

parsing:

import ConfigParser  # For Python 3 use the configparser module instead (all lowercase)

config = ConfigParser.SafeConfigParser()
config.read('default.ini')
config.read('config.ini')

print config.get('job1', 'mailto')
# -> sparrow (from config.ini)

print config.get('job1', 'logfile')
# -> blah.log (from config.ini)

print config.get('job2', 'mailto')
# -> john (from default.ini)

print config.get('job2', 'logfile')
# -> blah2.log (from config.ini)
再见回来 2024-11-16 12:19:30

使用构造函数的 defaults 参数:

# class ConfigParser.SafeConfigParser([defaults[, dict_type]]) 
#
config = ConfigParser.SafeConfigParser({'nothingThere': 'lalalalala'})
...
...
# If the job section has no "nothingThere", "lalalalala" will be returned
# 
config.get(jobSection, 'nothingThere')

Use the defaults parameter to the constructor:

# class ConfigParser.SafeConfigParser([defaults[, dict_type]]) 
#
config = ConfigParser.SafeConfigParser({'nothingThere': 'lalalalala'})
...
...
# If the job section has no "nothingThere", "lalalalala" will be returned
# 
config.get(jobSection, 'nothingThere')
终止放荡 2024-11-16 12:19:30

在 Python 3 中,您可以向 get() 方法提供后备值,如下所示:

values['nothingThere'] = config.get('job', 'nothingThere', fallback=0)
print(values['nothingThere'])
# -> 0

In Python 3 you can provide a fallback value to the get() method as follows:

values['nothingThere'] = config.get('job', 'nothingThere', fallback=0)
print(values['nothingThere'])
# -> 0
夏日落 2024-11-16 12:19:30

您可以使用 [DEFAULT] 部分为尚未在任何其他部分中定义的属性设置默认值。

例如

[DEFAULT]
checkout_root: /data/workspace

[pingpong]
name: Ping Pong App
checkout_root: /home/pingpong
src: %(checkout_root)s/src

[dingdong]
name: Ding Dong App
src: %(checkout_root)s/dingdong_src

,对于叮叮应用,src 的值为 /data/workspace/dingdong_src

You can use the [DEFAULT] section to set default values for the properties you haven't defined in any other section(s).

E.g.

[DEFAULT]
checkout_root: /data/workspace

[pingpong]
name: Ping Pong App
checkout_root: /home/pingpong
src: %(checkout_root)s/src

[dingdong]
name: Ding Dong App
src: %(checkout_root)s/dingdong_src

For the ding ding app, the value of src will be /data/workspace/dingdong_src

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