Skip to main content

Another message passing library.

Project description

License Travis

bands

Another python messaging library

There are ton of python message passing / signal dispatching libraries out there. Blinker, pysignal, dispatch, pydispatcher, louie, the list goes on and on. All of these libraries are fine. This library is okay too.

Features

  • bound and unbound Channels

  • Pluggable Dispatchers

  • Bands - groups of Channels with their own dispatcher

Working with an unbound Channel

>>> import bands

>>> def on_alert(message):
...    return message.upper()

>>> alert = bands.channel('alert')
>>> alert.connect(on_alert)
>>> alert.send('alert!!')
['ALERT!!']

Working with bound Channel’s

A Channel is bound when it’s parent attribute is set. If you use a channel as a class attribute, each instance of your class will have it’s own bound Channel. This is very similar to the way bound methods in python work, except with bound Channels, you’re gauranteed to get the same bound Channel instance everytime you access it.

>>> import bands

>>> class Component(object):
...     started = bands.channel('started')
...     def __init__(self, name):
...         self.name = name
...         self.started.connect(self.on_started)
...     def on_started(self):
...         return self.name + '.on_started'

>>> Component.started  # doctest:+ELLIPSIS
<unbound Channel at 0x...>(identifier='started')
>>> c1 = Component('one')
>>> c1.started  # doctest:+ELLIPSIS
<bound Channel at 0x...>(identifier='started')
>>> c2 = Component('two')
>>> c2.started  # doctest:+ELLIPSIS
<bound Channel at 0x...>(identifier='started')
>>> c1.started.send()
['one.on_started']
>>> c2.started.send()
['two.on_started']
>>> Component.started.send()
['one.on_started', 'two.on_started']
>>> bands.send('started')
['one.on_started', 'two.on_started']

From the above example, we can see that each bound Channel has it’s own subscribers. Additionally, if you call send on the unbound Channel, all bound channel receivers will also be notified. We can also use bands.send to send messages by identifier string.

Working with a Band

A Band is a group of channels with a Dispatcher used to actually execute a Channel’s receivers. Messages sent to one Band will not reach another Band’s Channels or receivers in another Band.

The api functions, bands.channel and bands.send, delegate their calls to the active band. The active band defaults to the default Band accessible via the DEFAULT_BAND constant. You can set the active band with bands.use_band, and get the active band with bands.get_band. It may be wise to have a Band per application or library.

>>> import bands
>>> my_band = bands.Band()
>>> chan = my_band.channel('one')

You can also provide your own Dispatcher to my_band. Here is an example of a LoggingDispatcher.

>>> import bands
>>> import logging

>>> class LoggingDispatcher(bands.Dispatcher):
...     def __init__(self, name):
...         self.log = logging.getLogger(name)
...     def before_dispatch(self, ctx):
...         self.log.debug('Sending %s' % ctx.identifier)

>>> my_band = bands.Band(LoggingDispatcher('my_band'))

The above LoggingDispatcher will log a debug message before every message is dispatched to a channels receivers.

Installation

> pip install bands

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bands-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bands-0.1.0-py2.py3-none-any.whl (6.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file bands-0.1.0.tar.gz.

File metadata

  • Download URL: bands-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for bands-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0fde90d6d3daf49d322128611c703fd8e4fee3cd98cadd29e4413f0bda0a021f
MD5 3067cbb6f80850c48f9af4622f0ec780
BLAKE2b-256 0c7ce84bc5d9f250ca13fbfd561e491991801556ec481efa9a67f0ff0060e03d

See more details on using hashes here.

File details

Details for the file bands-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for bands-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7f84542043b8b01963e3be3464928e59d4ad10434911ec27cf55ce6ecabefb58
MD5 572fa126006113e5d206f13ee8f43cd2
BLAKE2b-256 e9cae8d3e4e548b19d0e5a46c47e53f6d0c534acd17ec2932691ce240be65f2f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page