Skip to main content

Role based development

Project description

Library for Role based development.

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

The big 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 assigned and revoked. Multiple roles can be applied to an instance. Revocation can happen in any particular order.

Homepage: http://amolenaar.github.com/roles

Releases: http://pypi.python.org/pypi/roles

Using Roles

As a basic example, consider a domain class:

>>> class Person(object):
...     def __init__(self, name):
...         self.name = name
>>> person = Person("John")

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

>>> from roles import RoleType
>>> class Carpenter(object):
...     __metaclass__ = RoleType
...     def chop(self):
...          return "chop, chop"

Assign the role to the person:

>>> Carpenter(person)    # doctest: +ELLIPSIS
<Person+Carpenter object at 0x...>
>>> person                                      # doctest: +ELLIPSIS
<Person+Carpenter object at 0x...>

The person is still a Person:

>>> isinstance(person, Person)
True

… and can do carpenter things:

>>> person.chop()
'chop, chop'

See roles.py for more examples.

Factories

In most cases instances will require specific implementations of a certain role. This can be done by decorating the specific role implementations with the assignto() decorator.

>>> from roles import assignto
>>> @assignto(Person)
... class Biker(object):
...     __metaclass__ = RoleType
...     def bike(self):
...         return 'cycle, cycle'
>>> Biker(person)                               # doctest: +ELLIPSIS
<Person+Carpenter+Biker object at 0x...>

Assigning to a different class instance doesn’t work:

>>> class Cat(object):
...     pass
>>> Biker(Cat())                                # doctest: +ELLIPSIS
Traceback (most recent call last):
  ...
NoRoleError: No role found for <class 'Cat'>

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.7.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

roles-0.7.0-py2.6.egg (11.2 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for roles-0.7.0.tar.gz
Algorithm Hash digest
SHA256 31e1008983d0ad6c73df862b5e02791f5bb37ee7902b0157b2c0da5f78cdf6dc
MD5 23b0cb4a4ff2277a0065412324634a3b
BLAKE2b-256 b6487551ccf37816d377f794f4823dd1f668f2e591f5925d8bda921f74e94717

See more details on using hashes here.

File details

Details for the file roles-0.7.0-py2.6.egg.

File metadata

  • Download URL: roles-0.7.0-py2.6.egg
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for roles-0.7.0-py2.6.egg
Algorithm Hash digest
SHA256 d9c520926e0a97670cec610646cf94edcba33365df0d7a12ecea69c700423484
MD5 9557a26c64d84bc2a3763e74b14e09de
BLAKE2b-256 a6a45774fa82c2279502773d638f82f4d1dbfbecfac01b6aacb84dea137f6426

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