Add events to object methods and attributes
Project description
Add events to object methods and attributes.
Methods support events “before” and “after” Attributes support events: “on_get”, “on_set”, “on_del”
Events can be triggered conditionaly within arguments or user condition.
Table of Contents
Installation
Install it from pypi:
pip install eventize
or from sources:
git clone git@github.com:apieum/eventize.git cd eventize python setup.py install
Usage
Example 1 - observe a method:
from eventize import EventMethod
self_valid = lambda self, *args, **kwargs: self.valid
def not_valid(event):
event.instance.valid = not event.instance.valid
class Observed(object):
is_valid = EventMethod(self_valid)
def __init__(self):
self.valid = False
self.logs=[]
def log(self, event):
self.logs.append(self.log_message(*event.args, is_valid=self.valid))
def log_message(self, *args, **kwargs):
return "Validity Checks with args: '%s', current:'%s'" % (args, kwargs['is_valid'])
my_object = Observed()
my_object.is_valid.before += my_object.log
my_object.is_valid.before.called_with('permute').do(not_valid)
my_object.is_valid.after += my_object.log
assert my_object.is_valid() is False
assert my_object.is_valid('permute') is True
assert my_object.logs == [
my_object.log_message(is_valid=False),
my_object.log_message(is_valid=False),
my_object.log_message('permute', is_valid=False),
my_object.log_message('permute', is_valid=True),
]
Development
Fell free to give feedback or improvements.
Launch test:
git clone git@github.com:apieum/eventize.git cd eventize nosetests --with-spec --spec-color ./
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
eventize-0.1.1.tar.gz
(3.9 kB
view hashes)
Built Distribution
eventize-0.1.1-py2.7.egg
(8.1 kB
view hashes)