Skip to main content

Python does Roles

Project description

roleplay: Python does Roles

Version:

0.8

Roleplay is an in-progress implementation of roles for Python. The current state is that it passes the Perl6-Roles test suite, which is not the final authority on Perl6 roles, but a Perl5 implementation.

Synopsis

Creating Roles

>>> from roleplay.role import Role
>>> class LoadFrobulatorRole(Role):
...     '''
...         Simple role example.
...     '''
...
...     # Roles can use the '__requires__' attribute to define a set
...     # of attributes/methods the class using the role has to
...     # define (or else it would get an exception).
...
...     __requires__ = ["has_frobulator"]
...
...     def save_frobulator(self, data):
...
...         # Do something with data
...         # .....
...         print "saving frobulator..."
...
...
>>> class SaveFrobulatorRole(Role):
...     '''
...         Another role example
...     '''
...
...
...     def load_frobulator(self, article_id):
...
...         # Do something with data
...         # .....
...         print "loading frobulator..."
...
...

Using Roles

>>> from roleplay import has_role, does
>>> class Article(object):
...     '''
...         This is our class using the roles.
...     '''
...
...     def __init__(self):
...         pass
...
...     def load_article(self, id):
...
...         if does(self, LoadFrobulatorRole):
...             self.load_frobulator(id)
...         # ... do other loading stuff ... #
...
...     def save_article(self, data):
...
...         if does(self, SaveFrobulatorRole):
...             self.save_frobulator(data)
...         # ... do other saving stuff ... #
...
...
...     # This is the requirement for RoleA.
...     def has_frobulator(self):
...         frobulator = config.lookup('frobulator')
...         return frobulator
...
...
... has_role(Article, LoadFrobulatorRole, SaveFrobulatorRole)
...
...
... article = Article()
...
... art = article.load_article(13)
... article.save_article(art)
...

Installation

To install:

>>> python ./setup.py install

Or via easy_install:

>>> easy_install roleplay

Acknowledgements

Thanks to Rob Kinyon and Stevan Little for the Perl6-Roles test suite, hope you don’t mind me porting it to python :)

Project details


Download files

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

Source Distribution

roleplay-0.8.tar.gz (10.7 kB view hashes)

Uploaded Source

Built Distribution

roleplay-0.8-py2.5.egg (32.8 kB view hashes)

Uploaded Source

Supported by

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