本文整理匯總了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')
示例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')
示例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)
示例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())
示例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])
示例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')