用于编辑配置文件的Python@property
使用@property/@variable.setter从/在配置文件中获取/设置条目会是pythonic吗?例如,文件可以是xml、yaml等,我可能想写:用于编辑配置文件的Python@property,python,python-3.x,Python,Python 3.x,使用@property/@variable.setter从/在配置文件中获取/设置条目会是pythonic吗?例如,文件可以是xml、yaml等,我可能想写: config_file.port = 80 Python手册指出: INI文件的结构将在下一节中介绍。 本质上,该文件由多个部分组成,每个部分都包含 具有值的键配置解析器类可以读取和写入此类文件。 让我们从以编程方式创建上述配置文件开始 将值添加到configparse: >>> import configparser
config_file.port = 80
Python手册指出:
INI
文件的结构将在下一节中介绍。
本质上,该文件由多个部分组成,每个部分都包含
具有值的键<代码>配置解析器类可以读取和写入此类文件。
让我们从以编程方式创建上述配置文件开始
将值添加到configparse
:
>>> import configparser
>>> config = configparser.ConfigParser()
>>> config['DEFAULT'] = {'ServerAliveInterval': '45',
... 'Compression': 'yes',
... 'CompressionLevel': '9'}
>>> config['bitbucket.org'] = {}
>>> config['bitbucket.org']['User'] = 'hg'
>>> config['topsecret.server.com'] = {}
>>> topsecret = config['topsecret.server.com']
>>> topsecret['Port'] = '50022' # mutates the parser
>>> topsecret['ForwardX11'] = 'no' # same here
>>> config['DEFAULT']['ForwardX11'] = 'yes'
>>> with open('example.ini', 'w') as configfile:
... config.write(configfile)
>>> 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
>>> conf
从configparse
检索值:
>>> import configparser
>>> config = configparser.ConfigParser()
>>> config['DEFAULT'] = {'ServerAliveInterval': '45',
... 'Compression': 'yes',
... 'CompressionLevel': '9'}
>>> config['bitbucket.org'] = {}
>>> config['bitbucket.org']['User'] = 'hg'
>>> config['topsecret.server.com'] = {}
>>> topsecret = config['topsecret.server.com']
>>> topsecret['Port'] = '50022' # mutates the parser
>>> topsecret['ForwardX11'] = 'no' # same here
>>> config['DEFAULT']['ForwardX11'] = 'yes'
>>> with open('example.ini', 'w') as configfile:
... config.write(configfile)
>>> 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
>>> conf
它是语言的一部分,由你决定是否使用它。它适合你的需要吗?什么是
pythonnesque
——听起来有点主观。我当然会查看configparser,但我应该给出更多细节。实际上,我正在编写测试自动化来测试用C#编写的Windows服务。配置文件已经给了我。我想设置各种值以测试各种情况。虽然我已经使用decorator在代码的其他部分设置了属性,但我有这些设置配置(即写入文件)的方法。我只是想啊,我可以用@property来做这个。