Mixins

Mixins can be used to import code without inheritance.

Delegation

The Delegation mixins give the possibility to automatically delegate to members of a class without calling set() or get() directly.

Delegation is used for hardware abstractors.

class htf.mixins.DelegationError

DelegationError is thrown if DelegateMixin is used directly without being mixed in.

class htf.mixins.DelegateMixin

DelegateMixin defines set and get as methods that have to be overwritten by subclass.

get() Any

The getter for a delegate.

Note

This method is abstract.

set(value: Any) None

The setter for a delegate.

Parameters:

value – the value to be set.

Note

This method is abstract.

class htf.mixins.DelegateCallbackMixin(set_callback: Callable, get_callback: Callable)

DelegateCallbackMixin gives the possibility to user define setter and getter methods using a DelegateMixin.

Parameters:
  • set_callback – the method to be called as a setter.

  • get_callback – the method to be called as a getter.

class htf.mixins.DelegatorMixin
The DelegatorMixin automatically delegates calls for DelegateMixin.set and

DelegateMixin.get to its members if they are instances of DelegateMixin.

Example usage:

class ConcreteDelegate(DelegateMixin):

    def __init__(self, parameter):
        self.parameter = parameter
        self.value = None

    def set(self, value):
        print(self.__class__.__name__ + ".set()", value)
        self.value = value

    def get(self):
        print(self.__class__.__name__ + ".get()")
        return self.value

class AnotherDelegate(ConcreteDelegate):
    pass

class A(DelegatorMixin):

    def __init__(self):
        self.member = ConcreteDelegate("parameter for member")
        self.test = AnotherDelegate("blub")

class NormalMember:

    def __init__(self, parameter):
        self.parameter = parameter
        self.value = None

    def set(self, value):
        print(self.__class__.__name__ + ".set()", value)
        self.value = value

    def get(self):
        print(self.__class__.__name__ + ".get()")
        return self.value

a = A()
print(a.member)
a.member = 1
print(a.member)
a.b = 2
print(a.b)
a.test = 1337
print(a.test)
a.blub = AnotherDelegate("rolf") # a new delegate is instantiated
a.blub = "peter" # now setting a value leads to a delegated call to AnotherDelegate().set("peter")
print(a.blub) # new reading is delegated to it
a.normalMember = NormalMember("normal")
a.normalMember = "test"
print(a.normalMember)