Skip to main content

DECEV (decorator events) by @dantechguy

A teeny library for event handling which uses decorators for event subscription

Overview

  • Add functions to events with the @event.your_event decorator
  • Add instance methods to events with the @decev.cls class decorator and then the @event.m.your_event or @event.method.your_eventmethod decorator
  • Run events with event.run('your_event')
  • Pass arguments with events with event.run('your_event', 'bar', foo=True)

Installation

Either copy decev/decev.py into your directory, or run

pip install decev

then import into your file with import decev

Usage

1. Create an EventHandler object

import decev
events = decev.EventHandler()

2. Add functions to events

A single function can have multiple events

# add myFunction to firstEvent
@events.firstEvent
def myFunction():
    print('myFunction')

# add myOtherFunction to firstEvent and event_two
@events.firstEvent
@events.event_two
def myOtherFunction():
    print('myOtherFunction')

3. Add methods to events

Use the @decev.cls class decorator and @events.m.your_event to add methods with a self parameter

@decev.cls
class MyClass:      
    # add myMethod to THIRD_EVENT
    @events.m.THIRD_EVENT
    def myMethod(self):
        print('myMethod')

    # add unbound myOtherMethod to THIRD_EVENT
    @events.THIRD_EVENT
    def myOtherMethod():
        print('myOtherMethod')

# create instance of class        
myObject = MyClass()

4. Receive arguments in callbacks

Make sure the parameters match the arguments passed into events.run()

@events.ArgEvent
def myArgFunction(foo, bar=True):
    print(f'myArgFunction foo={foo} bar={bar}')

5. Run events

events.run('firstEvent')
print()
events.run('event_two')
print()
events.run('LAST_EVENT')
print()
events.run('ArgEvent', 100, bar=False)

Which produces this:

> py main.py
myOtherFunction
myFunction

myOtherFunction

myOtherMethod
myMethod

myArgFunction foo=100 bar=False

How it works

All functions added with @events.your_event are subscribed immediately to events's callback dictionary.

As instance methods require the self parameter, they can only be subscribed once the class has been instantiated and self has a value. The alternate @events.m.your_event syntax instead tags the method, storing the event names and event handler. The @decev.cls class decorator then inserts a code snippet into the object's __init__ method to subscribe the events on instantiation.

Metadata

Release files for decev 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for decev 1.2.0
File Size Uploaded
decev-1.2.0.tar.gz 3.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for decev 1.2.0
File Interpreter ABI Platform
decev-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.5 kB

Release files / decev-1.2.0.tar.gz

Download URL decev-1.2.0.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
39d74e79cbd737c6a90fdf5930ba26f94aa978b6338e31695eff08f46ae68f29
BLAKE2b-256 checksum
How to use checksums
9892e6c585d793ac2fefc95214ab8c84936b5482b934df741ea725488aa1e02b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/54.2.0 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

Release files / decev-1.2.0-py3-none-any.whl

Download URL decev-1.2.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fa9f03de32b8fb4ae74c60496726962f0f0b743abae4cc8857508243c4f24bb7
BLAKE2b-256 checksum
How to use checksums
6f94ae1b6e540845d2fe2234e4b10d571fa2b13cbee8658a2e78f231ef1b4c07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/54.2.0 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.0.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page