Skip to main content

Add events to object methods and attributes

Project description

https://pypip.in/v/eventize/badge.png

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 ./
https://secure.travis-ci.org/apieum/eventize.png?branch=master

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

eventize-0.1.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

eventize-0.1-py2.7.egg (8.0 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