Skip to content
Carefully crafted library to operate with continuous streams of data in a reactive style with publish/subscribe, asyncio support and broker functionality.
Python
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode
broqer fixes types for operator Mar 3, 2020
docs
examples
tests
.gitignore chnages on project settings Aug 2, 2018
.travis.yml
CONTRIBUTING.rst
LICENSE
README.rst
notes
requirements_dev.txt
setup.cfg
setup.py fixed setup.py Feb 17, 2020

README.rst

Python Broqer 2.0RC0 (work in progress)

https://readthedocs.org/projects/python-broqer/badge/?version=latest

Initial focus on embedded systems Broqer can be used wherever continuous streams of data have to be processed - and they are everywhere. Watch out!

Synopsis

  • Pure python implementation without dependencies
  • Under MIT license (2018 Günther Jena)
  • Source is hosted on GitHub.com
  • Documentation is hosted on ReadTheDocs.com
  • Tested on Python 3.5, 3.6, 3.7 and 3.8
  • Unit tested with pytest, coding style checked with Flake8, static type checked with mypy, static code checked with Pylint, documented with Sphinx
  • Operators known from ReactiveX and other streaming frameworks (like Map, CombineLatest, ...)
    • Centralised object to keep track of publishers and subscribers
    • Starting point to build applications with a microservice architecture

Showcase

In other frameworks a Publisher is sometimes called Oberservable. A Subscriber is able to observe changes the publisher is emitting. With these basics you're able to use the observer pattern - let's see!

Observer pattern

Subscribing to a publisher is done via the .subscribe() method. A simple subscriber is op.Sink which is calling a function with optional positional and keyword arguments.

>>> from broqer import Value, op
>>> a = Value(5)  # create a value (publisher and subscriber with state)
>>> disposable = a.subscribe(op.Sink(print, 'Change:'))  # subscribe a callback
Change: 5

>>> a.emit(3)  # change the value
Change: 3

>>> disposable.dispose()  # unsubscribe

Combine publishers with arithmetic operators

You're able to create publishers on the fly by combining two publishers with the common operators (like +, >, <<, ...).

>>> from broqer import Value, op
>>> a = Value(1)
>>> b = Value(3)

>>> c = a * 3 > b  # create a new publisher via operator overloading
>>> c.subscribe(op.Sink(print, 'c:'))
c: False

>>> a.emit(2)
c: True

>>> b.emit(10)
c: False

Also fancy stuff like getting item by index or key is possible:

>>> i = Value('a')
>>> d = Value({'a':100, 'b':200, 'c':300})

>>> d[i].subscribe(op.Sink(print, 'r:'))
r: 100

>>> i.emit('c')
r: 300
>>> d.emit({'c':123})
r: 123

Some python built in functions can't return Publishers (e.g. len() needs to return an integer). For this cases special functions are defined in broqer: Str, Int, Float, Len and In (for x in y). Also other functions for convenience are available: All, Any, BitwiseAnd and BitwiseOr.

Attribute access on a publisher is building a publisher where the actual attribute access is done on emitting values. A publisher has to know, which type it should mimic - this is done via .inherit_type(type).

>>> i = Value('Attribute access made REACTIVE')
>>> i.inherit_type(str)
>>> i.lower().split(sep=' ') | op.Sink(print)
['attribute', 'access', 'made', 'reactive']

>>> i.emit('Reactive and pythonic')
['reactive', 'and', 'pythonic']

Function decorators

Make your own operators on the fly with function decorators. Decorators are available for Accumulate, CombineLatest, Filter, Map, MapAsync, MapThreaded, Reduce and Sink.

>>> @build_map
... def count_vowels(s):
...     return sum([s.count(v) for v in 'aeiou'])

>>> msg = Value('Hello World!)
>>> (msg | count_vowels).subscribe(op.Sink(print, 'Number of vowels:'))
Number of vowels: 3
>>> msg.emit('Wahuuu')
Number of vowels: 4

You can even make configurable Map s and Filter s:

>>> import re

>>> @build_filter_factory
... def filter_pattern(pattern, s):
...     return re.search(pattern, s) is not None

>>> msg = Value('Cars passed: 135!')
>>> (msg | filter_pattern('[0-9]*')).subscribe(op.Sink(print))
Cars passed: 135!
>>> msg.emit('No cars have passed')
>>> msg.emit('Only 1 car has passed')
Only 1 car has passed

Install

pip install broqer

Credits

Broqer was inspired by:

  • RxPY: Reactive Extension for Python (by Børge Lanes and Dag Brattli)
  • aioreactive: Async/Await reactive tools for Python (by Dag Brattli)
  • streamz: build pipelines to manage continuous streams of data (by Matthew Rocklin)
  • MQTT: M2M connectivity protocol
  • Florian Feurstein: spending hours of discussion, coming up with great ideas and help me understand the concepts!

API

Publishers

A Publisher is the source for messages.

Publisher () Basic publisher

Operators

CombineLatest (*publishers) Combine the latest emit of multiple publishers and emit the combination
Filter (predicate, ...) Filters values based on a predicate function
Map (map_func, *args, **kwargs) Apply map_func(*args, value, **kwargs) to each emitted value

Subscribers

A Subscriber is the sink for messages.

Sink (func, *args, **kwargs) Apply func(*args, value, **kwargs) to each emitted value
OnEmitFuture (timeout=None) Build a future able to await for
Trace (d) Debug output for publishers

Values

Value (*init) Publisher and Subscriber
You can’t perform that action at this time.