GithubHelp home page GithubHelp logo

jayvdb / abc-delegation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from monomonedula/abc-delegation

0.0 2.0 0.0 38 KB

A tool for automated delegation with abstract base classes

License: MIT License

Python 100.00%

abc-delegation's Introduction

abc-delegation

Codeship Status for monomonedula/abc-delegation codecov PyPI version

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

The metaclasses also enable optional validation of the delegate attributes to ensure they have all of the methods required by the parent object.

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"

Validation

class A(metaclass=ABCMeta):
    @abstractmethod
    def bar(self):
        pass

    @abstractmethod
    def foo(self):
        pass

class B:
    pass

# validation is on by default
class C(A, metaclass=delegation_metaclass("_delegate")):
    def __init__(self, b):
        self._delegate = b

    def foo(self):
        return "C foo"

C(B())
# Trying to instantiate C class with B delegate which is missing 'bar' method
# Validation raises an error:
# TypeError: Can't instantiate bar: missing attribute bar in the delegate attribute _delegate

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"

Please refer to the unit tests for more examples.

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.