Pyjon.Events is an easy-to-use event dispatcher
Project description
What is it ?
Pyjon.Events is an easy-to-use event dispatcher metaclass for your objects : Just add it to any of your class, and you can add event listeners and dispatch events.
You can define arguments passed to your listener when you add it. You can also define argument passed to the listener when emiting an event.
Example
For our example, we will define a person class. Just import the module, and add it to your class (Python2 example):
from pyjon.events import EventDispatcher class Person(object): __metaclass__ = EventDispatcher def __init__(self, name): # just a sample initialization, you can do whatever you want, of course. self.name = name def run(self, meters=1): print "I'm running for %d meters !" % meters self.emit_event('ran', meters) self.emit_event('moved', meters=meters) def walk(self, meters=1): print "I'm walking for %d meters !" % meters self.emit_event('walked', meters) self.emit_event('moved', meters=meters) def sleep(self): print "sleeping..." self.emit_event('sleeping') time.sleep(5) self.emit_event('sleeped') print "Wow... had a good night !"
For Python3, just replace this:
class Person(object): __metaclass__ = EventDispatcher
with this:
class Person(metaclass = EventDispatcher):
Now, let’s instanciate our class and subscribe to events:
>>> henry = Person('henry') >>> def handle_movement(meters=None): ... print "he moved for %d meters" % meters >>> def handle_person_movement(who, meters=None): ... print "%s moved for %d meters" % (who, meters) >>> henry.add_listener('moved', handle_movement) >>> henry.add_listener('moved', handle_person_movement, "henry") >>> henry.walk(5) I'm walking for 5 meters ! he moved for 5 meters henry moved for 5 meters >>> def handle_advanced_movement(meters, who, movement_type): ... print "%s %s for %d meters" % (who, movement_type, meters) >>> henry.add_listener('walked', handle_advanced_movement, "henry", "walked") >>> henry.add_listener('ran', handle_advanced_movement, "henry", "ran") >>> henry.walk(5) I'm walking for 5 meters ! henry walked for 5 meters he moved for 5 meters henry moved for 5 meters >>> henry.run(5) I'm running for 5 meters ! henry ran for 5 meters he moved for 5 meters henry moved for 5 meters
That’s simple isn’t it ?
Changelog
1.0 - Unreleased
Initial release
1.1 - First public release
Support of events with keyword arguments
Added documentation
1.1.1 - Bugfix release
MANIFEST.in fix
Docs fixes
1.2 - Python 3 compatible !
Now works with Python 3 !
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file pyjon.events-1.2.tar.gz
.
File metadata
- Download URL: pyjon.events-1.2.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 190f6085bdc4f1b9ea78282a5b93a5afae6bc81cc87b8885c27c27712a361d31 |
|
MD5 | 9d137a9d1c899594e8215f07e7e6f1c9 |
|
BLAKE2b-256 | 3b5c9119de43a3bd65cd52970994eb74e2d7cb4df1c72836930e75c0cfed0b18 |
File details
Details for the file pyjon.events-1.2-py3.4.egg
.
File metadata
- Download URL: pyjon.events-1.2-py3.4.egg
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c82a5c52cd2bfeb2b79aaee1f417efd670087f3a0aaf53de970cb8b95bc8dc47 |
|
MD5 | 316de6c48198ebd9bb478936fd73d8d4 |
|
BLAKE2b-256 | 264caee0a035747a53addd3331efcac8d9bc5525c9cd019cf51283ceee37c5ec |
File details
Details for the file pyjon.events-1.2-py2.7.egg
.
File metadata
- Download URL: pyjon.events-1.2-py2.7.egg
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8c20b3ca0b17c3448aaf29dcf3fb9e2baf6bf1d4d9c001e84549adc70cd1498 |
|
MD5 | f76d976ff0acc86583200908d79f2d7e |
|
BLAKE2b-256 | 4122570cc7003da280cdf324ae5d7bf2bcb55d59467fe35b29a2d3a829832b39 |