Skip to main content

Small implementation of Roles

Project description

Pythonic implementation of the DCI (Data Context Interaction) pattern (http://www.artima.com/articles/dci_vision.html).

The difference with mixins is that this role is applied only to the subject instance, not to the subject class (alas, a new class is constructed).

Roles can be applied and revoked. Multiple roles can be applied to an instance. Revocation can happen in any particular order.

As a basic example, consider some domain class:

>>> from roles import RoleType
>>> class DomainClass(object):
...     def __init__(self, a=3):
...         self.a = a
>>> instance = DomainClass()

The instance should participate in a collaboration in which it fulfills a particular role:

>>> class MyRole(object):
...     __metaclass__ = RoleType
...     def rolefunc(self):
...          return self.a
>>> inrole = MyRole(instance)
>>> inrole       # doctest: +ELLIPSIS
<__main__.DomainClass+MyRole object at 0x...>
>>> isinstance(inrole, DomainClass)
True

Now the inrole instance can be invoked with the rolefunc() method as if it was the DomainClass’ one:

>>> inrole.rolefunc()
3

Project details


Download files

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

Source Distribution

roles-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

File details

Details for the file roles-0.1.0.tar.gz.

File metadata

  • Download URL: roles-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for roles-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c5eb113d26ab707ceeb1806432672cdec4992b190ce8325f415d6d387134fb82
MD5 203f3ff26475dad29622a6713fab913f
BLAKE2b-256 2c9c78582b725fd380b0bfd04822f6771a7e6015a9f8c8d35aa6f3ad6ae23a09

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