我们有一个config.yaml
这样的文件:
darwin:
installer:
title: "%(product_name)s %(version)s"
filename: "%(brand_name)s-%(version)s"
以及格式化它的函数:
def format_context(config):
return {
"company_name": config['company_name'],
"product_name": config['product_name'],
"brand_name": config['brand_name'],
"title": config['darwin']['installer']['title'],
"filename": config['darwin']['installer']['filename'],
}
这里的目标是我们可以将值作为格式化字符串输入。现在我需要将字典 return byformat_context
转换为变量。
第一次尝试是使用locals()
:
context = format_context(config)
for k, v in context.iteritems():
locals()[k] = str(v) % context
但也许由于订单,我有时会KeyError
出错。此外,来自Python doc:
注意 不得修改本词典的内容;更改可能不会影响解释器使用的局部变量和自由变量的值。
所以,我转而使用exec
:
context = format_context(config)
for k, v in context.iteritems():
exec("%s = '%s'" % (k, str(v) % context))
它有效,但我想知道这是一个好方法吗?
请让我澄清为什么我要从该字典创建变量。我有一个函数来解析这个config.yaml
:
class BrandConfiguration(object):
"""
A brand configuration (directory)
"""
def __init__(self, directory):
self.dirname = directory
@property
def config(self):
"""
return configuration for a single brand
"""
with open(os.path.join(self.dirname, "config.yaml")) as fh:
return yaml.load(fh)
然后在一类中,我定义了一些变量:
- brand_config = self.brand_config_instance.config
- binary_name = brand_config['binary_name']
- major_version = brand_config['version']['major']
- minor_version = brand_config['version']['minor']
- patch_version = brand_config['version']['patch']
在另一个类(或另一个 Python 文件)中,我需要做同样的事情:
- brand_name, binary_name = config['brand_name'], config['binary_name']
- identifiers = [binary_name] + brand_name.split('.')
- identifiers.reverse()
- identifier = '.'.join(identifiers)
- major_version = config['version']['major']
- minor_version = config['version']['minor']
- patch_version = config['version']['patch']
- version = '.'.join(
- (
- str(major_version),
- str(minor_version),
- str(patch_version),
- build_number
- )
- )
由于我不想复制代码,因此我尝试将其全部存储在字典中并将其转换为变量。
您在哪里/如何尝试使用 format_context 返回的字典中的值?
假设在config.yaml
,你有这样的事情:
version:
major: 1
minor: 0
patch: 0
为 Windows 添加元数据时,而不是创建一些变量:
- brand_name, binary_name = config['brand_name'], config['binary_name']
- identifiers = [binary_name] + brand_name.split('.')
- identifiers.reverse()
- identifier = '.'.join(identifiers)
- major_version = config['version']['major']
- minor_version = config['version']['minor']
- patch_version = config['version']['patch']
- version = '.'.join(
- (
- str(major_version),
- str(minor_version),
- str(patch_version),
- build_number
- )
- )
现在我可以直接使用它了:
json_data['FixedFileInfo']['FileVersion']['Major'] = major_version
json_data['FixedFileInfo']['FileVersion']['Minor'] = minor_version
json_data['FixedFileInfo']['FileVersion']['Patch'] = patch_version
json_data['FixedFileInfo']['FileVersion']['Build'] = build_number
json_data['FixedFileInfo']['ProductVersion'] = \
json_data['FixedFileInfo']['FileVersion']
json_data['StringFileInfo']['CompanyName'] = company_name
json_data['StringFileInfo']['FileDescription'] = service_description
json_data['StringFileInfo']['LegalCopyright'] = legal_copyright
json_data['StringFileInfo']['ProductName'] = product_name
json_data['StringFileInfo']['ProductVersion'] = '.'.join(
(
str(major_version),
str(minor_version),
str(patch_version),
self.target.build_number
)
)