本文整理汇总了Python中attrdict.AttrDict.bravo方法的典型用法代码示例。如果您正苦于以下问题:Python AttrDict.bravo方法的具体用法?Python AttrDict.bravo怎么用?Python AttrDict.bravo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类attrdict.AttrDict
的用法示例。
在下文中一共展示了AttrDict.bravo方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_default_dict
# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import bravo [as 别名]
def test_default_dict(self):
"""
test attrdict's defaultdict support.
"""
from attrdict import AttrDict
self.assertRaises(KeyError, lambda: AttrDict()['foo'])
self.assertRaises(AttributeError, lambda: AttrDict().foo)
adict = AttrDict(default_factory=lambda: ('foo', 'bar', 'baz'))
self.assertEqual(adict['foo'], ('foo', 'bar', 'baz'))
self.assertEqual(adict('bar'), ('foo', 'bar', 'baz'))
self.assertEqual(adict.baz, ('foo', 'bar', 'baz'))
self.assertEqual(adict.get('lorem'), None)
self.assertEqual(adict.get('ipsum', 'alpha'), 'alpha')
# make sure this doesn't break access
adict.bravo = 'charlie'
self.assertEqual(adict['bravo'], 'charlie')
self.assertEqual(adict('bravo'), 'charlie')
self.assertEqual(adict.bravo, 'charlie')
self.assertEqual(adict.get('bravo'), 'charlie')
self.assertEqual(adict.get('bravo', 'alpha'), 'charlie')
示例2: test_default_dict_pass_key
# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import bravo [as 别名]
def test_default_dict_pass_key(self):
"""
test attrdict's defaultdict support.
"""
from attrdict import AttrDict
adict = AttrDict(default_factory=lambda foo: (foo, 'bar', 'baz'),
pass_key=True)
self.assertEqual(adict['foo'], ('foo', 'bar', 'baz'))
self.assertEqual(adict('bar'), ('bar', 'bar', 'baz'))
self.assertEqual(adict.baz, ('baz', 'bar', 'baz'))
self.assertEqual(adict.get('lorem'), None)
self.assertEqual(adict.get('ipsum', 'alpha'), 'alpha')
# make sure this doesn't break access
adict.bravo = 'charlie'
self.assertEqual(adict['bravo'], 'charlie')
self.assertEqual(adict('bravo'), 'charlie')
self.assertEqual(adict.bravo, 'charlie')
self.assertEqual(adict.get('bravo'), 'charlie')
self.assertEqual(adict.get('bravo', 'alpha'), 'charlie')
示例3: test_default_dict_pass_key
# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import bravo [as 别名]
def test_default_dict_pass_key(self):
"""
test attrdict's defaultdict support.
"""
from attrdict import AttrDict
adict = AttrDict(default_factory=lambda foo: (foo, 'bar', 'baz'),
pass_key=True)
self.assertEqual(adict['foo'], ('foo', 'bar', 'baz'))
self.assertEqual(adict('bar'), ('bar', 'bar', 'baz'))
self.assertEqual(adict.baz, ('baz', 'bar', 'baz'))
self.assertEqual(adict.get('lorem'), None)
self.assertEqual(adict.get('ipsum', 'alpha'), 'alpha')
# make sure this doesn't break access
adict.bravo = 'charlie'
self.assertEqual(adict['bravo'], 'charlie')
self.assertEqual(adict('bravo'), 'charlie')
self.assertEqual(adict.bravo, 'charlie')
self.assertEqual(adict.get('bravo'), 'charlie')
self.assertEqual(adict.get('bravo', 'alpha'), 'charlie')
# make sure missing doesn't happen on hidden attributes
adict = AttrDict(default_factory=lambda: 'default')
self.assertEqual(adict.works, 'default')
self.assertRaises(AttributeError, lambda: adict._hidden)
self.assertEqual(adict.get('_hidden'), None)
self.assertRaises(AttributeError, lambda: adict.__magic__)
self.assertEqual(adict.get('__magic__'), None)
# but this should work
self.assertEqual(adict('_this_is_fine'), 'default')