Skip to main content

python-event-bus makes communication between python classes easier.

Project description

PyPiVersion SupportedVersions License

Installation

Built and tested on Python 3.10 and above.
No requirements other than the module itself.

pip install python-event-bus
python3 -m pip install python-event-bus

Example Usage

Subscribing to and calling an event

from event_bus import EventBus

@EventBus.on("my_event")
def on_my_event():
    print("Hello, World!")

EventBus.call("my_event")

Output

Hello, World!

Unsubscribing from an event

from event_bus import EventBus

@EventBus.on("my_event")
def on_my_event():
    print("Hello, World!")

EventBus.unsubscribe("my_event", on_my_event)
EventBus.call("my_event") # on_my_event will no longer be called because it was unsubscribed from the event

Calling an event with a single positional argument

from event_bus import EventBus

@EventBus.on("my_event")
def on_my_event(data):
    print(f"Received data: {data}")

EventBus.call("my_event", True)

Output

Received data: True

Calling an event with *args

from event_bus import EventBus

@EventBus.on("my_event")
def on_my_event(*args):
    print("Event called with the following arguments:")
    for argument in args:
        print(f" - {argument}")

EventBus.call("my_event", 1, 2, 3)

Output

Event called with the following arguments:
 - 1
 - 2
 - 3

Calling an event with **kwargs

from event_bus import EventBus

@EventBus.on("my_event")
def on_my_event(*args, **kwargs):
    print("Event called with the following arguments:")
    for argument in args:
        print(f" - {argument}")

    for kw_argument in kwargs:
        print(f" - {kw_argument} = {kwargs[kw_argument]}")

EventBus.call("my_event", False, value = 10, name = "Bob")

Output

Event called with the following arguments:
 - False
 - value = 10
 - name = Bob

Subscribing a method to an event without a decorator

from event_bus import EventBus

def on_my_event():
    print("Hello, World!")

EventBus.subscribe("my_event", on_my_event)
EventBus.call("my_event")

Output

Hello, World!

Event callbacks with different priorities

A higher priority indicates a higher priority of callback. The default priority is 1.

from event_bus import EventBus

@EventBus.on("my_event", priority = 0)
def low_priority():
    print("Low priority")

@EventBus.on("my_event", priority = 5)
def high_priority():
    print("High priority")
    
@EventBus.on("my_event")
def default_priority():
    print("Default priority")
    
EventBus.call("my_event")

Output

High priority
Default priority
Low priority

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

python_event_bus-1.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_event_bus-1.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file python_event_bus-1.1.0.tar.gz.

File metadata

  • Download URL: python_event_bus-1.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for python_event_bus-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d7329dbab3779f8a6acbf3cc5e628da03b0db8f473316f824d31349b2a54c15f
MD5 e587978ed04dc2b506ef451a3a6d2d91
BLAKE2b-256 fcee300abd9836d368c49757353a50d3643288ddb4d9bfe0fb8656c2aea0ac62

See more details on using hashes here.

File details

Details for the file python_event_bus-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_event_bus-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 08acf471c2180631b8c1b1495700d3d03bea36c43356d0da2f3654b531a203ff
MD5 9161454758ed6e210467857f9da78ab8
BLAKE2b-256 4dbcb5deb6b1a4a8c824fc2adabb55aae69027e8f1603b984e1748a76d770194

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page