Skip to main content

Library for Role based development.

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.

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
<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'
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.

Release files for py-roles 0.1

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

Source distribution (sdist)

Source distribution for py-roles 0.1
File Size Uploaded
py_roles-0.1.tar.gz 13.8 kB Details

Built distribution (wheel)

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

Total release size: 22.7 kB

Release files / py_roles-0.1.tar.gz

Download URL py_roles-0.1.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b670e83199e02e47e8fd634fd131b422a77562697f9376b7740ffb8d3e20ae25
BLAKE2b-256 checksum
How to use checksums
218ef24bbe6446011c19407bd39ab29cd37cdfec773c92cd15b34f321392a378
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

Release files / py_roles-0.1-py3-none-any.whl

Download URL py_roles-0.1-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6bd58983e77444e6c62e46f6bfdea684dab263b2dd41f82f7305c174dc92b1a1
BLAKE2b-256 checksum
How to use checksums
cff3e1c0fb96d9111837fe10e015c65002a8495f8a4bafac4862c433f8ff155a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

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