Skip to main content

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.

Metadata

Release files for pycompose 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycompose 1.2.1
File Size Uploaded
pycompose-1.2.1.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pycompose 1.2.1
File Interpreter ABI Platform
pycompose-1.2.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 9.1 kB

Release files / pycompose-1.2.1.tar.gz

Download URL pycompose-1.2.1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
dd0ab35a85752cb82bc95c7d9c6224f7e38861b49d0246129a1ffaccaa567b11
BLAKE2b-256 checksum
How to use checksums
5649f1b8dd328632fcf7670fb731b9c61f02481d405482fd1f6375448b8f7417
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / pycompose-1.2.1-py2.py3-none-any.whl

Download URL pycompose-1.2.1-py2.py3-none-any.whl
Size 4.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
19ab3982054c8d6b6a69ffadd266f55a7002416bcb6111f940eeec4200536808
BLAKE2b-256 checksum
How to use checksums
44887068f81f630a5665b8fe87f99b1f98377206c0218b75af1f3699ec120554
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page