mafw.active

Module implements active variable for classes.

Module Attributes

ActiveType

A type for templating the Active class.

ActivableType

A type for all classes that can include an Active.

Classes

Active([default])

A descriptor class to make class variable active.

class mafw.active.ActivableType

A type for all classes that can include an Active.

alias of TypeVar(‘ActivableType’)

class mafw.active.Active(default: ActiveType | None = None)[source]

Bases: Generic[ActiveType]

A descriptor class to make class variable active.

When assigned to a class variable, any change of this value will trigger a call back to a specific function.

Here is a clarifying example.

class Person:
    age = Active()

    def __init__(self, age):
        self.age = age

    def on_age_change(self, old_value, new_value):
        # callback invoked every time the value of age is changed
        # do something with the old and the new age
        pass

    def on_age_set(self, value):
        # callback invoked every time the value on age is set to the same
        # value as before.
        pass

    def on_age_get(self, value):
        # callback invoked every time the value of age is asked.
        # not really useful, but...
        pass

Once you have assigned an Active to a class member, you need to implement the callback in your class. If you do not implement them, the code will run without problems.

The three callbacks have the signature described in the example.

  • on_[var_name]_change(self, old, new)

  • on_[var_name]_set(self, value)

  • on_[var_name]_get(self, value)

Constructor parameter:

Parameters:

default (ActiveType) – Initial value of the Active value. Defaults to None.

class mafw.active.ActiveType

A type for templating the Active class.

alias of TypeVar(‘ActiveType’)