Skip to main content

Simpĺe but effective event framework

Project description

pyding 🛎

PyDing is a (very) simple but effective event handler.

Feito por João Bernardi



Usage

# Import the module
import pyding

# Attach a handler to an event.
@pyding.on("greetings")
def greeter(event):
    print("Hello there from pyding!")

# Call the event
pyding.call("greetings")

# Hello there from pyding!

Async handlers

# Import the module
import pyding
import asyncio

# Attach a handler to an event.
@pyding.on("greetings", is_async=True)
async def greeter(event):
    print("Hello there from pyding!")

# Call the event
asyncio.run(pyding.async_call('greetings'))

# Hello there from pyding!

Waiting for events

You can wait for events to be called from another part from your code without having to create a handler by using pyding.wait_for

# Import the module
import pyding
from threading import Thread
from time import sleep

def random_calls():
    # Keep calling the event every 10 seconds.
    while True:
        pyding.call("random_event")
        sleep(10)

# Start the thread
thread = Thread(target=random_calls, daemon=True)
thread.start()

# Wait for the event to be called
event_inputs = pyding.wait_for("random_event")
# event_inputs = {'event': EventCall object, ... any other keyargs here ... }

Queues

Queues are populated from events, useful for multiple entry points approaches with pyding.queue

import pyding
from threading import Thread

def listener(event):
    queue = pyding.queue("foo")
    while True:
        event = queue.get()
        # do stuff here...

# Start the listener
Thread(target=listener, daemon=True).start()

# Call the event
event = pyding.call("check", cancellable=True)

Cancellable events

You can also make events that can be cancelled, using the cancellable keyword for pyding.call

⚠️ - Cancelling an event which cannot be cancelled will raise pyding.exceptions.UncancellableEvent

import pyding

# Attach the handler to an event
@pyding.on("check")
def checker(event):
    # Do stuff    
    # Cancel the event
    event.cancel()

# Call the event
event = pyding.call("check", cancellable=True)

event.cancelled
# will return True

Hierarchy

Event handlers can have an priority attached to them. If the event is cancelled, it will not execute the next handlers. This behavior can be changed by the blocking keyword for pyding.call

import pyding

# Attach the handler to an event
@pyding.on("check", priority=10)
def checker_one(event):
    print("I got executed!")


@pyding.on("check", priority=0)
def checker_two(event):
    print("Me too")


# Call the event
event = pyding.call("check")

# I got executed!
# Me too
import pyding

# Attach the handler to an event
@pyding.on("check", priority=10)
def checker_one(event):
    print("I got executed!")
    event.cancel()


@pyding.on("check", priority=0)
def checker_two(event):
    # This won't be executed at first since it got cancelled by checker_one
    print("Me too")


# Call the event
pyding.call("check", cancellable=True)

# I got executed!

# Call the event and do not break if the event is cancelled.
event = pyding.call("check", cancellable=True, blocking=False)

# I got executed!
# Me too

event.cancelled
# True

🛑 - You can also stop the event execution by using event.stop()

Dealing with the response

Events can return values, which will be attributed to event.response and event.responses

import pyding

# Attach the handler to an event
@pyding.on("greetings")
def greeter(event):
    return "Hello World!"


# Call the event
event = pyding.call("greetings")

event.response
# Hello World!

event.responses
# ['Hello World!']

Using arguments

Arguments can be passed onto the handlers through pyding.call

import pyding

# Attach the handler to an event
@pyding.on("greetings")
def greeter(event, name):
    return f"Hello {name}!"


# Call the event
event = pyding.call("greetings", name="John Doe")

event.response
# Hello John Doe!

Essential arguments can be passed to @pyding.on to make sure the handler only will be called if they're met.

import pyding

# Attach the handler to an event
@pyding.on("greetings", name="John Doe")
def john_doe_greeter(event, name, time):
    return f"Hello {name}! It's currently {time}"


# Call the event
event_two = pyding.call("greetings", name="John Bar", time="10 AM")
# There won't be any response since the handler won't be called since the 'name' essential keyword wasn't equal to 'John Doe'.
event_two.response
# None

# Call the event
event = pyding.call("greetings", name="John Doe", time="10 AM")

event.response
# "Hello John Doe! It's currently 10 AM"

# You can also raise pyding.exceptions.UnfulfilledException if you add requirement_exceptions=True to pyding.on decorator.

Events within classes

Objects can have methods that act as an event handler.

import pyding


class MyClass(pyding.EventSupport):
    def __init__(self, name):
        self.register_events()
        self.name = name

    @pyding.on("my_event")
    def event_handler(self, event):
        print(f"Hello World from MyClass! My name is {self.name}.")

# Nothing will happen because there is no instance of MyClass
pyding.call("my_event")

myclass = MyClass("foo")

pyding.call("my_event")
# "Hello world from MyClass! My name is foo."

Dealing with Event Spaces

Event spaces allow to separate event handlers.

# Import the module
import pyding

# Create an Event Space
myspace = pyding.EventSpace()

# Attach a handler to an event.
@myspace.on("greetings")
def greeter(event):
    print("Hello there from myspace's event space!")

# Calling the event from the global space won't trigger any handler from myspace.
pyding.call("greetings")

# Calling the event from myspace will trigger the "greeter" handler.
myspace.call("greetings")
# Hello there from myspace's event space!

Removing handlers

Event spaces allow you to unregister handlers.

# Import the module
import pyding

# Attach a handler to an event.
@on("greetings")
def greeter(event):
    print("Hello there from myspace's event space!")

# Unregister event
pyding.unregister_handler(greeter)
# Or
greeter.unregister()
# You can also remove handlers from other modules by using pyding.unregister_from_module(module)

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

pyding-1.8.3.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

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

pyding-1.8.3-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file pyding-1.8.3.tar.gz.

File metadata

  • Download URL: pyding-1.8.3.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for pyding-1.8.3.tar.gz
Algorithm Hash digest
SHA256 0b34af56a1b88294d5d63f453718387ebdc6e9f463c486272f591a8789ffc5dd
MD5 08d8ab7085aad4b5d5fc2ae15cbaaf5d
BLAKE2b-256 b3a6049f0a42723ac65a708b37b57ca824b0dae094b1523398c4b44e3bf52041

See more details on using hashes here.

File details

Details for the file pyding-1.8.3-py3-none-any.whl.

File metadata

  • Download URL: pyding-1.8.3-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for pyding-1.8.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2f684fe7781cea6ccc9420601bcd9ffdf9a4dbb2a6a3a3cbdde753cd3579da73
MD5 6d20b8bd4b3fab692f41ef47ce573189
BLAKE2b-256 9cc9b42cc8c98b42b6789f57e101535e5d0a46bd75107b58140dbd789745e580

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