PyObjCTools.KeyValueCoding – Key-Value Coding API¶
Support for Key-Value Coding in Python. This provides a simple functional interface to Cocoa’s Key-Value coding that also works for regular Python objects.
Key-Value Coding is Cocoa functionality that is simular to
the getattr() and setattr() functions in Python. The APIs
in this module are modelled on those functions and work on
Cocoa objects as well as basic Python objects.
Key-Value Coding works with keys, basically attribute names, as well as key-paths. A key-path is a string that contains a sequence of dot-separated keys and is used to chain a number of keys together.
Accessor functions¶
- PyObjCTools.KeyValueCoding.getKey(object, key)¶
Return the value of the attribute referenced by
key. The key is used to build the name of an accessor method or attribute name.The following methods are tried for regular Python objects:
Accessor method
getKeyAccessor method
get_keyAccessor method or attribute
keyAccessor method or attribute
isKeyAttribute
_key
(In all of these “key” is replaced by the value of
key).This function calls the regular Key-Value Coding methods for Cocoa objects, including those implemented in Python.
- Parameters:
object – An arbitrary object
key (string) – name of a key
- Result:
the value of a key
- Raise:
KeyErrorwhen the key does not exist.
- PyObjCTools.KeyValueCoding.setKey(object, key, value)¶
Set the value of the attribute referenced by
keytokey. The key is used to build the name of an accessor method or attribute name.When the object is a Cocoa object (including those implemented in Python) this method calls the regular Cocoa API for setting a property.
When the object is an instance of
collections.Mappingthis function usesoperator.setitem()to update the dictionary.The following methods are tried for regular Python objects:
Accessor method
setKeyAccessor method
set_keyattribute
keywhen that already exists (and is not a method)attribute
_keywhen that already existsAttribute
key
(In all of these “key” is replaced by the value of
key).- Parameters:
object – An arbitrary object
key (string) – name of a key
value – The value to set
- Result:
the value of a key
- Raise:
KeyErrorwhen the key does not exist.
- PyObjCTools.KeyValueCoding.getKeyPath(object, keypath)¶
The
keypathis a string containing a path of keys. The keys are separated by colons, for example"owner.firstName".The key path is used to traverse an object graph to an attribute. This function also supports set and array operators. Those are keys of the form
@operatorare are used aspathToArray.@operator.pathToProperty, for examplesystem.disks.@max.capacity.The table below lists the supported array operators
Operator
avgUse the rest of the keypath to fetch the value of each item in the container and returns the average of those values.
countReturns the number of items in the container
distinctUnionOfArraysUse the rest of the keypath to fetch the value of each item, which must be a sequence. Those sequences are merged into an array with distinct values.
distinctUnionOfObjectsUse the rest of the keypath to fetch the value of each item and return an array with all distinct values.
maxUse the rest of the keypath to fetch the value of each item in the container and returns the maximum of those values.
minUse the rest of the keypath to fetch the value of each item in the container and returns the minimum of those values.
sumUse the rest of the keypath to fetch the value of each item in the container and returns the sum of those values.
unionOfArraysLike
distinctUnionOfArrays, but without removing duplicates.unionOfObjectsLike
distinctUnionOfObjects, but without removing duplicatesThis function calls the regular Key-Value Coding method for Cocoa objects.
- Parameters:
object – An arbitrary object
keypath (string) – The keypath, colon separated keys
Key-Value Coding wrapper¶
- class PyObjCTools.KeyValueCoding.kvc(value)¶
This wrappers
valuein an object that uses KeyValue Coding to implement the attribute and item accessors.