fsleyes_props.properties_value
Definitions of the PropertyValue and PropertyValueList
classes.
An object which encapsulates a value of some sort.
A
PropertyValueListis aPropertyValueinstance which stores otherPropertyValueinstance in a list.
PropertyValue and PropertyValueList instances are intended to be
created and managed by PropertyBase and ListPropertyBase
instances respectively, and are used to encapsulate attribute values of
HasProperties instances.
These class definitions are really a part of the properties module -
they are separated to keep file sizes down. However, the
PropertyValue class definitions have no dependence upon the
PropertyBase or HasProperties definitions.
- class fsleyes_props.properties_value.Listener(propVal, name, function, enabled, immediate)[source]
Bases:
objectThe
Listenerclass is used byPropertyValueinstances to manage their listeners - seePropertyValue.addListener().Create a
Listener.- Parameters:
propVal – The
PropertyValuethat owns thisListener.name – The listener name.
function – The callback function.
enabled – Whether the listener is enabled/disabled.
immediate – Whether the listener is to be called immediately, or via the
PropertyValue.queue.
- makeQueueName()[source]
Returns a more descriptive name for this
Listener, which is used as its name when passed to theCallQueue.
- property expectsArguments
Returns
Trueif the listener function needs to be passed arguments,Falseotherwise. Property listener functions can be defined to accept either zero arguments, or a set of positional arguments - seePropertyValue.addListener()andPropertyValue.addAttributeListener()for more details.
- class fsleyes_props.properties_value.PropertyValue(context, name=None, value=None, castFunc=None, validateFunc=None, equalityFunc=None, preNotifyFunc=None, postNotifyFunc=None, allowInvalid=True, parent=None, **attributes)[source]
Bases:
objectAn object which encapsulates a value of some sort.
The value may be subjected to casting and validation rules, and listeners may be registered for notification of value and validity changes.
Notification of value and attribute listeners is performed by the
bindablemodule - see thebindable.syncAndNotify()andbindable.syncAndNotifyAtts()functions.Create a
PropertyValueobject.- Parameters:
context – An object which is passed as the first argument to the
validateFunc,preNotifyFunc,postNotifyFunc, and any registered listeners. Can technically be anything, but will nearly always be aHasPropertiesinstance.name – Value name - if not provided, a default, unique name is created.
value – Initial value.
castFunc – Function which performs type casting or data conversion. Must accept three parameters - the context, a dictionary containing the attributes of this object, and the value to cast. Must return that value, cast/converted appropriately.
validateFunc – Function which accepts three parameters - the context, a dictionary containing the attributes of this object, and a value. This function should test the provided value, and raise a
ValueErrorif it is invalid.equalityFunc – Function which accepts two values, and should return
Trueif they are equal,Falseotherwise. If not provided, the python equailty operator (i.e.==) is used.preNotifyFunc – Function to be called whenever the property value changes, but before any registered listeners are called. See the
addListener()method for details of the parameters this function must accept.postNotifyFunc – Function to be called whenever the property value changes, but after any registered listeners are called. Must accept the same parameters as the
preNotifyFunc.allowInvalid – If
False, any attempt to set the value to something invalid will result in aValueError. Note that this does not guarantee that the property will never have an invalid value, as the definition of ‘valid’ depends on external factors (i.e. thevalidateFunc). Therefore, the validity of a value may change, even if the value itself has not changed.parent – If this PV instance is a member of a
PropertyValueListinstance, the latter sets itself as the parent of this PV. Whenever the value of this PV changes, thePropertyValueList._listPVChanged()method is called.attributes – Any key-value pairs which are to be associated with this
PropertyValueobject, and passed to thecastFuncandvalidateFuncfunctions. Attributes are not used by thePropertyValueorPropertyValueListclasses, however they are used by thePropertyBaseandListPropertyBaseclasses to store per-instance property attributes. Listeners may register to be notified when attribute values change.
- queue = <fsleyes_props.callqueue.CallQueue object>
A
CallQueueinstance which is shared by allPropertyValueinstances, and used for notifying listeners of value and attribute changes.A queue is used for notification so that listeners are notified in the order that values were changed.
- getParent()[source]
If this
PropertyValueis an item in aPropertyValueList, this method returns a reference to the owningPropertyValueList. Otherwise, this method returnsNone.
- allowInvalid(allow=None)[source]
Query/set the allow invalid state of this value.
If no arguments are passed, returns the current allow invalid state. Otherwise, sets the current allow invalid state. to the given argument.
- enableNotification(bound=False, att=False)[source]
Enables notification of property value and attribute listeners for this
PropertyValueobject.- Parameters:
bound – If
True, notification is enabled on all otherPropertyValueinstances that are bound to this one (see thebindablemodule). IfFalse(the default), notification is only enabled on thisPropertyValue.att – If
True, notification is enabled on all attribute listeners as well as property value listeners.
- disableNotification(bound=False, att=False)[source]
Disables notification of property value and attribute listeners for this
PropertyValueobject. Notification can be re-enabled via theenableNotification()orsetNotificationState()methods.- Parameters:
bound – If
True, notification is disabled on all otherPropertyValueinstances that are bound to this one (see thebindablemodule). IfFalse(the default), notification is only disabled on thisPropertyValue.att – If
True, notification is disabled on all attribute listeners as well as property value listeners.
- addAttributeListener(name, listener, weak=True, immediate=False)[source]
Adds an attribute listener for this
PropertyValue. The listener callback function must accept either no arguments, or the following arguments:context: The context associated with thisPropertyValue.attribute: The name of the attribute that changed.value: The new attribute value.name: The name of thisPropertyValueinstance.
- Parameters:
name – A unique name for the listener. If a listener with the specified name already exists, it will be overwritten.
listener – The callback function.
weak – If
True(the default), a weak reference to the callback function is used.immediate – If
False(the default), the listener is called immediately; otherwise, it is called via thequeue.
- getAttributes()[source]
Returns a dictionary containing all the attributes of this
PropertyValueobject.
- setAttributes(atts)[source]
Sets all the attributes of this
PropertyValueobject. from the given dictionary.
- getAttribute(name, *arg)[source]
Returns the value of the named attribute.
- Parameters:
default – If provided, the default value to use if
nameis not an attribute. If not provided, andnameis not an attribute, aKeyErroris raised.
- setAttribute(name, value)[source]
Sets the named attribute to the given value, and notifies any registered attribute listeners of the change.
- prepareListeners(att, name=None, value=None)[source]
Prepares a list of
Listenerinstances ready to be called, and a list of arguments to pass to them.- Parameters:
att – If
True, attribute listeners are returned, otherwise value listeners are returned.name – If
att == True, the attribute name.value – If
att == True, the attribute value.
- notifyAttributeListeners(name, value)[source]
Notifies all registered attribute listeners of an attribute changed - see the
bindable.syncAndNotifyAtts()function.
- addListener(name, callback, overwrite=False, weak=True, immediate=False)[source]
Adds a listener for this value.
When the value changes, the listener callback function is called. The callback function may either be defined to accept no arguments, or to accept the following arguments:
value: The property valuevalid: Whether the value is valid or invalidcontext: The context object passed to__init__().name: The name of thisPropertyValueinstance.
Listener names
prenotifyandpostnotifyare reserved - if either of these are passed in for the listener name, a :exc`ValueError` is raised.- Parameters:
name (str) – A unique name for this listener. If a listener with the name already exists, a :exc`RuntimeError` will be raised, or it will be overwritten, depending upon the value of the
overwriteargument.callback – The callback function.
overwrite – If
Trueany previous listener with the same name will be overwritten.weak – If
True(the default), a weak reference to the callback function is retained, meaning that it can be garbage-collected. If passing in a lambda or inner function, you will probably want to setweaktoFalse, in which case a strong reference will be used.immediate – If
False(the default), this listener will be notified through theCallQueue- listeners for allPropertyValueinstances are queued, and notified in turn. IfTrue, IfTrue, theCallQueuewill not be used, and this listener will be notified as soon as thisPropertyValuechanges.
- disableListener(name)[source]
Disables the listener with the specified
name, but does not remove it from the list of listeners.
- getListenerState(name)[source]
Returns
Trueif the specified listener is currently enabled,Falseotherwise.An
AttributeErroris raised if a listener with the specifiednamedoes not exist.
- hasListener(name)[source]
Returns
Trueif a listener with the given name is registered,Falseotherwise.
- setPreNotifyFunction(preNotifyFunc)[source]
Sets the function to be called on value changes, before any registered listeners.
- setPostNotifyFunction(postNotifyFunc)[source]
Sets the function to be called on value changes, after any registered listeners.
- set(newValue)[source]
Sets the property value.
The property is validated and, if the property value or its validity has changed, any registered listeners are called through the
propNotify()method. IfallowInvalidwas set toFalse, and the new value is not valid, aValueErroris raised, and listeners are not notified.
- propNotify()[source]
Notifies registered listeners - see the
bindable.syncAndNotify()function.
- class fsleyes_props.properties_value.PropertyValueList(context, name=None, values=None, itemCastFunc=None, itemEqualityFunc=None, itemValidateFunc=None, listValidateFunc=None, itemAllowInvalid=True, preNotifyFunc=None, postNotifyFunc=None, listAttributes=None, itemAttributes=None)[source]
Bases:
PropertyValueA
PropertyValueListis aPropertyValueinstance which stores otherPropertyValueinstance in a list. Instances of this class are generally managed by aListPropertyBaseinstance.When created, separate validation functions may be passed in for individual items, and for the list as a whole. Listeners may be registered on individual
PropertyValueinstances (accessible via thegetPropertyValueList()method), or on the entire list.The values contained in this
PropertyValueListmay be accessed through standard Python list operations, including slice-based access and assignment,append(),insert(),extend(),pop(),index(),count(),move(),insertAll(),removeAll(), andreorder()(these last few are non-standard).Because the values contained in this list are
PropertyValueinstances themselves, some limitations are present on list modifying operations:class MyObj(props.HasProperties): mylist = props.List(default[1, 2, 3]) myobj = MyObj()
Simple list-slicing modifications work as expected:
# the value after this will be [5, 2, 3] myobj.mylist[0] = 5 # the value after this will be [5, 6, 7] myobj.mylist[1:] = [6, 7]
However, modifications which would change the length of the list are not supported:
# This will result in an IndexError myobj.mylist[0:2] = [6, 7, 8]
The exception to this rule concerns modifications which would replace every value in the list:
# These assignments are equivalent myobj.mylist[:] = [1, 2, 3, 4, 5] myobj.mylist = [1, 2, 3, 4, 5]
While the simple list modifications described above will change the value(s) of the existing
PropertyValueinstances in the list, modifications which replace the entire list contents will result in existingPropertyValueinstances being destroyed, and new ones being created. This is a very important point to remember if you have registered listeners on individualPropertyValueitems.A listener registered on a
PropertyValueListwill be notified whenever the list is modified (e.g. additions, removals, reorderings), and whenever any individual value in the list changes. Alternately, listeners may be registered on the individualPropertyValueinstances (which are accessible through thegetPropertyValueList()method) to be nofitied of changes to those values only.There are some interesting type-specific subclasses of the
PropertyValueList, which provide additional functionality:The
PointValueList, forPointproperties.The
BoundsValueList, forBoundsproperties.
Create a
PropertyValueList.- Parameters:
context – See
PropertyValue.__init__().name – See
PropertyValue.__init__().values – Initial list values.
itemCastFunc – Function which casts a single list item.
itemEqualityFunc – Function which tests equality of two values.
itemValidateFunc – Function which validates a single list item.
listValidateFunc – Function which validates the list as a whole.
itemAllowInvalid – Whether items are allowed to containg invalid values.
preNotifyFunc – See
PropertyValue.__init__().postNotifyFunc – See
PropertyValue.__init__().listAttributes – Attributes to be associated with this
PropertyValueList.itemAttributes – Attributes to be associated with new
PropertyValueitems added to the list.
- getPropertyValueList()[source]
Return (a copy of) the underlying property value list, allowing access to the
PropertyValueinstances which manage each list item.
- get()[source]
Overrides
PropertyValue.get(). Returns thisPropertyValueListobject.
- set(newValues)[source]
Overrides
PropertyValue.set().Sets the values stored in this
PropertyValueList. If the length of thenewValuesargument does not match the current list length, anIndexErroris raised.
- getLast()[source]
Overrides
PropertyValue.getLast(). Returns the most recent list values.