Skip to main content

Delegable is a simple Python alternative to Ruby on Rails' delegate module.

Project description

Delegable

Summary

Delegable is a simple Python alternative to Ruby on Rails' delegate module, which makes delegation easy. It enables any class to delegate its functions to an instance of another one. Here's an example:

@delegable
class Que:
    def __init__(self):
        self.q = []
        self.delegate('append', 'pop', to='q')

que = Que()
que.append(1)
que.pop()
# => 1

Features:

  • Inheritance Safe
  • No dependent libraries
  • Available for py35, py36, py37, py38

Install

pip install delegable

Getting Started

Basic Usage

Place @delegable.delegator on the top of a class in which you want to enable delegation.

import delegable

@delegable.delegator
class Que:
    def __init__(self, name='default_queue'):
        self._name = name
        self.q = []
        self.delegate('append', 'pop', to='q')

    @property
    def all(self):
        return self.q


que = Que('')
que.append(1)
que.append(2)
que.pop(0)
assert que.all == [2]

Inheritance Safe

It works well with classes inheriting its parent which uses @delegable.delegtor without any problem.

class Que2(Que):
    def __init__(self, name='default_queue'):
        super().__init__(name)

que1 = Que('')
que2 = Que2('')
que1.delegate('append', 'pop', to='q')
que2.delegate('append', 'pop', to='q')
que1.append(1)
que2.append(2)
assert que1.all == [1]
assert que2.all == [2]

delegates

The decorator offers a property, delegates to define delegates all at once. Note it overwrites whole delegates but doesn't merge delegates.

@delegable.delegator
class Que:
    def __init__(self, name='default_queue'):
        self._name = name
        self.q = []
        self.delegate('append', 'pop', to='q')
        self.delegates = {'s': 'join'}

assert que.join('ab') == 'a,b'

que.append(1)
# => AttributeError("'Que' object has no attribute 'append'")

How to Test

This project uses tox to test it against different versions of Python, with underlying pyenv.

Set up

You have to have pyenv installed to run tox in this project.

Test

To test with all versions:

tox

To test with a certain version:

tox -e py37

or

pyenv shell 3.7.7
pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

delegable-0.0.5-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file delegable-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: delegable-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for delegable-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2c0bd70a310cc2d0b7083f774830a7be7dae4187175dcfc42591b3beb56d178a
MD5 b3b8c4f19c7c054d913776614016c49f
BLAKE2b-256 1eef0f0f093456d032130f236bca8500c13a2a58fb465de6a50de239f7bfeb89

See more details on using hashes here.

Supported by

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