Skip to main content

Pythonic event-driven programming.

Project description

Classy Events

Pythonic event-driven programming.

Dependencies

Python 3.8, 3.9

Install

pip install classy-events

Usage

The main ingredient is an event handler instance:

from classy_events import EventHandler
event_handler = EventHandler()

You can register event listeners for events of any type. In this example we use string events. An event listener is a callable, like a function or a method. The registered event handlers for a specific event are called if that event is dispatched by the event handler. Any keyword arguments that accompany the event are passed to the event listeners.

Function listeners

The listener for the event "spam", no callbacks needed!

@event_handler.on("spam")
def spam(value):
    print(f"on 'spam': '{value}'")

When the "spam" event is dispatched from anywhere in the code, def spam will be called:

event_handler.dispatch("spam", value="ham and eggs")

Now on 'spam': 'ham and eggs' is printed.

Classy listeners

To use instance methods as listeners, only one extra step is needed:

class Spam:
    def __init__(self):
        event_handler.bind(self)

    @event_handler.on("classy_spam")
    def spam(self, value):
        print(f"on '{type(self).__name__}.spam': '{value}'")

spam_instance = Spam()

In __init__, the instance is bound to the handler. Note that this event_handler.bind call is not required to be within the __init__.

To see this in action, we dispatch the "classy_spam" event:

event_handler.dispatch("classy_spam", value="the classiest")

And we see the output on 'Spam.spam': 'the classiest'.

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

classy-events-0.2.8.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

classy_events-0.2.8-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file classy-events-0.2.8.tar.gz.

File metadata

  • Download URL: classy-events-0.2.8.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5

File hashes

Hashes for classy-events-0.2.8.tar.gz
Algorithm Hash digest
SHA256 77942151bbcac38e50efe1e74696ef52fe0484e94abb7ba050baf991b6757890
MD5 bf5cf42cf4e29130d0619691df1404df
BLAKE2b-256 b3366c0ee051f8e12b9808fa534e233d18aac35b589482f7f0792203f2584174

See more details on using hashes here.

File details

Details for the file classy_events-0.2.8-py3-none-any.whl.

File metadata

  • Download URL: classy_events-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5

File hashes

Hashes for classy_events-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 958482c5bf5282d0f7d0cd8d35f5de9611dab730f3707b5e2a8e6df7cc1b5a40
MD5 a785949900c4eddf975e79f0b0acda6b
BLAKE2b-256 80126d678e7206d8d5e2f57d429dce4c6a2fa59531c5d2a49670b20a26ab1f3f

See more details on using hashes here.

Supported by

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