dele-gator

delegator is a micro-package for defining delegated methods and attributes.


License
MIT
Install
pip install dele-gator==0.1.0

Documentation

dele➣gator

Build Status Coverage Status Codacy Badge

delegator is a micro-package for defining delegated methods and attributes.

In short, it serves to get rid of boilerplate such as the one on the left hand side with the compact notation on the right hand side. It's very similar to delegation syntax in Ruby.

alt text

delegate attribue on the delegating class can be a sequence or a single string with arguments separated by whitespace (similar to namedtuple). Delegatee attribute must be the first argument. If you don't like the style - no worries - there are three interchangeable notations:

Metaclass

class Foo(metaclass=Delegator):
    delegate = 'bar spam ham eggs'
    # or delegate = 'bar', 'spam', 'ham', 'eggs'

Explicit class decorator

@delegate('bar spam ham eggs')
# or @delegate('bar', 'spam', 'ham', 'eggs')
class Foo(metaclass=Delegator):

Implicit class decorator

@delegator
class Foo:
    delegate = 'bar spam ham eggs'
    # or, again: delegate = 'bar', 'spam', 'ham', 'eggs'

Internally, delegation works via simple attribute accessors with __get__, __set__ and __delete__ methods. Setting and deleting works as expected:

foo.spam = 'chopped pork and ham'
foo.spam
# = 'chopped pork and ham'

del foo.spam
foo.spam
# = 'spam' (falls back to `spam` attribute on `Bar`)

For more details, see decorate and Delegated in the source code. For more examples, check out tests.py.

Installation

You can get it from PyPI:

pip install dele-gator # delegator was taken

Or install directly from GitHub:

pip install git+https://github.com/IwoHerka/delegator@master