本文整理汇总了Python中conf_d.Configuration.get方法的典型用法代码示例。如果您正苦于以下问题:Python Configuration.get方法的具体用法?Python Configuration.get怎么用?Python Configuration.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类conf_d.Configuration
的用法示例。
在下文中一共展示了Configuration.get方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get
# 需要导入模块: from conf_d import Configuration [as 别名]
# 或者: from conf_d.Configuration import get [as 别名]
def test_get(self):
conf = Configuration(
name='multiple_sections',
path='./data/multiple_sections.ini',
)
self.assertEqual({}, conf.get('multiple_sections'))
self.assertEqual({'til': 'brooklyn', 'no': 'sleep'}, conf.get('derp'))
self.assertEqual('brooklyn', conf.get('derp', 'til'))
self.assertEqual(None, conf.get('derp', 'missing_key'))
self.assertEqual('1', conf.get('derp', 'missing_key_with_default', '1'))
示例2: test_parser
# 需要导入模块: from conf_d import Configuration [as 别名]
# 或者: from conf_d.Configuration import get [as 别名]
def test_parser(self):
def safe_cast(val):
try:
return int(val)
except ValueError:
return val
def all_as_bool(config):
for key in config:
config[key] = bool(config[key])
return config
def all_as_int(config):
for key in config:
try:
config[key] = int(config[key])
except ValueError:
pass
return config
conf = Configuration(
name='multiple_sections',
path='./data/multiple_sections.ini',
confd_path='./data/conf.d',
main_defaults={'main_key': 'main_value'},
section_defaults={'sleep': '2', 'wait': '30'},
main_parser=all_as_bool
)
self.assertEqual({'main_key': True}, conf.get('multiple_sections'))
self.assertEqual(True, conf.get('multiple_sections', 'main_key'))
self.assertEqual('1', conf.get('another/conf', 'sleep'))
conf = Configuration(
name='multiple_sections',
path='./data/multiple_sections.ini',
confd_path='./data/conf.d',
main_defaults={'main_key': 'main_value'},
section_defaults={'sleep': '2', 'wait': '30'},
section_parser=all_as_bool
)
self.assertEqual({'main_key': 'main_value'}, conf.get('multiple_sections'))
self.assertEqual('main_value', conf.get('multiple_sections', 'main_key'))
self.assertEqual(1, conf.get('another/conf', 'sleep'))
conf = Configuration(
name='multiple_sections',
path='./data/multiple_sections.ini',
confd_path='./data/conf.d',
main_defaults={'main_key': 'main_value'},
section_defaults={'sleep': '2', 'wait': '30'},
main_parser=all_as_bool,
section_parser=all_as_int
)
self.assertEqual({'main_key': True}, conf.get('multiple_sections'))
self.assertEqual(True, conf.get('multiple_sections', 'main_key'))
self.assertEqual(None, conf.get('derp', 'missing_key'))
self.assertEqual(1, conf.get('another/conf', 'sleep'))
示例3: test_readme
# 需要导入模块: from conf_d import Configuration [as 别名]
# 或者: from conf_d.Configuration import get [as 别名]
def test_readme(self):
def digitize(config):
for key in config:
if not config[key].isdigit():
try:
config[key] = float(config[key])
except ValueError:
pass
else:
try:
config[key] = int(config[key])
except ValueError:
pass
return config
conf = Configuration(
name='derp',
path='./data/conf',
main_defaults={
'no': 'one',
'expected': 'the spanish inquisition',
'cats': '1',
},
section_parser=digitize
)
expected = {
'sections': {'herp': {'sleep': 1, 'wait': 5.0, 'timeout': 'seventy'}},
'derp': {'expected': 'the spanish inquisition', 'til': 'brooklyn', 'cats': '1', 'no': 'sleep'}
}
self.assertEqual(expected, conf.raw())
actual = conf.get(section='derp', key='no', default="jumping")
self.assertEqual('sleep', actual)
actual = conf.get(section='derp', key='til')
self.assertEqual('brooklyn', actual)
actual = conf.get(section='derp', key='cats')
self.assertEqual('1', actual)
actual = conf.get(section='derp', key='dogs')
self.assertEqual(None, actual)
actual = conf.get(section='herp', key='sleep')
self.assertEqual(1, actual)
actual = conf.get(section='herp', key='wait')
self.assertEqual(5.0, actual)
actual = conf.get(section='herp', key='timeout')
self.assertEqual('seventy', actual)
actual = conf.has(section='derp')
self.assertEqual(True, actual)
actual = conf.has(section='derp', key='no')
self.assertEqual(True, actual)
expected = {
'sections': {
'herp': {
'sleep': 1,
'wait': 5.0,
'timeout': 'seventy'
}
},
'derp': {
'expected': 'the spanish inquisition',
'til': 'brooklyn',
'cats': '1',
'no': 'sleep'
}
}
self.assertEqual(expected, conf.raw())
示例4: test_confd
# 需要导入模块: from conf_d import Configuration [as 别名]
# 或者: from conf_d.Configuration import get [as 别名]
def test_confd(self):
conf = Configuration(
name='multiple_sections',
path='./data/multiple_sections.ini',
confd_path='./data/conf.d',
main_defaults={'main_key': 'main_value'}
)
self.assertEqual({'main_key': 'main_value'}, conf.get('multiple_sections'))
self.assertEqual({'til': 'brooklyn', 'no': 'sleep'}, conf.get('derp'))
self.assertEqual('main_value', conf.get('multiple_sections', 'main_key'))
self.assertEqual('brooklyn', conf.get('derp', 'til'))
self.assertEqual(None, conf.get('derp', 'missing_key'))
self.assertEqual(None, conf.get('derp', 'main_key'))
self.assertEqual('1', conf.get('derp', 'missing_key_with_default', '1'))
expected = {
'multiple_sections': {'main_key': 'main_value'},
'sections': {
'another/conf': {'sleep': '1', 'wait': '15'},
'derp': {'no': 'sleep', 'til': 'brooklyn'},
'section': {'key': 'value'},
'test': {'conf': 'path/to/another/conf', 'sleep': '15'}
}
}
self.assertEqual(expected, conf.raw())
conf = Configuration(
name='multiple_sections',
path='./data/multiple_sections.ini',
confd_path='./data/conf.d',
section_defaults={'sleep': '2', 'wait': '30'}
)
self.assertEqual({}, conf.get('multiple_sections'))
self.assertEqual({'no': 'sleep', 'sleep': '2', 'til': 'brooklyn', 'wait': '30'}, conf.get('derp'))
self.assertEqual(None, conf.get('multiple_sections', 'main_key'))
self.assertEqual('brooklyn', conf.get('derp', 'til'))
self.assertEqual(None, conf.get('derp', 'missing_key'))
self.assertEqual(None, conf.get('derp', 'main_key'))
self.assertEqual('1', conf.get('derp', 'missing_key_with_default', '1'))
expected = {
'multiple_sections': {},
'sections': {
'another/conf': {'sleep': '1', 'wait': '15'},
'derp': {'no': 'sleep', 'sleep': '2', 'til': 'brooklyn', 'wait': '30'},
'section': {'key': 'value', 'sleep': '2', 'wait': '30'},
'test': {'conf': 'path/to/another/conf', 'sleep': '15', 'wait': '30'}
}
}
self.assertEqual(expected, conf.raw())
conf = Configuration(
name='multiple_sections',
path='./data/multiple_sections.ini',
confd_path='./data/conf.d',
main_defaults={'main_key': 'main_value'},
section_defaults={'sleep': '2', 'wait': '30'}
)
self.assertEqual({'main_key': 'main_value'}, conf.get('multiple_sections'))
self.assertEqual({'no': 'sleep', 'sleep': '2', 'til': 'brooklyn', 'wait': '30'}, conf.get('derp'))
self.assertEqual('main_value', conf.get('multiple_sections', 'main_key'))
self.assertEqual('brooklyn', conf.get('derp', 'til'))
self.assertEqual(None, conf.get('derp', 'missing_key'))
self.assertEqual(None, conf.get('derp', 'main_key'))
self.assertEqual('1', conf.get('derp', 'missing_key_with_default', '1'))
expected = {
'multiple_sections': {'main_key': 'main_value'},
'sections': {
'another/conf': {'sleep': '1', 'wait': '15'},
'derp': {'no': 'sleep', 'sleep': '2', 'til': 'brooklyn', 'wait': '30'},
'section': {'key': 'value', 'sleep': '2', 'wait': '30'},
'test': {'conf': 'path/to/another/conf', 'sleep': '15', 'wait': '30'}
}
}
self.assertEqual(expected, conf.raw())
conf = Configuration(
name='multiple_sections',
path='./data/multiple_sections.ini',
confd_path='./data/conf.d',
conf_ext='conf',
main_defaults={'main_key': 'main_value'},
section_defaults={'sleep': '2', 'wait': '30'}
)
self.assertEqual({'main_key': 'main_value'}, conf.get('multiple_sections'))
self.assertEqual({'no': 'sleep', 'sleep': '2', 'til': 'brooklyn', 'wait': '30'}, conf.get('derp'))
self.assertEqual('main_value', conf.get('multiple_sections', 'main_key'))
self.assertEqual('brooklyn', conf.get('derp', 'til'))
self.assertEqual(None, conf.get('derp', 'missing_key'))
self.assertEqual(None, conf.get('derp', 'main_key'))
self.assertEqual('1', conf.get('derp', 'missing_key_with_default', '1'))
expected = {
'multiple_sections': {'main_key': 'main_value'},
'sections': {
#.........这里部分代码省略.........