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 ifDelegateMixin
is used directly without being mixed in.
- class htf.mixins.DelegateMixin
DelegateMixin
definesset
andget
as methods that have to be overwritten by subclass.
- class htf.mixins.DelegateCallbackMixin(set_callback: Callable, get_callback: Callable)
DelegateCallbackMixin
gives the possibility to user define setter and getter methods using aDelegateMixin
.- 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 forDelegateMixin.set
and DelegateMixin.get
to its members if they are instances ofDelegateMixin
.
- The
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)