当前位置: 首页>>代码示例>>Python>>正文


Python AttrDict.get方法代码示例

本文整理汇总了Python中attrdict.AttrDict.get方法的典型用法代码示例。如果您正苦于以下问题:Python AttrDict.get方法的具体用法?Python AttrDict.get怎么用?Python AttrDict.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在attrdict.AttrDict的用法示例。


在下文中一共展示了AttrDict.get方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_default_dict

# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import get [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')
开发者ID:explody,项目名称:AttrDict,代码行数:27,代码来源:tests.py

示例2: test_get

# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import get [as 别名]
    def test_get(self):
        """
        Test that attributes can be accessed (both as keys, and as
        attributes).
        """
        from attrdict import AttrDict

        adict = AttrDict({'foo': 'bar'})

        # found
        self.assertEqual(adict.get('foo'), 'bar')

        # found, default given
        self.assertEqual(adict.get('foo', 'baz'), 'bar')

        # not found
        self.assertEqual(adict.get('bar'), None)

        # not found, default given
        self.assertEqual(adict.get('bar', 'baz'), 'baz')
开发者ID:donsignore,项目名称:AttrDict,代码行数:22,代码来源:tests.py

示例3: test_delattr

# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import get [as 别名]
    def test_delattr(self):
        """
        Test that key-value pairs can be deleted as attributes.
        """
        from attrdict import AttrDict

        adict = AttrDict({'foo': 'bar', '_set': 'shadows', 'get': 'shadows'})

        del adict.foo

        # with self.assertRaises(AttributeError):
        try:
            adict.foo
        except AttributeError:
            pass  # expected
        else:
            raise AssertionError("Exception not thrown")

        # with self.assertRaises(KeyError):
        try:
            adict['foo']
        except KeyError:
            pass  # expected
        else:
            raise AssertionError("Exception not thrown")

        # with self.assertRaises(TypeError):
        try:
            del adict.lorem
        except TypeError:
            pass  # expected
        else:
            raise AssertionError("Exception not thrown")

        # with self.assertRaises(TypeError):
        try:
            del adict._set
        except TypeError:
            pass  # expected
        else:
            raise AssertionError("Exception not thrown")

        # with self.assertRaises(TypeError):
        try:
            del adict.get
        except TypeError:
            pass  # expected
        else:
            raise AssertionError("Exception not thrown")

        # make sure things weren't deleted
        adict._set
        self.assertEqual(adict.get('get'), 'shadows')
        self.assertEqual(adict, {'_set': 'shadows', 'get': 'shadows'})
开发者ID:donsignore,项目名称:AttrDict,代码行数:56,代码来源:tests.py

示例4: test_default_dict_pass_key

# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import get [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')
开发者ID:jtratner,项目名称:AttrDict,代码行数:25,代码来源:tests.py

示例5: test_default_dict_pass_key

# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import get [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')
开发者ID:explody,项目名称:AttrDict,代码行数:39,代码来源:tests.py


注:本文中的attrdict.AttrDict.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。