Teventlet: a tiny event handler for Python
What is it?
Teventlet has one basic class, whose purpose is to handle subscribers. It does this by leveraging the PubSub pattern.
What problem does this solve?
In order to understand what Teventlet does, you need to understand
the purpose behind events. Events are methods that can be triggered when a user opens a window, sends a message, or does any number of things. When a user opens a window, you could set it to call a method on a class. Rather than hard-coding the method into your system though, you can use events. An event is a handler that essentially handles calling the method you have attached to it. This allows for you to attach and detach events at runtime.
Teventlet works by allowing you to do this; for each event, you create a teventlet object. The system that handles window events calls the WindowOpened event, and does not have to know about what methods are attached to it. Teventlet takes any arguments passed to this event and calls all the callbacks that have been attached to it.
The publish-subscriber design pattern
Publish-Subscriber is an important design pattern when working with events. The publisher is the caller of that event; in our previous example, the system that dispatches window events. Subscribers are all other systems that need to know about windowing events.
Lets take another example; in most games, collision detection is important. This is generally handled by a physics engine that knows where objects are, as well as, among many other tasks, makes sure that two objects don’t collide. So how do you handle it if they do? Events help with this task. With the publisher-subscriber pattern, the collision detection system would call an event on the object. All other systems or objects that need to know when the object hit something else would be the subscribers, and their methods would be called when the object collides with something else.
You can download Teventlet from PyPi, or you can use pip install:
pip install teventlet
Lets try some simple examples. The first, as shown on the project page just adds a number to an argument.
1 2 3 4 5 6 7 8 9
>>> from __future__ import print_function >>> from teventlet import teventlet >>> adder = teventlet(lambda x:print(x+2), lambda x:print(x+3), lambda x:print(x +6)) >>> adder(2) 4 5 8 >>>
Obviously this isn’t the best of examples, but it allows you to see the basic idea. Essentially you can pass multiple callbacks to Teventlet’s constructor, as well as a single callback or None.
Lets create another more practical example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
>>> from teventlet import teventlet >>> class car(object): ... def __init__(self): ... self.onSpeedChange = teventlet() ... self.speed = 50 ... def changeSpeed(self, speed): ... self.speed = speed ... self.onSpeedChange(self.speed) ... >>> def speedChanged(speed): ... if speed <= 0: ... print("The car is stopped.") ... >>> c = car() >>> c.onSpeedChange += speedChanged >>> c.changeSpeed(50) >>> c.changeSpeed(0) The car is stopped. >>>
While this wasn’t the best example, making something more complex would take up a lot more code. This is obviously more useful in larger projects where you need to handle events like player connections, collisions etc.
As always, any questions, comments and suggestions would be welcome. If you have any ideas, please let me know and I will add them to the next release.