monomonedula / abc-delegation Goto Github PK
View Code? Open in Web Editor NEWA tool for automated delegation with abstract base classes
License: MIT License
A tool for automated delegation with abstract base classes
License: MIT License
Document the delegation metaclasses & update examples in readme
Raise an error if the object's delegate is missing a delegated attribute. Implement the same behavior for both single and multi delegate classes
First up super cool project. Takes most of the headache out of delegation.
I was wondering if in the case of multiple inheritance you could specify which class to delegate.
Say your class C inherits from A and B but only require that the delegate implements A.
Right now the only way that seems to work is to add all the methods of B to your Class C as abstract methods, or turn of validation completely.
More concretely I would like to do this:
from abc import ABCMeta, abstractmethod
from abc_delegation import delegation_metaclass
class abc_foo(metaclass=ABCMeta):
@abstractmethod
def foo(self):
pass
class abc_bar(metaclass=ABCMeta):
@abstractmethod
def bar(self):
pass
...
#Potentially many more
class delegate_foo(abc_foo, abc_bar,
metaclass=delegation_metaclass("_foo")):
def __init__(self, foo):
self._foo = foo
class implement_bar(delegate_foo):
def __init__(self, foo):
super().__init__(foo)
def bar(self):
super().bar()
return "bar"
class foo:
def foo(self):
return "foo"
bar = implement_bar(foo())
Without having to write:
class delegate_foo(abc_foo, abc_bar,
metaclass=delegation_metaclass("_foo")):
def __init__(self, foo):
self._foo = foo
@abstractmethod
def bar(self):
pass
...
#Potentially many more
Or turn of validation.
Alternatively making this work would be equally nice
from abc import ABCMeta, abstractmethod
from abc_delegation import delegation_metaclass
class abc_foo(metaclass=ABCMeta):
@abstractmethod
def foo(self):
pass
class abc_bar(metaclass=ABCMeta):
@abstractmethod
def bar(self):
pass
...
#Potentially many more
class delegate_foo(abc_foo, metaclass=delegation_metaclass("_foo")):
def __init__(self, foo):
self._foo = foo
class implement_bar_delegate_foo(abc_bar, delegate_foo):
def __init__(self, foo):
super().__init__(foo)
def bar(self):
return "bar"
class foo:
def foo(self):
return "foo"
bar = implement_bar_delegate_foo(foo())
bar.bar()
bar.foo()
I don't know how difficult either thing is because metaclasses and inheritance gives me headaches, but it feels like it should be possible.
Add badges
Fix wrong class name bug (appears in the errors).
For example, here search_recommended
is a name of one of the attributes, not the class name.
E AttributeError: 'search_recommended' object has no attribute 'search'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.