colour.utilities.MixinCallback#

class colour.utilities.MixinCallback[source]#

Bases: object

Provide callback support for attribute changes in classes.

This mixin extends class functionality to enable callback registration, allowing automatic invocation when specified attributes are modified. Callbacks can transform or validate attribute values before they are set.

Attributes

Methods

Examples

>>> class WithCallback(MixinCallback):
...     def __init__(self):
...         super().__init__()
...         self.attribute_a = "a"
>>> with_callback = WithCallback()
>>> def _on_attribute_a_changed(self, name: str, value: str) -> str:
...     return value.upper()
>>> with_callback.register_callback(
...     "attribute_a", "on_attribute_a_changed", _on_attribute_a_changed
... )
>>> with_callback.attribute_a = "a"
>>> with_callback.attribute_a
'A'
__init__() None[source]#
Return type:

None

property callbacks: defaultdict[str, List[Callback]]#

Getter for the event callbacks dictionary.

Returns:

Dictionary mapping event names to lists of callback functions. Each key represents an event identifier, and each value contains the registered callbacks for that event.

Return type:

defaultdict

__setattr__(name: str, value: Any) None[source]#

Set the specified value to the attribute with the specified name.

Parameters:
  • name (str) – Name of the attribute to set.

  • value (Any) – Value to set the attribute with.

Return type:

None

register_callback(attribute: str, name: str, function: Callable) None[source]#

Register a callback with the specified name for the specified attribute.

Parameters:
  • attribute (str) – Attribute to register the callback for.

  • name (str) – Callback name.

  • function (Callable) – Callback callable.

Return type:

None

Examples

>>> class WithCallback(MixinCallback):
...     def __init__(self):
...         super().__init__()
...         self.attribute_a = "a"
>>> with_callback = WithCallback()
>>> with_callback.register_callback(
...     "attribute_a", "callback", lambda *args: None
... )
>>> with_callback.callbacks
defaultdict(<class 'list'>, {'attribute_a': [Callback(name='callback', function=<function <lambda> at 0x...>)]})
unregister_callback(attribute: str, name: str) None[source]#

Unregister the callback with the specified name for the specified attribute.

Parameters:
  • attribute (str) – Attribute to unregister the callback for.

  • name (str) – Callback name.

Return type:

None

Examples

>>> class WithCallback(MixinCallback):
...     def __init__(self):
...         super().__init__()
...         self.attribute_a = "a"
>>> with_callback = WithCallback()
>>> with_callback.register_callback(
...     "attribute_a", "callback", lambda s, n, v: v
... )
>>> with_callback.callbacks
defaultdict(<class 'list'>, {'attribute_a': [Callback(name='callback', function=<function <lambda> at 0x...>)]})
>>> with_callback.unregister_callback("attribute_a", "callback")
>>> with_callback.callbacks
defaultdict(<class 'list'>, {})
__weakref__#

list of weak references to the object