Skip to main content

Composite Class Decorator

This project contains a generic implementation of the Composite Design Pattern as a Python class decorator.

This is a targeted solution addressing the particular need of creating composite Builders. All existing solutions we observed so far assumed manual reproduction on every abstract method in Composite, which is problematic from a maintenance point of view.

For more technical in-depth discussion please refer to our article: Generic Composite in Python

Usage Example

from abc import ABC, abstractmethod
from typing import Tuple, Any
from pycomposite import composite

class Configuration:
    '''
    Just an example to demostrate side-effect
    '''
    def __init__(self):
        self._configuration = {}
        
    def configure(self, name: str, value: Any) -> None:
        self._configuration[name] = value
        
class Builder(ABC):

    @abstractmethod
    def build_part_one(self, arg: int) -> Tuple: #better coveres a general case
        pass
        
    @abstractmethod
    def build_part_two(self, arg: str) -> Tuple:
        pass
        
    @abstractmethod
    def configure_part_three(self, arg: Configuration) -> None:
        pass

class BuilderA(Builder):

    def build_part_one(self, arg: int) -> Tuple: #better coveres a general case
        return arg*10, arg+5,
        
    def build_part_two(self, arg: str) -> Tuple:
        return f'A: {arg}', 
        
    def configure_part_three(self, arg: Configuration) -> None:
        arg.configure('A', 'A builder')

class BuilderB(Builder):

    def build_part_one(self, arg: int) -> Tuple: #better coveres a general case
        return arg-100, arg*5,
        
    def build_part_two(self, arg: str) -> Tuple:
        return f'B: {arg}', 
        
    def configure_part_three(self, arg: Configuration) -> None:
        arg.configure('B', 'B builder')

@composite
class CompositeBuilder(Builder):
    pass
    
builder = CompositeBuilder(BuilderA(), BuilderB())
config = Configuration()

builder.configure_part_three(config)
assert (70, 12, -93, 35) == builder.build_part_one(7)
assert ('A: high', 'B: high') == builder.build_part_two('high')
assert {'A': 'A builder', 'B': 'B builder'} == config._configuration

Metadata

Release files for pycomposite 1.0.2

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

Source distribution (sdist)

Source distribution for pycomposite 1.0.2
File Size Uploaded
pycomposite-1.0.2.tar.gz 9.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pycomposite 1.0.2
File Interpreter ABI Platform
pycomposite-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / pycomposite-1.0.2.tar.gz

Download URL pycomposite-1.0.2.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5b97bed93a5ad223618e4d8da954dec1773a854ddf0114a84e55f978ad78dd69
BLAKE2b-256 checksum
How to use checksums
609abbb13d6a68b204e5da109e412bc2c470bd3df5f3f05b743563b636869d00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release files / pycomposite-1.0.2-py3-none-any.whl

Download URL pycomposite-1.0.2-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0de04399ff908234725ea7c1a7393a553bd0165c48054c5f505678603bdfc044
BLAKE2b-256 checksum
How to use checksums
76c6d684b6345963066aaaccec025581f56f4b469cb8c701bb101aaa278d796c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

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