Delegable is a simple Python alternative to Ruby on Rails' delegate module.
Project description
Delegatable
Summary
Delegable is a simple Python alternative to Ruby on Rails' delegate module, which makes it easy for any class to delegate functions to an instance of another class. 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 on the top of a class you want to make delegable.
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
pyenv global 3.5.9 3.6.10 3.7.7 3.8.2
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for delegable-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c27073c661a4747927d66eccf7a06ebfd5d1d1eafa6db10a6b5e9a68029e9b71 |
|
MD5 | e980eb9aa46566fda1eeb722b8454d95 |
|
BLAKE2b-256 | 5f8e30f1d55c7f16e7b34ccdf67a7d20e88eb0c7ffe76f96c5261a78eeec5d97 |