當前位置: 首頁>>代碼示例>>Python>>正文


Python KeyValueCoding.setKey方法代碼示例

本文整理匯總了Python中PyObjCTools.KeyValueCoding.setKey方法的典型用法代碼示例。如果您正苦於以下問題:Python KeyValueCoding.setKey方法的具體用法?Python KeyValueCoding.setKey怎麽用?Python KeyValueCoding.setKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在PyObjCTools.KeyValueCoding的用法示例。


在下文中一共展示了KeyValueCoding.setKey方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_cocoa_set

# 需要導入模塊: from PyObjCTools import KeyValueCoding [as 別名]
# 或者: from PyObjCTools.KeyValueCoding import setKey [as 別名]
    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')
開發者ID:BMXE,項目名稱:music-player,代碼行數:13,代碼來源:test_keyvaluecoding.py

示例2: test_dict_set

# 需要導入模塊: from PyObjCTools import KeyValueCoding [as 別名]
# 或者: from PyObjCTools.KeyValueCoding import setKey [as 別名]
    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'} })
開發者ID:BMXE,項目名稱:music-player,代碼行數:13,代碼來源:test_keyvaluecoding.py

示例3: test_accessor

# 需要導入模塊: from PyObjCTools import KeyValueCoding [as 別名]
# 或者: from PyObjCTools.KeyValueCoding import setKey [as 別名]
    def test_accessor(self):
        class Record (object):
            def __init__(self):
                self._attr1 = 1
                self._attr2 = 2
                self._attr3 = 3

            def attr1(self):
                return self._attr1

            def setAttr1_(self, value):
                self._attr1 = (1, value)

            def setAttr1(self, value):
                self._attr1 = (2, value)

            def set_attr1(self, value):
                self._attr1 = (3, value)

            def setAttr2(self, value):
                self._attr2 = (2, value)

            def set_attr2(self, value):
                self._attr2 = (3, value)

            def set_attr3(self, value):
                self._attr3 = (3, value)
            
            set_no_attr = 4

        o = Record()
        self.assertEqual(o._attr1, 1)
        self.assertEqual(o._attr2, 2)
        self.assertEqual(o._attr3, 3)
        self.assertEqual(o.set_no_attr, 4)

        KeyValueCoding.setKey(o, 'attr1', 9)
        KeyValueCoding.setKey(o, 'attr2', 10)
        KeyValueCoding.setKey(o, 'attr3', 11)
        KeyValueCoding.setKey(o, 'no_attr', 12)

        
        self.assertEqual(o._attr1, (1, 9))
        self.assertEqual(o._attr2, (2, 10))
        self.assertEqual(o._attr3, (3, 11))
        self.assertEqual(o.no_attr, 12)

        KeyValueCoding.setKeyPath(o, 'attr1', 29)
        KeyValueCoding.setKeyPath(o, 'attr2', 210)
        KeyValueCoding.setKeyPath(o, 'attr3', 211)
        
        self.assertEqual(o._attr1, (1, 29))
        self.assertEqual(o._attr2, (2, 210))
        self.assertEqual(o._attr3, (3, 211))

        o._attr1 = {'a': 'b'}
        KeyValueCoding.setKeyPath(o, 'attr1.a', 30)
        self.assertEqual(o._attr1, {'a': 30})
開發者ID:BMXE,項目名稱:music-player,代碼行數:60,代碼來源:test_keyvaluecoding.py

示例4: test_attr_set

# 需要導入模塊: from PyObjCTools import KeyValueCoding [as 別名]
# 或者: from PyObjCTools.KeyValueCoding import setKey [as 別名]
    def test_attr_set(self):
        class R (object):
            @property
            def attr3(self):
                return self._attr3

            @attr3.setter
            def attr3(self, v):
                self._attr3 = v * 2

            @property
            def attr4(self):
                return self._attr4

            def attr6(self):
                return self._attr6

        r = R()
        r._attr1 = 42
        r._attr4 = 43
        r.attr5 = {}
        r._attr6 = 9

        KeyValueCoding.setKey(r, 'attr1', 1)
        KeyValueCoding.setKey(r, 'attr2', 2)
        KeyValueCoding.setKey(r, 'attr3', 3)
        self.assertRaises(KeyError, KeyValueCoding.setKey, r, 'attr4', 4)
        KeyValueCoding.setKey(r, 'attr6', 7)

        self.assertEqual(r._attr1, 1)
        self.assertEqual(r.attr2, 2)
        self.assertEqual(r.attr3, 6)
        self.assertEqual(r._attr3, 6)
        self.assertEqual(r._attr6, 7)

        KeyValueCoding.setKeyPath(r, 'attr1', 'one')
        KeyValueCoding.setKeyPath(r, 'attr2', 'two')
        KeyValueCoding.setKeyPath(r, 'attr3', 'three')
        KeyValueCoding.setKeyPath(r, 'attr5.sub1', 3)
        KeyValueCoding.setKeyPath(r, 'attr6', 'seven')

        self.assertEqual(r._attr1, 'one')
        self.assertEqual(r.attr2, 'two')
        self.assertEqual(r.attr3, 'threethree')
        self.assertEqual(r._attr3, 'threethree')
        self.assertEqual(r.attr5, {'sub1': 3})
        self.assertEqual(r._attr6, 'seven')
開發者ID:BMXE,項目名稱:music-player,代碼行數:49,代碼來源:test_keyvaluecoding.py

示例5: test_none_set

# 需要導入模塊: from PyObjCTools import KeyValueCoding [as 別名]
# 或者: from PyObjCTools.KeyValueCoding import setKey [as 別名]
 def test_none_set(self):
     # setKey(None, 'any', 'value') is documented as a no-op
     # check that this doesn't raise an exception.
     v = None
     KeyValueCoding.setKey(v, 'a', 42)
     KeyValueCoding.setKeyPath(v, 'a', 42)
開發者ID:BMXE,項目名稱:music-player,代碼行數:8,代碼來源:test_keyvaluecoding.py


注:本文中的PyObjCTools.KeyValueCoding.setKey方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。