delegate-pattern


Namedelegate-pattern JSON
Version 0.0.2 PyPI version JSON
download
home_pageNone
SummaryPython implementation of the Delegation Pattern
upload_time2025-07-14 06:03:34
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseNone
keywords windows linux delegates
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            [![Test](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test.yml/badge.svg)](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test.yml)
[![Coverage](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test-coverage.yml/badge.svg)](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test-coverage.yml)
[![Stable Version](https://img.shields.io/pypi/v/delegate-pattern?label=stable&sort=semver&color=blue)](https://github.com/apmadsen/delegate-pattern/releases)
![Pre-release Version](https://img.shields.io/github/v/release/apmadsen/delegate-pattern?label=pre-release&include_prereleases&sort=semver&color=blue)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/delegate-pattern)
[![PyPI Downloads](https://static.pepy.tech/badge/delegate-pattern/week)](https://pepy.tech/projects/delegate-pattern)

# delegate-pattern: Python implementation of the Delegation Pattern.

delegate-pattern provides a basic implementation of the well-known Delegation Pattern.

## What is delegation

Delegation is a pattern in object oriented programming where a class (delegator) delegates responsibilities to one or more delegates.

This allows for greater code reusability and reduced class complexity and may help adhering to the DRY (Do not Repeat Yourself) and SoC (Separation of Concerns) principles.

## Example

Consider a trivial task of delegating the printing of an objects name to the console. Here the delegator `SomeClass` delegates the task to the delegate `PrintNameDelegate` which is not much more than a function wrapped in a class.

The delegate class is always initialized with an argument specifying its delegator, and while this example shows the use of a protocol class called `NamedClassProtocol`, this is merely included for convenience, and will not be enforced at runtime. In fact the type of `delegator` can be anything, but delegate constructors cannot have any other arguments.

Note that it's strongly recommended to use weak references to the delegator in the delegate constructor.

```python
from typing import Protocol
from weakref import ref
from delegate.pattern import delegate

class NamedClassProtocol(Protocol):
    _name: str

class PrintNameDelegate:
    def __init__(self, delegator: NamedClassProtocol): # delegator type annotation is syntactic sugar and not enforced in any way
        self.delegator = ref(delegator)

    def __call__(self):
        print(self.delegator()._name)

class NamePropertyDelegate:
    def __init__(self, delegator: NamedClassProtocol): # delegator type annotation is syntactic sugar and not enforced in any way
        self.delegator = ref(delegator)

    def __get__(self) -> str:
        return self.delegator()._name

    def __set__(self, value: str):
        self.delegator()._name = value

class SomeClass:
    _name: str
    def __init__(self, name: str) -> None:
        self._name = name

    name_printer = delegate(PrintNameDelegate) # => PrintNameDelegate instance
    name = delegate(NamePropertyDelegate, str) # => string getter

some_instance = SomeClass("Neo")
some_instance.name_printer() # prints Neo

name = some_instance.name # => Neo
some_instance.name = "Trinity"
new_name = some_instance.name # => Trinity
```

## Full documentation

[Go to documentation](https://github.com/apmadsen/delegate-pattern/blob/main/docs/documentation.md)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "delegate-pattern",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "windows, linux, delegates",
    "author": null,
    "author_email": "Anders Madsen <anders.madsen@alphavue.com>",
    "download_url": "https://files.pythonhosted.org/packages/8c/9c/51781b38fed3026824e59a099642fef8cd8fe82c2256c67469a440bded98/delegate_pattern-0.0.2.tar.gz",
    "platform": null,
    "description": "[![Test](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test.yml/badge.svg)](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test.yml)\n[![Coverage](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test-coverage.yml/badge.svg)](https://github.com/apmadsen/delegate-pattern/actions/workflows/python-test-coverage.yml)\n[![Stable Version](https://img.shields.io/pypi/v/delegate-pattern?label=stable&sort=semver&color=blue)](https://github.com/apmadsen/delegate-pattern/releases)\n![Pre-release Version](https://img.shields.io/github/v/release/apmadsen/delegate-pattern?label=pre-release&include_prereleases&sort=semver&color=blue)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/delegate-pattern)\n[![PyPI Downloads](https://static.pepy.tech/badge/delegate-pattern/week)](https://pepy.tech/projects/delegate-pattern)\n\n# delegate-pattern: Python implementation of the Delegation Pattern.\n\ndelegate-pattern provides a basic implementation of the well-known Delegation Pattern.\n\n## What is delegation\n\nDelegation is a pattern in object oriented programming where a class (delegator) delegates responsibilities to one or more delegates.\n\nThis allows for greater code reusability and reduced class complexity and may help adhering to the DRY (Do not Repeat Yourself) and SoC (Separation of Concerns) principles.\n\n## Example\n\nConsider a trivial task of delegating the printing of an objects name to the console. Here the delegator `SomeClass` delegates the task to the delegate `PrintNameDelegate` which is not much more than a function wrapped in a class.\n\nThe delegate class is always initialized with an argument specifying its delegator, and while this example shows the use of a protocol class called `NamedClassProtocol`, this is merely included for convenience, and will not be enforced at runtime. In fact the type of `delegator` can be anything, but delegate constructors cannot have any other arguments.\n\nNote that it's strongly recommended to use weak references to the delegator in the delegate constructor.\n\n```python\nfrom typing import Protocol\nfrom weakref import ref\nfrom delegate.pattern import delegate\n\nclass NamedClassProtocol(Protocol):\n    _name: str\n\nclass PrintNameDelegate:\n    def __init__(self, delegator: NamedClassProtocol): # delegator type annotation is syntactic sugar and not enforced in any way\n        self.delegator = ref(delegator)\n\n    def __call__(self):\n        print(self.delegator()._name)\n\nclass NamePropertyDelegate:\n    def __init__(self, delegator: NamedClassProtocol): # delegator type annotation is syntactic sugar and not enforced in any way\n        self.delegator = ref(delegator)\n\n    def __get__(self) -> str:\n        return self.delegator()._name\n\n    def __set__(self, value: str):\n        self.delegator()._name = value\n\nclass SomeClass:\n    _name: str\n    def __init__(self, name: str) -> None:\n        self._name = name\n\n    name_printer = delegate(PrintNameDelegate) # => PrintNameDelegate instance\n    name = delegate(NamePropertyDelegate, str) # => string getter\n\nsome_instance = SomeClass(\"Neo\")\nsome_instance.name_printer() # prints Neo\n\nname = some_instance.name # => Neo\nsome_instance.name = \"Trinity\"\nnew_name = some_instance.name # => Trinity\n```\n\n## Full documentation\n\n[Go to documentation](https://github.com/apmadsen/delegate-pattern/blob/main/docs/documentation.md)\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python implementation of the Delegation Pattern",
    "version": "0.0.2",
    "project_urls": {
        "repository": "https://github.com/apmadsen/delegate-pattern"
    },
    "split_keywords": [
        "windows",
        " linux",
        " delegates"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "984435a2196864b953c9dcd5f1041a72a9dc415041512edbde00848148d2f1e5",
                "md5": "bd389c43ece84e66a12e5f5babb8369a",
                "sha256": "a450f5d8f771579069d82ebd37a390dac8da9c6fdc18becf0b5083a0c3b89f8e"
            },
            "downloads": -1,
            "filename": "delegate_pattern-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bd389c43ece84e66a12e5f5babb8369a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 7985,
            "upload_time": "2025-07-14T06:03:33",
            "upload_time_iso_8601": "2025-07-14T06:03:33.065387Z",
            "url": "https://files.pythonhosted.org/packages/98/44/35a2196864b953c9dcd5f1041a72a9dc415041512edbde00848148d2f1e5/delegate_pattern-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "8c9c51781b38fed3026824e59a099642fef8cd8fe82c2256c67469a440bded98",
                "md5": "4aa8046fe99983a6bab23fd5c853e393",
                "sha256": "9766b81b4d5d0cb85746812df6aee223f47bc6a3907c7c9f14ad1de9c92cc85f"
            },
            "downloads": -1,
            "filename": "delegate_pattern-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "4aa8046fe99983a6bab23fd5c853e393",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 8516,
            "upload_time": "2025-07-14T06:03:34",
            "upload_time_iso_8601": "2025-07-14T06:03:34.167296Z",
            "url": "https://files.pythonhosted.org/packages/8c/9c/51781b38fed3026824e59a099642fef8cd8fe82c2256c67469a440bded98/delegate_pattern-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-14 06:03:34",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "apmadsen",
    "github_project": "delegate-pattern",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "delegate-pattern"
}
        
Elapsed time: 1.51231s