Events Manager
Python implementation of the Events Management system (aka Observer pattern).
Getting started
Requirements
- Python >= 3.8
Installation
pip install events-manager
Usage
Listening and emitting an Event
Listener is invoked when the event is emitted.
from asyncio import run, sleep
from events_manager import Event, emit, listen
class FooEvent(Event):
pass
def foo_listener(event: FooEvent):
print(f"'foo_listener' invoked with {event}")
async def main():
listen(FooEvent, foo_listener)
print("Emitting event...")
emit(FooEvent())
# do the other stuff...
await sleep(1)
if __name__ == '__main__':
run(main())
Output:
Emitting event...
'foo_listener' invoked with <__main__.FooEvent object at 0x7fdce6f5a0a0>
Process finished with exit code 0
sync and async listeners
from asyncio import run, sleep
from events_manager import Event, emit, listen
class FooEvent(Event):
pass
async def foo_listener(event: FooEvent):
print(f"'foo_listener' invoked with {event}")
async def main():
listen(FooEvent, foo_listener)
print("Emitting event...")
emit(FooEvent())
# do the other stuff...
await sleep(1)
if __name__ == '__main__':
run(main())
Output:
Emitting event...
'foo_listener' invoked with <__main__.FooEvent object at 0x7f81e76ad0a0>
Process finished with exit code 0
args and kwargs support
The listen method supports also args and kwargs that will be passed to the listened listener.
from asyncio import run, sleep
from events_manager import Event, emit, listen
class FooEvent(Event):
pass
async def foo_listener(event: FooEvent, bar, baz):
print(f"'foo_listener' invoked with {event}, {bar} and {baz}")
async def main():
listen(FooEvent, foo_listener, False, 'bar', baz='baz')
print("Emitting event...")
emit(FooEvent())
# do the other stuff...
await sleep(1)
if __name__ == '__main__':
run(main())
Output:
Emitting event...
'foo_listener' invoked with <__main__.FooEvent object at 0x7fdbd0fa50d0>, bar and baz
Process finished with exit code 0
Register a listener with @on decorator
Instead of calling listen method, you can also use the @on decorator.
from asyncio import run, sleep
from events_manager import Event, emit, on
class FooEvent(Event):
pass
@on(FooEvent)
def foo_listener(event: FooEvent):
print(f"'foo_listener' invoked with {event}")
async def main():
print("Emitting event...")
emit(FooEvent())
# do the other stuff...
await sleep(1)
if __name__ == '__main__':
run(main())
Output:
Emitting event...
'foo_listener' invoked with <__main__.FooEvent object at 0x7fa0a9a47100>
Process finished with exit code 0
Unregister a listener
Call unregister method passing the event type that you want to stop listening and the listener.
from asyncio import run, sleep
from events_manager import Event, emit, listen, unregister
class FooEvent(Event):
pass
def foo_listener(event: FooEvent):
print(f"'foo_listener' invoked with {event}")
async def main():
listen(FooEvent, foo_listener)
print("Emitting first event...")
emit(FooEvent())
# let the event be processed
await sleep(1)
unregister(FooEvent, foo_listener)
print("Emitting second event...")
emit(FooEvent())
# do the other stuff...
await sleep(1)
if __name__ == '__main__':
run(main())
Output:
Emitting first event...
'foo_listener' invoked with <__main__.FooEvent object at 0x7f92c79b9070>
Emitting second event...
Process finished with exit code 0
Development
Run Tests
./test
Style Check
./lint
License
Distributed under the MIT License. See LICENSE file for more information.
Metadata
Release files for events-manager 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| events-manager-0.2.2.tar.gz | 5.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| events_manager-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.7 kB
Release files / events-manager-0.2.2.tar.gz
| Download URL | events-manager-0.2.2.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8c78c93263f1d97cb8275f98a8dab687036bdfa43168119639b99a3ab530a3c6
|
|
BLAKE2b-256 checksum How to use checksums |
6f11ee3e723ca1e459bf14ca8b2c2033b032b1831884832de1c9c6f4c262ddaa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.17
|
Release files / events_manager-0.2.2-py3-none-any.whl
| Download URL | events_manager-0.2.2-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
48d433ff12701c4e774d9f202df0ef27fa2ce76905f46eb1d2f3c37980c0f858
|
|
BLAKE2b-256 checksum How to use checksums |
ebf70a3237b253414f52a57a436f7087f9dfeeee1a59c817f055cafd7a6ae919
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.17
|
Security for events-manager 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.