本文整理汇总了Python中attrdict.AttrDict.iteritems方法的典型用法代码示例。如果您正苦于以下问题:Python AttrDict.iteritems方法的具体用法?Python AttrDict.iteritems怎么用?Python AttrDict.iteritems使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类attrdict.AttrDict
的用法示例。
在下文中一共展示了AttrDict.iteritems方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_iteration_2
# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import iteritems [as 别名]
def test_iteration_2(self):
"""
Test the iteration methods (items, keys, values[, iteritems,
iterkeys, itervalues]).
"""
if not PY2: # Python2.6 doesn't have skipif/skipunless
return
from attrdict import AttrDict
empty = AttrDict()
adict = AttrDict({'foo': 'bar', 'lorem': 'ipsum', 'alpha': {
'beta': 1, 'bravo': empty}})
self.assertEqual(empty.items(), [])
self.assertEqual(empty.keys(), [])
self.assertEqual(empty.values(), [])
items = adict.items()
self.assertEqual(len(items), 3)
self.assertTrue(('foo', 'bar') in items)
self.assertTrue(('lorem', 'ipsum') in items)
self.assertTrue(('alpha', {'beta': 1, 'bravo': empty}) in items)
self.assertEqual(set(adict.keys()), set(['foo', 'lorem', 'alpha']))
values = adict.values()
self.assertEqual(len(values), 3)
self.assertTrue('bar' in values)
self.assertTrue('ipsum' in values)
self.assertTrue({'beta': 1, 'bravo': empty} in values)
# Iterator methods
iterator = empty.iteritems()
self.assertFalse(isinstance(iterator, list))
self.assertEqual(list(iterator), [])
iterator = empty.iterkeys()
self.assertFalse(isinstance(iterator, list))
self.assertEqual(list(iterator), [])
iterator = empty.itervalues()
self.assertFalse(isinstance(iterator, list))
self.assertEqual(list(iterator), [])
iterator = adict.iteritems()
self.assertFalse(isinstance(iterator, list))
self.assertEqual(list(iterator), adict.items())
iterator = adict.iterkeys()
self.assertFalse(isinstance(iterator, list))
self.assertEqual(list(iterator), adict.keys())
iterator = adict.itervalues()
self.assertFalse(isinstance(iterator, list))
self.assertEqual(list(iterator), adict.values())
示例2: __init__
# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import iteritems [as 别名]
def __init__(self, env='prod'):
self.env = os.environ.get('NETKI_ENV', env)
config_file = ConfigManager.find_config_file(self.env)
if not config_file or not os.path.isfile(config_file):
raise Exception('Cannot Find Config File app.%s.config' % self.env)
log.info('Loading Configuration [ENV: %s | FILE: %s]' % (self.env, config_file))
with open(config_file,'r') as file:
config = ConfigParser.ConfigParser()
config.readfp(file)
pre_transform_dict = AttrDict(config._sections)
for k,v in pre_transform_dict.iteritems():
if isinstance(v, dict):
is_changed = False
for key,value in v.items():
# Convert Bools
if value.strip().lower() == 'true':
v[key] = True
is_changed = True
continue
if value.strip().lower() == 'false':
v[key] = False
is_changed = True
continue
# Convert Floats
try:
if '.' in value:
v[key] = float(value)
is_changed = True
continue
except ValueError:
pass
# Convert Ints
try:
v[key] = int(value)
is_changed = True
continue
except ValueError:
pass
if is_changed:
pre_transform_dict.__setattr__(k,v)
self.config_dict = pre_transform_dict