https://github.com/unclechu/py-radio-class
Python library that provides event-bus inspired by backbone.radio
https://github.com/unclechu/py-radio-class
backbone backbone-marionette class eventbus events library python python2 python3
Last synced: 8 months ago
JSON representation
Python library that provides event-bus inspired by backbone.radio
- Host: GitHub
- URL: https://github.com/unclechu/py-radio-class
- Owner: unclechu
- License: mit
- Created: 2017-06-17T15:42:58.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-17T18:09:11.000Z (almost 9 years ago)
- Last Synced: 2025-05-18T15:44:33.258Z (10 months ago)
- Topics: backbone, backbone-marionette, class, eventbus, events, library, python, python2, python3
- Language: Python
- Homepage: https://pypi.python.org/pypi/radio-class
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
Radio
=====
Event-bus implementation inspired by `backbone.radio`__.
__ https://github.com/marionettejs/backbone.radio
Install
-------
.. code:: bash
pip install radio-class
Usage
-----
Here are some small examples that demonstrate how to use it:
on/off/trigger
~~~~~~~~~~~~~~
.. code:: python
from radio import Radio
radio = Radio()
def foo(arg): print('argument:', arg)
radio.on('bar', foo) # bind 'foo' handler on 'bar' event
radio.trigger('bar', 123) # "argument: 123" will be shown in output
def baz(arg): print('baz handler was triggered')
radio.on('bar', baz) # bind another handler on same event
radio.trigger('bar', 456) # "argument: 456" and then "baz handler was triggered" will be shown in output
radio.off('bar', baz) # unbind previously bound handler
radio.trigger('bar', 789) # now only "argument: 789" will be shown
once
~~~~
You could bind some handler that automatically unbounds after first trigger.
.. code:: python
from radio import Radio
radio = Radio()
def foo(arg): print('argument:', arg)
radio.once('bar', foo) # bind 'foo' handler on 'bar' event
radio.trigger('bar', 123) # "argument: 123" will be shown in output
radio.trigger('bar', 456) # nothing shown in output
request/reply/stopReplying
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
from radio import Radio
radio = Radio()
def sum(a=5, b=10): return a + b
radio.reply('get-sum', sum) # bind 'sum' replier-handler on 'get-sum' requests
x = radio.request('get-sum')
print(x) # 15 will be shown in output (5 + 10 from default arguments)
x = radio.request('get-sum', b=2, a=4)
print(x) # 6 will be shown
x = radio.request('get-sum', 2)
print(x) # 12 will be shown
radio.stopReplying('get-sum', sum)
radio.request('get-sum') # will raise 'ListenerNotFound' exception
Author
------
`Viacheslav Lotsmanov`__
__ https://github.com/unclechu/
License
-------
`MIT`__
__ LICENSE