Skip to main content

Python event handling using decorators

Project description

Decovent is a small Python library that allows an easy and elegant event rising and handling, using decorators.

New in this version:
  • Private methods have been made public to facilitate inheritance

Basic example:

from decovent import *         #required import

class Mouse(object):           #no special inheritance required
def __init__(self):
self.on_click()          #register handler (no arguments)

@raise_event()               #raises event *click*
def click(self, x, y):
return (x, y)

@raise_event()               #raises event *move*
def move(self, x, y): pass   #the event can be an empty method

@set_handler('click')        #handles event *click*
def on_click(self, x, y):    #arguments will be sent by the event
return (x, y)

>> mouse = Mouse()             #the handler is registered
>> e, h =, 20)  #*click* event is raised and results
>>                             #are returned as tuple in e and h
>> mouse.move(30, 40)          #*move* event is raised, but unhandled
  • Decovent has been tested with Python’s both productive versions, Python 2.6.4 and Python 3.1.1
  • events and handlers are tied to the local-thread
  • event name is case sensitive, Unicode safe and not required if it equals the decorated method name
  • for an event can be registered as many handlers as necessary
  • handlers are registered for (class, event) pair, to differentiate between events with similar names, but raised by different classes
  • a handler can be registered many times, but will be executed only once for (class, event) pair
  • handlers execution order is the same as the registration order
  • handlers are always executed in parallel threads, synchronous or asynchronous
  • parameters received by the handlers are being sent by the event
  • no arguments are required for a handler at registration time
  • a handler can be flagged to run only once for an event and then unregister itself
  • @classmethods can be raised as events or registered as handlers
  • events and handlers can be memoized at local or global level
  • events and handlers can be synchronized on the same lock
  • the time allocated for the execution of an event or handler is controllable
  • the number of methods that can be executed in parallel is controllable
  • events and handlers must be methods that belong to new-style classes
  • @staticmethods can’t be raised as events or registered as handlers
  • one handler can be registered for only one event

It’s important to understand that events and handlers are not classes but decorated methods that may belong to any new style class. There are no restrictions on the class itself regarding inheritance or the interfaces that are implemented.

Please see the documentation for the full list of features:

Download files

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

Source Distribution (79.4 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page