GithubHelp home page GithubHelp logo

monomonedula / abc-delegation Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 37 KB

A tool for automated delegation with abstract base classes

License: MIT License

Python 100.00%
python python3 delegation abstract-base-class metaclasses utility-library abc-delegation

abc-delegation's People

Contributors

monomonedula avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

jayvdb

abc-delegation's Issues

Multiple Inheritance partial delegation

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.

Fix class name

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'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.