Skip to main content

This minimal yet powerful library allows delegation of an objects: methods, properties, and his own member objects, to an object who owns it. Recursive delegation is also supported.Use the simple API to handle delegations.

Project description

Delegate objects powerfully

This minimal yet powerful library allows delegating an object's: methods, properties, and his own member objects, to its owner. Recursive delegation is also supported.

Use the simple API to handle delegations.

From wikipedia, what is delegation:

In object-oriented programming, delegation refers to evaluating a member (property or method) of one object (the receiver) in the context of another original object (the sender). Delegation can be done explicitly, by passing the sending object to the receiving object, which can be done in any object-oriented language; or implicitly, by the member lookup rules of the language, which requires language support for the feature.

Installation

This is python3 only with no dependencies, simply:

pip install ObjectDelegator

Examples

ObjectDelegator is used by inheriting from Delegeator class. Delegation is then handled via the API, and delegated objects can simply be referred to as if they members of the original class subclassing Delegeator.

Define some dummy classes for the sake of example:

class RabbitHole:
    def __init__(self, txt):
        self.down_we_go = txt

class Foo:
    foo_property = 'hi'
    not_delegated_property = 'bye'
    rabbit = RabbitHole('first rabbit')

    # noinspection PyMethodMayBeStatic
    def foo(self):
        """dummy method"""
        return 'foo'

class Bar:
    rabbit_too = RabbitHole('second rabbit')
    boring = 2

Now define a class which inherits from Delegeator:

class Master(Delegator):
    def __init__(self):
        self.foo_obj = Foo()
        self.bar_obj = Bar()
        self.test = 1

# instantiate a master
master = Master()

Delegate using the set_delegated_members method of the Delegeator:

# use a diciotionary with keys as objects delegating members, and values as their members 
master.set_delegated_members({'foo_obj': ['foo', 'foo_property', 'rabbit'], 'bar_obj': ['rabbit_too']})

Delegation is versatile:

# can delegate methods
master.foo() # => 'foo'
# can delegate properties
master.foo_property # => 'hi'
# or even other objects
master.rabbit.down_we_go # => 'first rabbit'
# and another rabbit hole instance
master.rabbit_too.down_we_go # => 'second rabbit'

API

The API allows to:

  • Delegate objects (see above).
  • Find potential objects which can delegate their members to the master:
master.get_possible_objects_to_delegate_to()  # => ['bar_obj', 'foo_obj', 'test', 'foo', 'foo_property', 'rabbit', 'rabbit_too']
  • Find potential members which can be delegated to the master:
master.get_possible_members_to_delegate()  # => {'bar_obj': ['boring', 'rabbit_too'], 'foo_obj': ['foo', 'foo_property', 'not_delegated_property', 'rabbit'], 'test': ['as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'numerator', 'real', 'to_bytes'], 'foo': [], ...}
  • List currently delegated members:
master.get_delegated_members()  # => {'foo_obj': ['foo', 'foo_property', 'rabbit'], 'bar_obj': ['rabbit_too']}
  • Add further recursive delegation:
# Note that rabbit itself was delegated before
master.set_delegated_members({'rabbit': ['down_we_go']})
master.down_we_go   # => first rabbit
  • Raise a ValueError if we try to delegate to objects of the sae name
# if we try to delegate the second rabbit we get a ValueError as we can't have two members with the same name (down_we_go)
master.set_delegated_members({'rabbit_too': ['down_we_go']})
  • Remove delegations for a specific object:
master.remove_delegations_for_object('rabbit')
# Now we may delegate the other rabbit
master.set_delegated_members({'rabbit_too': ['down_we_go']})
master.down_we_go  # => second rabbit
  • clear all delegations:
master.clear_all_delegations()

Project details


Release history Release notifications | RSS feed

This version

1.1

Download files

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

Source Distribution

ObjectDelegator-1.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

ObjectDelegator-1.1-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file ObjectDelegator-1.1.tar.gz.

File metadata

  • Download URL: ObjectDelegator-1.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

File hashes

Hashes for ObjectDelegator-1.1.tar.gz
Algorithm Hash digest
SHA256 dea282102d29a177f180c77c02b53835989a2284742535066ec9a339a31c0599
MD5 e13b832504b85ba545b6328937e965eb
BLAKE2b-256 9664cf96715adf3fcd0599db0d576f31865277c40e29558b97b55fd55541cff4

See more details on using hashes here.

File details

Details for the file ObjectDelegator-1.1-py3-none-any.whl.

File metadata

  • Download URL: ObjectDelegator-1.1-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

File hashes

Hashes for ObjectDelegator-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a0b2e00239270314109f68ea026c46d465452460213202c82183834ee48b7e0d
MD5 e17f7828c465cebca7b0289206d3b4fd
BLAKE2b-256 b5e1b4c6bf95fdf740f148728d02912250dcbdd19360bb92e07861646469f0ce

See more details on using hashes here.

Supported by

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