Skip to main content

Roles

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://github.com/amolenaar/roles

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

Using Roles

As a basic example, consider a domain class:

>>> class Person:
...     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(metaclass=RoleType):
...     def chop(self):
...          return "chop, chop"

Assign the role to the person:

>>> Carpenter(person)				# doctest: +ELLIPSIS
<__main__.Person+Carpenter object at 0x...>
>>> person					# doctest: +ELLIPSIS
<__main__.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.

Context

Roles make a lot of sense when used in a context. A classic example is the money transfer example. Here two accounts are used and an amount of money is transfered from one account to the other. So, one account playes the role of source account and the other plays the role of target account.

An example can be found in example.py.

Release files for roles 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for roles 1.0.0
File Size Uploaded
roles-1.0.0.tar.gz 7.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for roles 1.0.0
File Interpreter ABI Platform
roles-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.2 kB

Release files / roles-1.0.0.tar.gz

Download URL roles-1.0.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
123d843e38c6e578c15533eb2c7cf98aaf53cbaa9fe78dc318dbc61297e2ed61
BLAKE2b-256 checksum
How to use checksums
c96f7fd955e74bae50565d8a010ea1a4e45aa06841d6d38111ea736879fc7678
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.6 Linux/5.8.0-33-generic

Release files / roles-1.0.0-py3-none-any.whl

Download URL roles-1.0.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d7595df077e7a39db7edc36322b52ea861c7da65be2dd329f8b487a56f37494
BLAKE2b-256 checksum
How to use checksums
505712a193eeeff2f4ab62ef5be98a83c18b24bebbab90a358c7f120b7b32408
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.6 Linux/5.8.0-33-generic

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.10

2 release files

0.9

2 release files

0.8

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page