Skip to main content

Role based development

Project description

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.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

py_roles-0.1.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

py_roles-0.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file py_roles-0.1.tar.gz.

File metadata

  • Download URL: py_roles-0.1.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for py_roles-0.1.tar.gz
Algorithm Hash digest
SHA256 b670e83199e02e47e8fd634fd131b422a77562697f9376b7740ffb8d3e20ae25
MD5 dac1dfc5e587a7b531a6b28a31932b49
BLAKE2b-256 218ef24bbe6446011c19407bd39ab29cd37cdfec773c92cd15b34f321392a378

See more details on using hashes here.

File details

Details for the file py_roles-0.1-py3-none-any.whl.

File metadata

  • Download URL: py_roles-0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for py_roles-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6bd58983e77444e6c62e46f6bfdea684dab263b2dd41f82f7305c174dc92b1a1
MD5 2e2a3ea78514fcf426e4f722e9d920e5
BLAKE2b-256 cff3e1c0fb96d9111837fe10e015c65002a8495f8a4bafac4862c433f8ff155a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page