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

Example Usage

Subscribing to and calling an event

from python_event_bus import EventBus

@EventBus.on("example_event")
def on_example_event():
    print("Hello, World!")

EventBus.call("example_event")

Output

Hello, World!

Unsubscribing from an event

Unsubscribing from an event will no longer invoke the subscribed method when the event is called.

from python_event_bus import EventBus

@EventBus.on("example_event")
def on_example_event():
    print("Hello, World!")

EventBus.unsubscribe("example_event", on_example_event)
EventBus.call("example_event") # on_example_event will not be called because the subscription is no longer active.

Calling an event with a single positional argument

from python_event_bus import EventBus

@EventBus.on("example_event")
def on_example_event(data):
    print(f"Received data: {data}")

EventBus.call("example_event", True)

Output

Received data: True

Calling an event with *args

from python_event_bus import EventBus

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

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

Output

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

Calling an event with **kwargs

from python_event_bus import EventBus

@EventBus.on("example_event")
def on_example_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("example_event", False, value = 10, text = "Hello")

Output

Event called with the following arguments:
 - False
 - value = 10
 - text = Hello

Subscribing a method to an event without a decorator

from python_event_bus import EventBus

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

EventBus.subscribe("example_event", on_example_event) # on_example_event is subscribed to the event "example_event"
EventBus.call("example_event")

Output

Hello, World!

Using the event bus throughout different files

main.py

from python_event_bus import EventBus
from test import call_example_event

@EventBus.on("example_event")
def on_example_event(data):
    print(f"Example event called with data: {data}")

call_example_event()

test.py

from python_event_bus import EventBus

def call_example_event():
    EventBus.call("example_event", "Hello from test.py")

Output

Example event called with data: Hello from test.py

Event callbacks with different priorities

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

from python_event_bus import EventBus

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

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

Output

High priority
Default priority
Low priority

Using the event bus context manager

Note that existing methods such as subscribe, unsubscribe, and call will still function correctly inside of a context manager.

from python_event_bus import EventBus, EventBusContextManager

with EventBusContextManager() as event_bus:
    # Use the context manager to create methods
    # that are subscribed to an event only for
    # the lifetime of the context-managed bus.
    @event_bus.on("example_event")
    def on_example_event(data):
        print(f"Example event was called. Data: {data}")
        
    # Use the EventBus class to call events even if they are created within the context manager.
    EventBus.call("example_event", 1)
    
# All methods that are subscribed to events during the lifetime of the context
# manager will be automatically unsubscribed when the context manager exits.
# The subscription on_example_event that was created within the context manager
# is no longer active.
EventBus.call("example_event", 2) # on_example_event will not be called.

Output

Example event was called. Data: 1

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.2.0.tar.gz (4.2 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.2.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_event_bus-1.2.0.tar.gz
  • Upload date:
  • Size: 4.2 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.2.0.tar.gz
Algorithm Hash digest
SHA256 31e1f4b41db29f7b0409cf27db80d14d1443e760493990a990e479b5e7bf1297
MD5 0638870441a1f7a1e10afd8ee511429e
BLAKE2b-256 b2e24bfc943ee649e7998e7366f00947b9004e514a3ad654bb5ff67550bd1081

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_event_bus-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8359a4ed423fb5b558eb7e87765085b85be8cd610e32cef2063b236ea0b840f7
MD5 8d9ef44e72a39faa37505d898d4f88bb
BLAKE2b-256 eea04a1f1aef8e7dbc4f5e0154f27dba7ac03a7d56f2be945f2d7a32f7c31fdf

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