colour.utilities.MixinCallback#

class colour.utilities.MixinCallback[source]#

Bases: object

A mixin providing support for callbacks.

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 property for the callbacks.

Returns:

Callbacks.

Return type:

defaultdict

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

Set given value to the attribute with given name.

Parameters:
  • attribute – Attribute to set the value of.

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

  • name (str) –

Return type:

None

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

Register the callback with given name for given 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 given name for given 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 (if defined)