Giter Club home page Giter Club logo

py-radio-class's Introduction

Radio

Event-bus implementation inspired by backbone.radio.

Install

pip install radio-class

Usage

Here are some small examples that demonstrate how to use it:

on/off/trigger

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.

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

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

License

MIT

py-radio-class's People

Contributors

unclechu avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.