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


Python KeyValueCoding.getKey方法代码示例

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


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

示例1: test_attr_get

# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKey [as 别名]
    def test_attr_get(self):
        class Record (object):
            __slots__ = ('slot1', '__dict__')
            def __init__(self, **kwds):
                for k, v in kwds.items():
                    setattr(self, k, v)

            @property
            def prop1(self):
                return 'a property'

        r = Record(slot1=42, attr1='a')

        self.assertEqual(KeyValueCoding.getKey(r, 'slot1'), 42)
        self.assertEqual(KeyValueCoding.getKey(r, 'attr1'), 'a')
        self.assertEqual(KeyValueCoding.getKey(r, 'prop1'), 'a property')
        self.assertEqual(KeyValueCoding.getKeyPath(r, 'slot1'), 42)
        self.assertEqual(KeyValueCoding.getKeyPath(r, 'attr1'), 'a')
        self.assertEqual(KeyValueCoding.getKeyPath(r, 'prop1'), 'a property')

        r = Record(attr1=Record(attr2='b', attr3=[Record(a=1), Record(a=2, b='b')]))
        self.assertRaises(KeyError, KeyValueCoding.getKey, r, 'slot1')
        self.assertRaises(KeyError, KeyValueCoding.getKey, r, 'attr99')
        self.assertRaises(KeyError, KeyValueCoding.getKeyPath, r, 'slot1')
        self.assertRaises(KeyError, KeyValueCoding.getKeyPath, r, 'attr99')

        self.assertEqual(KeyValueCoding.getKeyPath(r, 'attr1.attr2'), 'b')
        self.assertEqual(KeyValueCoding.getKeyPath(r, 'attr1.attr3.a'), [1, 2])
        self.assertEqual(KeyValueCoding.getKeyPath(r, 'attr1.attr3.b'), [null, 'b'])
        self.assertRaises(KeyError, KeyValueCoding.getKeyPath, r, 'attr3')
        self.assertRaises(KeyError, KeyValueCoding.getKeyPath, r, 'attr1.attr9')
开发者ID:BMXE,项目名称:music-player,代码行数:33,代码来源:test_keyvaluecoding.py

示例2: test_cocoa_get

# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKey [as 别名]
 def test_cocoa_get(self):
     r = objc.lookUpClass('NSObject').alloc().init()
     self.assertEqual(KeyValueCoding.getKey(r, 'description'), r.description())
     self.assertEqual(KeyValueCoding.getKeyPath(r, 'description'), r.description())
     self.assertEqual(KeyValueCoding.getKeyPath(r, 'description.length'), len(r.description()))
     self.assertRaises(KeyError, KeyValueCoding.getKey, r, 'nosuchattr')
     self.assertRaises(KeyError, KeyValueCoding.getKeyPath, r, 'description.nosuchattr')
开发者ID:BMXE,项目名称:music-player,代码行数:9,代码来源:test_keyvaluecoding.py

示例3: test_none_get

# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKey [as 别名]
 def test_none_get(self):
     self.assertEqual(KeyValueCoding.getKey(None, 'a'), None)
     self.assertEqual(KeyValueCoding.getKeyPath(None, 'a'), None)
开发者ID:BMXE,项目名称:music-player,代码行数:5,代码来源:test_keyvaluecoding.py

示例4: test_accessor_get

# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKey [as 别名]
    def test_accessor_get(self):
        class Object (object):
            def get_attr1(self):
                return "attr1"

            def getAttr1(self):
                return "Attr1"

            def attr1(self):
                return ".attr1"

            def get_attr2(self):
                return "attr2"

            def attr2(self):
                return '.attr2'

            def attr3(self):
                return '.attr3'

            def isAttr4(self):
                return "attr4?"

            @objc.selector
            def attrsel(self):
                return 'selattr'

        r = Object()
        self.assertEqual(KeyValueCoding.getKey(r, 'attr1'), 'Attr1')
        self.assertEqual(KeyValueCoding.getKey(r, 'attr2'), 'attr2')
        self.assertEqual(KeyValueCoding.getKey(r, 'attr3'), '.attr3')
        self.assertEqual(KeyValueCoding.getKey(r, 'attr4'), 'attr4?')
        self.assertEqual(KeyValueCoding.getKey(r, 'attrsel'), 'selattr')

        t = Object()
        o = objc.lookUpClass('NSObject').alloc().init()
        l = []

        r.attr5 = t.isAttr4
        r.attr6 = o.description
        r.attr7 = l.__len__
        r.attr8 = os.getpid
        r.attr9 = 'attribute 9'

        self.assertEqual(KeyValueCoding.getKey(r, 'attr5'), t.isAttr4)
        self.assertEqual( KeyValueCoding.getKey(r, 'attr6'), r.attr6)
        self.assertEqual(KeyValueCoding.getKey(r, 'attr7'), l.__len__)
        self.assertEqual(KeyValueCoding.getKey(r, 'attr8'), os.getpid())
        self.assertEqual(KeyValueCoding.getKey(r, 'attr9'), 'attribute 9')
        self.assertEqual(KeyValueCoding.getKey(1.5, 'hex'), (1.5).hex())
开发者ID:BMXE,项目名称:music-player,代码行数:52,代码来源:test_keyvaluecoding.py

示例5: test_array_get

# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKey [as 别名]
 def test_array_get(self):
     l = [{'a': 1, 'b':2 }, {'a':2} ]
     self.assertEqual(KeyValueCoding.getKey(l, 'a'), [1, 2])
     self.assertEqual(KeyValueCoding.getKey(l, 'b'), [2, null])
开发者ID:BMXE,项目名称:music-player,代码行数:6,代码来源:test_keyvaluecoding.py

示例6: test_dict_get

# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKey [as 别名]
 def test_dict_get(self):
     d = {'a':1 }
     self.assertEqual(KeyValueCoding.getKey(d, 'a'), 1)
     self.assertRaises(KeyError, KeyValueCoding.getKey, d, 'b')
开发者ID:BMXE,项目名称:music-player,代码行数:6,代码来源:test_keyvaluecoding.py


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