亲宝软件园·资讯

展开

configparser中默认值设定

Believer007 人气:2

configparser中默认值的设定

在做某一个项目时,在读配置文件中,当出现配置文件中没有对应项目时,如果要设置默认值,以前的做法是如下的:

try:
    apple = config.get(section, 'apple')
except NoSectionError, NoOptionError:
    apple = None

但当存在很多配置时,这种写法太糟糕

幸好,在Configparser.get()函数中有一个vars()的参数,可以自定义;注:只能用ConfigParser.ConfigParser;rawconfigparser是不支持的

解决方案

1、定义函数:

class DefaultOption(dict):
    def __init__(self, config, section, **kv):
        self._config = config
        self._section = section
        dict.__init__(self, **kv)
    def items(self):
        _items = []
        for option in self:
            if not self._config.has_option(self._section, option):
                _items.append((option, self[option]))
            else:
                value_in_config = self._config.get(self._section, option)
                _items.append((option, value_in_config))
        return _items

2、使用

def read_config(section, location):
    config = configparser.ConfigParser()
    config.read(location)
    apple = config.get(section, 'apple',
                       vars=DefaultOption(config, section, apple=None))
    pear = config.get(section, 'pear',
                      vars=DefaultOption(config, section, pear=None))
    banana = config.get(section, 'banana',
                        vars=DefaultOption(config, section, banana=None))
    return apple, pear, banana

这样就很好解决了读取配置文件时没有option时自动取默认值,而不是用rasie的方式取默认值

此方案来之stackoverflow

使用configparser的注意事项

以这个非常简单的典型配置文件为例:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no

1、config parser 操作跟dict 类似,在数据存取方法基本一致

>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
'50022'
>>> for key in config['bitbucket.org']: print(key)
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'

2、默认配置项[DEFAULT]section 的默认参数会作用于其他Sections

3、数据类型

4、获取参数值方法 get()

5、参数分隔符可以使用‘=’或‘:’(默认)

6、可以使用‘#’或‘;’(默认)添加备注或说明 

[Simple Values]
key=value
spaces in keys=allowed
spaces in values=allowed as well
spaces around the delimiter = obviously
you can also use : to delimit keys from values
[All Values Are Strings]
values like this: 1000000
or this: 3.14159265359
are they treated as numbers? : no
integers, floats and booleans are held as: strings
can use the API to get converted values directly: true
[Multiline Values]
chorus: I'm a lumberjack, and I'm okay
    I sleep all night and I work all day
[No Values]
key_without_value
empty string value here =
[You can use comments]
# like this
; or this
# By default only in an empty line.
# Inline comments can be harmful because they prevent users
# from using the delimiting characters as parts of values.
# That being said, this can be customized.
    [Sections Can Be Indented]
        can_values_be_as_well = True
        does_that_mean_anything_special = False
        purpose = formatting for readability
        multiline_values = are
            handled just fine as
            long as they are indented
            deeper than the first line
            of a value
        # Did I mention we can indent comments, too?

7、写配置

常见做法:

config.write(open('example.ini', 'w'))

合理做法:

with open('example.ini', 'w') as configfile:
    config.write(configfile)

注意要点

1、ConfigParser 在get 时会自动过滤掉‘#’或‘;‘注释的行(内容);

2、在ConfigParser write之后,配置文本如果有大写字母’PRODUCT’会变为小写字母’product’,并不影响配置的正确读写。 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论