本文整理汇总了Python中PyObjCTools.KeyValueCoding.getKeyPath方法的典型用法代码示例。如果您正苦于以下问题:Python KeyValueCoding.getKeyPath方法的具体用法?Python KeyValueCoding.getKeyPath怎么用?Python KeyValueCoding.getKeyPath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyObjCTools.KeyValueCoding
的用法示例。
在下文中一共展示了KeyValueCoding.getKeyPath方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cocoa_get
# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKeyPath [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')
示例2: test_count
# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKeyPath [as 别名]
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))
示例3: test_avg
# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKeyPath [as 别名]
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)
示例4: test_sum
# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKeyPath [as 别名]
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)
示例5: testMixedGraph2
# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKeyPath [as 别名]
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])
示例6: test_distinctUnionOfObjects
# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKeyPath [as 别名]
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 ])
示例7: test_attr_get
# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKeyPath [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')
示例8: test_max
# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKeyPath [as 别名]
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)
示例9: test_unionOfObjects
# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKeyPath [as 别名]
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'))
示例10: test_none_get
# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKeyPath [as 别名]
def test_none_get(self):
self.assertEqual(KeyValueCoding.getKey(None, 'a'), None)
self.assertEqual(KeyValueCoding.getKeyPath(None, 'a'), None)
示例11: test_unionOfArrays
# 需要导入模块: from PyObjCTools import KeyValueCoding [as 别名]
# 或者: from PyObjCTools.KeyValueCoding import getKeyPath [as 别名]
def test_unionOfArrays(self):
arrayOperators = KeyValueCoding._ArrayOperators
class Rec (object):
def __init__(self, **kwds):
for k, v in kwds.items():
setattr(self, k, v)
def __eq__(self, other):
return type(self) is type(other) and self.__dict__ == other.__dict__
def __hash__(self): raise TypeError
class Str (object):
def __init__(self, value):
self._value = value
def __repr__(self):
return 'Str(%r)'%(self._value)
def __eq__(self, other):
if isinstance(other, str):
return self._value == other
elif isinstance(other, Str):
return self._value == other._value
else:
return False
def __cmp__(self, other):
if isinstance(other, str):
return cmp(self._value, other)
elif isinstance(other, Str):
return cmp(self._value, other._value)
else:
return NotImplementedError
def __hash__(self): raise TypeError
transactions = [
[
dict(payee='Green Power', amount=120.0),
dict(payee='Green Power', amount=150.0),
dict(payee=Str('Green Power'), amount=170.0),
Rec(payee='Car Loan', amount=250.0),
dict(payee='Car Loan', amount=250.0),
dict(payee='Car Loan', amount=250.0),
dict(payee=Str('General Cable'), amount=120.0),
dict(payee='General Cable', amount=155.0),
Rec(payee='General Cable', amount=120.0),
dict(payee='Mortgage', amount=1250.0),
dict(payee='Mortgage', amount=1250.0),
dict(payee='Mortgage', amount=1250.0),
dict(payee='Animal Hospital', amount=600.0),
],
[
dict(payee='General Cable - Cottage', amount=120.0),
dict(payee='General Cable - Cottage', amount=155.0),
Rec(payee='General Cable - Cottage', amount=120.0),
dict(payee='Second Mortgage', amount=1250.0),
dict(payee='Second Mortgage', amount=1250.0),
dict(payee=Str('Second Mortgage'), amount=1250.0),
dict(payee='Hobby Shop', amount=600.0),
]
]
self.assertEqual(arrayOperators.distinctUnionOfArrays(transactions, ('payee',)), ['Green Power', 'Car Loan', 'General Cable', 'Mortgage', 'Animal Hospital', 'General Cable - Cottage', 'Second Mortgage', 'Hobby Shop'])
self.assertEqual(KeyValueCoding.getKeyPath(transactions, '@distinctUnionOfArrays.payee'), ['Green Power', 'Car Loan', 'General Cable', 'Mortgage', 'Animal Hospital', 'General Cable - Cottage', 'Second Mortgage', 'Hobby Shop'])
self.assertEqual(arrayOperators.unionOfArrays(transactions, ('payee',)), [
'Green Power',
'Green Power',
'Green Power',
'Car Loan',
'Car Loan',
'Car Loan',
'General Cable',
'General Cable',
'General Cable',
'Mortgage',
'Mortgage',
'Mortgage',
'Animal Hospital',
'General Cable - Cottage',
'General Cable - Cottage',
'General Cable - Cottage',
'Second Mortgage',
'Second Mortgage',
'Second Mortgage',
'Hobby Shop'
])
self.assertEqual(KeyValueCoding.getKeyPath(transactions, '@unionOfArrays.payee'), [
'Green Power',
'Green Power',
'Green Power',
'Car Loan',
'Car Loan',
'Car Loan',
#.........这里部分代码省略.........