A tool for automated delegation with abstract base classes
Project description
abc-delegation
A tool for automated delegation with abstract base classes.
This metaclass enables creation of delegating classes inheriting from an abstract base class.
This technique is impossible with regular __getattr__
approach for delegation,
so normally, you would have to define every delegated method explicitly.
Not any more
Installation:
pip install abc-delegation
Basic usage:
from abc import ABCMeta
from abc_delegation import delegation_metaclass
class A(metaclass=ABCMeta):
@abstractmethod
def bar(self):
pass
@abstractmethod
def foo(self):
pass
class B:
def bar(self):
return "B bar"
def foo(self):
return "B foo"
class C(A, metaclass=delegation_metaclass("my_delegate")):
def __init__(self, b):
self.my_delegate = b
def foo(self):
return "C foo"
c = C(B())
assert c.foo() == "C foo"
assert c.bar() == "B bar"
Multiple delegates:
from abc import ABCMeta
from abc_delegation import multi_delegation_metaclass
class A(metaclass=ABCMeta):
@abstractmethod
def bar(self):
pass
@abstractmethod
def foo(self):
pass
@abstractmethod
def baz(self):
pass
class B:
def bar(self):
return "B bar"
def foo(self):
return "B foo"
class X:
def baz(self):
return "X baz"
class C(A, metaclass=multi_delegation_metaclass("_delegate1", "_delegate2")):
def __init__(self, d1, d2):
self._delegate1 = d1
self._delegate2 = d2
def foo(self):
return "C foo"
c = C(B(), X())
assert c.bar() == "B bar"
assert c.foo() == "C foo"
assert c.baz() == "X baz"
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
abc-delegation-0.2.2.tar.gz
(3.0 kB
view hashes)
Built Distribution
Close
Hashes for abc_delegation-0.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5f3bdfc0b7b88aeb78eed6760ea7ac0f6ec54f322e57e170ea179393deb8a2e |
|
MD5 | 10e7cf8532946bcde1bee1adb7209fd9 |
|
BLAKE2b-256 | 57751190dcfb27209a6d39472e5bd05bee3b1a03814bda4b60be33822539f4cf |