Giter VIP home page Giter VIP logo

roles's Introduction

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://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 <http://github.com/amolenaar/roles/blob/master/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 <http://github.com/amolenaar/roles/blob/master/example.py>`_ and `example2.py <http://github.com/amolenaar/roles/blob/master/example2.py>`_.

roles's People

Contributors

amolenaar avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.