本文整理汇总了Python中PyObjCTools.KeyValueCoding类的典型用法代码示例。如果您正苦于以下问题:Python KeyValueCoding类的具体用法?Python KeyValueCoding怎么用?Python KeyValueCoding使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了KeyValueCoding类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_attribute_access
def test_attribute_access(self):
v = object()
o = KeyValueCoding.kvc(v)
o.key
o.key2
getattr(o, "key3.key4")
self.assertEqual(self._trace, [
('get', v, 'key'),
('get', v, 'key2'),
('get', v, 'key3.key4'),
])
self._trace[:] = []
o.key = 42
setattr(o, "key3.key4", 99)
self.assertEqual(self._trace, [
('set', v, 'key', 42),
('set', v, 'key3.key4', 99),
])
self._trace[:] = []
class Object (object):
pass
v = Object()
o = KeyValueCoding.kvc(v)
o.key = 42
o._key = 99
self.assertEqual(self._trace, [
('set', v, 'key', 42),
])
self.assertEqual(o._key, 99)
self.assertIn('_key', o.__dict__)
self.assertNotIn('key', o.__dict__)
示例2: test_cocoa_get
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_attr_get
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')
示例4: test_cocoa_set
def test_cocoa_set(self):
o = objc.lookUpClass('NSMutableDictionary').alloc().init()
KeyValueCoding.setKey(o, 'attr', 'value')
self.assertEqual(o, {'attr': 'value'})
KeyValueCoding.setKeyPath(o, 'attr', 'value2')
self.assertEqual(o, {'attr': 'value2'})
o = objc.lookUpClass('NSObject').alloc().init()
self.assertRaises(KeyError, KeyValueCoding.setKey, o, 'description', 'hello')
self.assertRaises(KeyError, KeyValueCoding.setKeyPath, o, 'description', 'hello')
示例5: test_dict_set
def test_dict_set(self):
v = {'a': 42, 'c':{} }
KeyValueCoding.setKey(v, 'a', 43)
KeyValueCoding.setKey(v, 'b', 'B')
self.assertEqual(v, {'a': 43, 'b': 'B', 'c':{} })
KeyValueCoding.setKeyPath(v, 'a', 44)
KeyValueCoding.setKeyPath(v, 'b', 'C')
KeyValueCoding.setKeyPath(v, 'c.a', 'A')
self.assertEqual(v, {'a': 44, 'b': 'C', 'c':{'a': 'A'} })
示例6: test_count
def test_count(self):
arrayOperators = KeyValueCoding._ArrayOperators
values = [
{ 'a' : 1 },
{ 'a' : 2, 'b': 4 },
{ 'a' : 3, 'b': 2 },
{ 'a' : 4 },
]
self.assertEqual(arrayOperators.count(values, 'a'), len(values))
self.assertEqual(arrayOperators.count(values, 'b'), len(values))
self.assertEqual(arrayOperators.count(values, ()), len(values))
self.assertEqual(KeyValueCoding.getKeyPath(values, '@count'), len(values))
self.assertEqual(KeyValueCoding.getKeyPath(values, '@count.a'), len(values))
示例7: test_keyCaps
def test_keyCaps(self):
self.assertEqual(KeyValueCoding.keyCaps("attr"), "Attr")
self.assertEqual(KeyValueCoding.keyCaps("Attr"), "Attr")
self.assertEqual(KeyValueCoding.keyCaps("AttR"), "AttR")
self.assertEqual(KeyValueCoding.keyCaps("attr_with_value"), "Attr_with_value")
self.assertEqual(KeyValueCoding.keyCaps(b"attr"), b"Attr")
self.assertEqual(KeyValueCoding.keyCaps(b"Attr"), b"Attr")
self.assertEqual(KeyValueCoding.keyCaps(b"AttR"), b"AttR")
self.assertEqual(KeyValueCoding.keyCaps(b"attr_with_value"), b"Attr_with_value")
示例8: test_avg
def test_avg(self):
arrayOperators = KeyValueCoding._ArrayOperators
values = [
{ 'a' : 1 },
{ 'a' : 2, 'b': 4 },
{ 'a' : 3, 'b': 2 },
{ 'a' : 4 },
]
self.assertEqual(arrayOperators.avg(values, 'a'), 2.5)
self.assertEqual(arrayOperators.avg(values, 'b'), 1.5)
self.assertEqual(arrayOperators.avg(values, 'c'), 0)
self.assertEqual(arrayOperators.avg([], 'b'), 0)
self.assertRaises(KeyError, arrayOperators.avg, [], ())
self.assertEqual(KeyValueCoding.getKeyPath(values, '@avg.a'), 2.5)
self.assertEqual(KeyValueCoding.getKeyPath(values, '@avg.b'), 1.5)
self.assertEqual(KeyValueCoding.getKeyPath(values, '@avg.c'), 0)
示例9: test_sum
def test_sum(self):
arrayOperators = KeyValueCoding._ArrayOperators
values = [
{ 'a' : 1 },
{ 'a' : 2, 'b': 4 },
{ 'a' : 3, 'b': 2 },
{ 'a' : 4 },
]
self.assertEqual(arrayOperators.sum(values, 'a'), 10)
self.assertEqual(arrayOperators.sum(values, 'b'), 6)
self.assertEqual(arrayOperators.sum(values, 'c'), 0)
self.assertEqual(arrayOperators.sum([], 'b'), 0)
self.assertRaises(KeyError, arrayOperators.sum, [], ())
self.assertEqual(KeyValueCoding.getKeyPath(values, '@sum.a'), 10)
self.assertEqual(KeyValueCoding.getKeyPath(values, '@sum.b'), 6)
self.assertEqual(KeyValueCoding.getKeyPath(values, '@sum.c'), 0)
示例10: testMixedGraph2
def testMixedGraph2(self):
arr = objc.lookUpClass('NSMutableArray').alloc().init()
d1 = objc.lookUpClass('NSMutableDictionary').alloc().init()
d2 = objc.lookUpClass('NSMutableDictionary').alloc().init()
d3 = {}
root = objc.lookUpClass('NSMutableDictionary').dictionaryWithDictionary_({ 'a': arr, 'd': d2 })
arr.addObject_(d1)
arr.addObject_(d3)
d1['k'] = 1
d3['k'] = 2
KeyValueCoding.setKeyPath(root, 'd.a', 'the letter A')
self.assertEqual(d2, {'a': 'the letter A', })
self.assertEqual(KeyValueCoding.getKeyPath(root, 'd.a'), 'the letter A')
self.assertEqual(KeyValueCoding.getKeyPath(arr, 'k'), [1, 2])
self.assertEqual(KeyValueCoding.getKeyPath(root, 'a.k'), [1, 2])
示例11: test_distinctUnionOfObjects
def test_distinctUnionOfObjects(self):
arrayOperators = KeyValueCoding._ArrayOperators
class Int (object):
def __init__(self, value):
self._value = value
def __repr__(self):
return 'Int(%r)'%(self._value)
def __eq__(self, other):
if isinstance(other, int):
return self._value == other
elif isinstance(other, Int):
return self._value == other._value
else:
return False
def __hash__(self): raise TypeError
values = [
{ 'a' : { 'b': 1 } },
{ 'a' : { 'b': Int(1) } },
{ 'a' : { 'b': 2 } },
{ 'a' : { 'b': Int(3) } },
{ 'a' : { 'b': Int(3) } },
]
self.assertEqual(arrayOperators.distinctUnionOfObjects(values, ('a', 'b')), [1, 2, 3 ])
self.assertEqual(KeyValueCoding.getKeyPath(values, '@distinctUnionOfObjects.a.b'), [1, 2, 3 ])
values.append({'a': {}})
self.assertRaises(KeyError, arrayOperators.distinctUnionOfObjects, values, ('a', 'b'))
self.assertRaises(KeyError, KeyValueCoding.getKeyPath, values, '@distinctUnionOfObjects.a.b')
class Rec (object):
def __init__(self, b):
self.b = b
def __eq__(self, other):
return type(self) == type(other) and self.b == other.b
def __hash__(self): raise TypeError
values = [
{ 'a' : Rec(1) },
{ 'a' : Rec(1) },
{ 'a' : Rec(2) },
{ 'a' : Rec(3) },
]
self.assertEqual(arrayOperators.distinctUnionOfObjects(values, ('a', 'b')), [1, 2, 3 ])
示例12: test_max
def test_max(self):
arrayOperators = KeyValueCoding._ArrayOperators
values = [
{ 'a' : 1 },
{ 'a' : 2, 'b': 5 },
{ 'a' : 3, 'b': 2 },
{ 'a' : 4 },
]
self.assertEqual(arrayOperators.max(values, 'a'), 4)
self.assertEqual(arrayOperators.max(values, 'b'), 5)
self.assertRaises(KeyError, arrayOperators.max, values, ())
self.assertEqual(KeyValueCoding.getKeyPath(values, '@max.a'), 4)
示例13: test_unionOfObjects
def test_unionOfObjects(self):
arrayOperators = KeyValueCoding._ArrayOperators
values = [
{ 'a' : { 'b': 1 } },
{ 'a' : { 'b': 1 } },
{ 'a' : { 'b': 2 } },
{ 'a' : { 'b': 3 } },
]
self.assertEqual(arrayOperators.unionOfObjects(values, ('a', 'b')), [1, 1, 2, 3 ])
self.assertEqual(KeyValueCoding.getKeyPath(values, '@unionOfObjects.a.b'), [1, 1, 2, 3])
values.append({'a': {}})
self.assertRaises(KeyError, arrayOperators.unionOfObjects, values, ('a', 'b'))
示例14: test_item_access
def test_item_access(self):
v = object()
o = KeyValueCoding.kvc(v)
o['key']
o['key2']
o['key3.key4']
self.assertEqual(self._trace, [
('get-path', v, 'key'),
('get-path', v, 'key2'),
('get-path', v, 'key3.key4'),
])
self._trace[:] = []
o["key"] = 42
o["key3.key4"] = 99
self.assertEqual(self._trace, [
('set-path', v, 'key', 42),
('set-path', v, 'key3.key4', 99),
])
self._trace[:] = []
self.assertRaises(TypeError, operator.getitem, o, 42)
self.assertRaises(TypeError, operator.setitem, o, 42, 99)
示例15: test_msum
def test_msum(self):
self.assertEqual(KeyValueCoding.msum([1, 1e100, 1, -1e100] * 10000), 20000)
self.assertEqual(KeyValueCoding.msum([1.0, 2.0, 3.0, 4.0]), 10.0)