Package: pyding
pyding 🛎PyDing is a (very) simple but effective event handler.Usage# Import the moduleimportpyding# Attach a handler to an event.@pyding.on("greetings")defgreeter(event):print("Hello there from pyding!")# Call the eventpyding.call("greetings")# Hello there from pyding!Async handlers# Import the moduleimportpydingimportasyncio# Attach a handler to an event.@pyding.on("greetings",is_async=True)asyncdefgreeter(event):print("Hello there from pyding!")# Call the eventasyncio.run(pyding.async_call('greetings'))# Hello there from pyding!Waiting for eventsYou can wait for events to be called from another part from your code without having to create a handler by usingpyding.wait_for# Import the moduleimportpydingfromthreadingimportThreadfromtimeimportsleepdefrandom_calls():# Keep calling the event every 10 seconds.whileTrue:pyding.call("random_event")sleep(10)# Start the threadthread=Thread(target=random_calls,daemon=True)thread.start()# Wait for the event to be calledevent_inputs=pyding.wait_for("random_event")# event_inputs = {'event': EventCall object, ... any other keyargs here ... }QueuesQueues are populated from events, useful for multiple entry points approaches withpyding.queueimportpydingfromthreadingimportThreaddeflistener(event):queue=pyding.queue("foo")whileTrue:event=queue.get()# do stuff here...# Start the listenerThread(target=listener,daemon=True).start()# Call the eventevent=pyding.call("check",cancellable=True)Cancellable eventsYou can also make events that can be cancelled, using thecancellablekeyword forpyding.call⚠️ - Cancelling an event which cannot be cancelled will raisepyding.exceptions.UncancellableEventimportpyding# Attach the handler to an event@pyding.on("check")defchecker(event):# Do stuff# Cancel the eventevent.cancel()# Call the eventevent=pyding.call("check",cancellable=True)event.cancelled# will return TrueHierarchyEvent 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 theblockingkeyword forpyding.callimportpyding# Attach the handler to an event@pyding.on("check",priority=10)defchecker_one(event):print("I got executed!")@pyding.on("check",priority=0)defchecker_two(event):print("Me too")# Call the eventevent=pyding.call("check")# I got executed!# Me tooimportpyding# Attach the handler to an event@pyding.on("check",priority=10)defchecker_one(event):print("I got executed!")event.cancel()@pyding.on("check",priority=0)defchecker_two(event):# This won't be executed at first since it got cancelled by checker_oneprint("Me too")# Call the eventpyding.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 tooevent.cancelled# True🛑 - You can also stop the event execution by usingevent.stop()Dealing with the responseEvents can return values, which will be attributed toevent.responseandevent.responsesimportpyding# Attach the handler to an event@pyding.on("greetings")defgreeter(event):return"Hello World!"# Call the eventevent=pyding.call("greetings")event.response# Hello World!event.responses# ['Hello World!']Using argumentsArguments can be passed onto the handlers throughpyding.callimportpyding# Attach the handler to an event@pyding.on("greetings")defgreeter(event,name):returnf"Hello{name}!"# Call the eventevent=pyding.call("greetings",name="John Doe")event.response# Hello John Doe!Essential arguments can be passed to@pyding.onto make sure the handler only will be called if they're met.importpyding# Attach the handler to an event@pyding.on("greetings",name="John Doe")defjohn_doe_greeter(event,name,time):returnf"Hello{name}! It's currently{time}"# Call the eventevent_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 eventevent=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 classesObjects can have methods that act as an event handler.importpydingclassMyClass(pyding.EventSupport):def__init__(self,name):self.register_events()self.name=name@pyding.on("my_event")defevent_handler(self,event):print(f"Hello World from MyClass! My name is{self.name}.")# Nothing will happen because there is no instance of MyClasspyding.call("my_event")myclass=MyClass("foo")pyding.call("my_event")# "Hello world from MyClass! My name is foo."Dealing with Event SpacesEvent spaces allow to separate event handlers.# Import the moduleimportpyding# Create an Event Spacemyspace=pyding.EventSpace()# Attach a handler to an event.@myspace.on("greetings")defgreeter(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 handlersEvent spaces allow you to unregister handlers.# Import the moduleimportpyding# Attach a handler to an event.@on("greetings")defgreeter(event):print("Hello there from myspace's event space!")# Unregister eventpyding.unregister_handler(greeter)# Orgreeter.unregister()# You can also remove handlers from other modules by using pyding.unregister_from_module(module)