Role based development
Project description
Roles
Library for Role based development.
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 assigned and revoked. Multiple roles can be applied to an instance. Revocation can happen in any particular order.
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): ... NoRoleException: No role found for <class 'Cat'>
Releases
Releases can be found on:
Change Log
0.3.0
Module works for Python 2.6 as well as Python 3.x. Doctests still run under 2.6.
Note that conversion of doctests is trivial with:
$ 2to3 -d -w roles.py
0.2.0
Added psyco_optimize() for optimizing code with psyco.
0.1.0
Initial release: roles.py
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 Distribution
File details
Details for the file roles-0.3.0.tar.gz
.
File metadata
- Download URL: roles-0.3.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 120aa0f94adf51c1509122ad15248d7f95b3121a255fb844f727cb27806d84c6 |
|
MD5 | e6cab357baaf42b9bab226890de68265 |
|
BLAKE2b-256 | 01211d4c9fa75c38d09068ce11916eb4ddedce417050ef509f95ef0f0065b435 |