本文整理汇总了Python中configure.Configuration.from_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Configuration.from_dict方法的具体用法?Python Configuration.from_dict怎么用?Python Configuration.from_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类configure.Configuration
的用法示例。
在下文中一共展示了Configuration.from_dict方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_merge_error
# 需要导入模块: from configure import Configuration [as 别名]
# 或者: from configure.Configuration import from_dict [as 别名]
def test_merge_error():
src_dict = {
"d1": [1, 2, 3],
"s": "foo",
"a": [1, 2, 3],
"nested_dict": {
"d1": "ok",
"d": "ok"
}
}
other_dict = {
"d2": "foobar",
"s": "foobar",
"a": [3, 4, 5],
"nested_dict": {
"d2": "ok",
"d": { # ### raises ConfigError
"foo": "bar"
}
}
}
src = Configuration.from_dict(src_dict)
print_yaml("src", src)
other = Configuration.from_dict(other_dict)
print_yaml("other", other)
with pytest.raises(ConfigurationError):
merged = deepcopy(src).merge(other)
print_yaml("Merged", dict(merged))
logger.info("Merge raised configuration error")
示例2: test_merge
# 需要导入模块: from configure import Configuration [as 别名]
# 或者: from configure.Configuration import from_dict [as 别名]
def test_merge():
d1 = {
"d1": [1, 2, 3],
"s": "foo",
"a": [1, 2, 3],
"nested_dict": {
"d1": "ok",
"d": "ok"
}
}
c1 = Configuration.from_dict(d1)
print_yaml("d1", d1)
d2 = {
"d2": "foobar",
"s": "foobar",
"a": [3, 4, 5],
"nested_dict": {
"d2": "ok",
"d": {
"foo": "bar"
}
}
}
c2 = Configuration.from_dict(d2)
print_yaml("d2", d2)
c3 = c1.merge(c2)
print_yaml("Merged", dict(c3))
示例3: load
# 需要导入模块: from configure import Configuration [as 别名]
# 或者: from configure.Configuration import from_dict [as 别名]
def load(self):
if self._loaded:
return
self._file_list = []
self._invalid_paths = []
self._create_file_list(self._settings, self._file_list)
logger.info(
"\nList of files to load :\n - %s",
'\n - '.join([
x[len(self._config_dir) + 1:] for x in self._file_list
]))
if self._invalid_paths:
logger.info("invalid files :\n %s", '\n'.join(self._invalid_paths))
raise OptionError(self._invalid_paths)
all_cfg = Configuration.from_dict({})
for f in self._file_list:
cfg = load_configuration(f, self._config_dir)
try:
del cfg[DEFAULTS_TAG]
except KeyError:
pass
else:
logger.debug("Successfully removed default traces from %s" % f)
all_cfg.merge(cfg)
self._all_settings.merge(all_cfg)
self._loaded = True
示例4: test_array_extend
# 需要导入模块: from configure import Configuration [as 别名]
# 或者: from configure.Configuration import from_dict [as 别名]
def test_array_extend():
src_dict = {
"src": [11, 12, 13],
"merge": [100, 101, 102],
'nested_dict': {
'src': [111, 112, 113],
'merge': [1000, 1001, 1002]
}
}
other_dict = {
"other": [22, 22, 23],
"merge": [200, 202, 202],
'nested_dict': {
'other': [222, 222, 223],
'merge': [2000, 2002, 2002]
}
}
src = Configuration.from_dict(src_dict)
print_yaml("Src", src)
other = Configuration.from_dict(other_dict)
print_yaml("Other", other)
merged = deepcopy(src).merge(other)
print_yaml("merged", merged)
print_yaml("Src after merge", src)
print_yaml("Other after merge", other)
assert verify_key_val(src, src_dict, 'merge')
assert verify_key_val(src, src_dict, 'src')
assert verify_key_val(src, src_dict, 'nested_dict.merge')
assert verify_key_val(src, src_dict, 'nested_dict.src')
with pytest.raises(KeyError):
verify_key_val(src, src_dict, 'other')
assert verify_key_val(merged, src_dict, 'src')
assert verify_key_val(merged, other_dict, 'other')
assert merged['merge'] == src_dict['merge'] + other_dict['merge']
assert verify_key_val(merged, src_dict, 'nested_dict.src')
assert verify_key_val(merged, other_dict, 'nested_dict.other')
示例5: test_simple_merge
# 需要导入模块: from configure import Configuration [as 别名]
# 或者: from configure.Configuration import from_dict [as 别名]
def test_simple_merge():
src_dict = {
"merge": "src merge",
"src": 'src only',
'nested_dict': {
'merge': 'nested src merge',
'src': 'nested src only'
}
}
other_dict = {
"merge": "other merge",
"other": 'other only',
'nested_dict': {
'merge': 'nested other merge',
'other': 'nested other only'
}
}
src = Configuration.from_dict(src_dict)
print_yaml("Src", src)
other = Configuration.from_dict(other_dict)
print_yaml("Other", other)
merged = deepcopy(src).merge(other)
print_yaml("merged", merged)
print_yaml("Src after merge", src)
print_yaml("Other after merge", other)
assert verify_key_val(src, src_dict, 'merge')
assert verify_key_val(src, src_dict, 'src')
assert verify_key_val(src, src_dict, 'nested_dict.merge')
assert verify_key_val(src, src_dict, 'nested_dict.src')
with pytest.raises(KeyError):
verify_key_val(src, src_dict, 'other')
assert verify_key_val(merged, other_dict, 'merge')
assert verify_key_val(merged, src_dict, 'src')
assert verify_key_val(merged, other_dict, 'other')
return
示例6: load
# 需要导入模块: from configure import Configuration [as 别名]
# 或者: from configure.Configuration import from_dict [as 别名]
def load(self):
if self._loaded:
return
self._file_list = []
self._invalid_paths = []
self._create_file_list(self._settings, self._file_list)
logger.info(
"\nList of files to load :\n - %s",
'\n - '.join([
x[len(self._config_dir) + 1:] for x in self._file_list
]))
if self._invalid_paths:
logger.info("invalid files :\n %s", '\n'.join(self._invalid_paths))
raise OptionError(self._invalid_paths)
all_cfg = Configuration.from_dict({})
for f in self._file_list:
cfg = load_configuration(f, self._config_dir)
all_cfg.merge(cfg)
self._all_settings.merge(all_cfg)
self._loaded = True
示例7: test_monkey_patch_merge
# 需要导入模块: from configure import Configuration [as 别名]
# 或者: from configure.Configuration import from_dict [as 别名]
def test_monkey_patch_merge():
"""
Monkey patch configure so that merge will
append lists instead of replacing them
"""
src_dict = {
"d1": [1, 2, 3],
"s": "foo",
"a": [1, 2, 3],
"nested_dict": {
"d1": "ok",
"d": "ok"
}
}
src = Configuration.from_dict(src_dict)
print_yaml("Src", src)
other_dict = {
"d2": [1, 3, 5],
"s": "bar",
"a": [3, 2, 8],
"nested_dict": {
"d1": "ok",
"d": "ok"
}
}
src = Configuration.from_dict(src_dict)
print_yaml("Src", src)
other = Configuration.from_dict(other_dict)
print_yaml("Other", src)
merged = deepcopy(src).merge(other)
print_yaml("src", merged)
print_yaml("Merged", src)
assert merged['d1'] == [1, 2, 3]
assert merged['d2'] == [1, 3, 5]
assert merged['a'] == [1, 2, 3, 3, 2, 8]
assert merged['s'] == 'bar'
src = Configuration.from_string("""array: [1, 2, 3] """)
print_yaml("Original config", src)
other = Configuration.from_string("""array: [2, 3, 8, 9] """)
merged = deepcopy(src).merge(other)
print_yaml("Merged", merged)
assert merged['array'] == [1, 2, 3, 2, 3, 8, 9]
# ### test overwrite
overwrite = Configuration.from_string(""" array: !overwrite [0, 0, 0] """)
print_yaml("Overwrite", overwrite)
merged = deepcopy(src).merge(overwrite)
print_yaml('Merge with overwrite', merged)
assert merged['array'] == [0, 0, 0]
another_overwrite = Configuration.from_string(
"array: !overwrite [1, 1, 1] ")
print_yaml("Another Overwrite", another_overwrite)
merged = merged.merge(another_overwrite)
print_yaml('Merge with another overwrite', merged)
assert merged['array'] == [1, 1, 1]
# extend overwritten
print_yaml("Extending src", src)
merged = merged.merge(src)
print_yaml('Merge with src', merged)
assert merged['array'] == [1, 1, 1, 1, 2, 3]
from ksgen.tree import OrderedTree
tree = OrderedTree(delimiter='.')
tree.update(merged)
print_yaml("Merged", tree)