Event handlers with decorators
Project description
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_event
method 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.
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
Built Distribution
File details
Details for the file decev-1.2.0.tar.gz
.
File metadata
- Download URL: decev-1.2.0.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39d74e79cbd737c6a90fdf5930ba26f94aa978b6338e31695eff08f46ae68f29 |
|
MD5 | 8ff8b992f4113955ec0034945c8f2738 |
|
BLAKE2b-256 | 9892e6c585d793ac2fefc95214ab8c84936b5482b934df741ea725488aa1e02b |
File details
Details for the file decev-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: decev-1.2.0-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa9f03de32b8fb4ae74c60496726962f0f0b743abae4cc8857508243c4f24bb7 |
|
MD5 | 29034765b2e37fca51e824b46c2db638 |
|
BLAKE2b-256 | 6f94ae1b6e540845d2fe2234e4b10d571fa2b13cbee8658a2e78f231ef1b4c07 |