Skip to main content

A simple way to inject composition through inheritance syntax.

Project description

PyCompose

A simple way to inject composition through inheritance syntax.

PyPI - Version PyPI - Status PyPI - Python Version PyPI - License

Build Status codecov Code style: black

Inspirations

The following links served as inpiration during the writing of this, and as a in-depth explanation of what actually is composition and its benefits and drawbacks versus inheritance.

Explanation

From the inspirations one can easily see that an older implementation of compositional forwarding exists (forwardable) so why would I do this to myself? In short, I didn't like the syntax introduced by it. I find the syntax provided by this implementation a bit easier to understand as I find adding things that related to properties of a class inside the brackets in the class statement the most logical.

Usage

from pycompose import Compose


class A:
    test1 = "123"

    def hello(self):
        print("A")

    def __repr__(self):
        return "<A>"


class B:
    test2 = "456"

    def hello(self):
        print("B")

    def __repr__(self):
        return "<B>"


class C(
    Compose(A, "test1", ("hello", "helloA")),
    Compose(B, "test2", ("hello", "helloB"), name="b_object"),
):
    ...


c = C()
print(c.test1 + c.test2)  # > 123456
c.helloA()                # > A
c.helloB()                # > B
print(c._a, c.b_object)   # > <A> <B>

Disclaimer

Please do note that this project is but a small abstraction for something that can easily be achieved only a few more lines of hand written code. As such I am aware of the fact that this may seem pretty useless to some people. MIT License

Copyright (c) 2020 Tim Fischer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

pycompose-1.2.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

pycompose-1.2.1-py2.py3-none-any.whl (4.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pycompose-1.2.1.tar.gz.

File metadata

  • Download URL: pycompose-1.2.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for pycompose-1.2.1.tar.gz
Algorithm Hash digest
SHA256 dd0ab35a85752cb82bc95c7d9c6224f7e38861b49d0246129a1ffaccaa567b11
MD5 2442b809d8daa4121ff94490016d0742
BLAKE2b-256 5649f1b8dd328632fcf7670fb731b9c61f02481d405482fd1f6375448b8f7417

See more details on using hashes here.

File details

Details for the file pycompose-1.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pycompose-1.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for pycompose-1.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 19ab3982054c8d6b6a69ffadd266f55a7002416bcb6111f940eeec4200536808
MD5 bb0a9ccbd2444f2fd653c5a574e20dc9
BLAKE2b-256 44887068f81f630a5665b8fe87f99b1f98377206c0218b75af1f3699ec120554

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page