Skip to main content
Bands 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!!']

Alternatively you can send your message via bands.send…

>>> bands.send('alert', 'ALERT!!')
['ALERT!!']

The send method forwards *args and **kwargs to all of the Channel’s receivers.

Working with bound Channel’s

A Channel is bound when it’s parent attribute is set. If you use bands.channel as a class attribute, each instance of your class will have it’s own bound Channel. This is very similar to the way Python methods 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.

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.

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 channel’s receivers.

Installation

> pip install bands

Release files for bands 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bands 0.1.3
File Size Uploaded
bands-0.1.3.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bands 0.1.3
File Interpreter ABI Platform
bands-0.1.3-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 13.0 kB

Release files / bands-0.1.3.tar.gz

Download URL bands-0.1.3.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
920ad0a486ee30db11fda18a41a91307873db39f0d2c80fdc309641b458c4340
BLAKE2b-256 checksum
How to use checksums
662306a114c86d21cb3794f134c6fa62561d66599642c883fc7cd06d8b121020
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / bands-0.1.3-py2.py3-none-any.whl

Download URL bands-0.1.3-py2.py3-none-any.whl
Size 6.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8465f821da9857449dccd26ea22be355501090d8609c05c51400902eccb09366
BLAKE2b-256 checksum
How to use checksums
72dc00dce556d1cda148b8e1ba05b57ec59b80fd33a28e20f24a1dab44f78c7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page