Skip to main content

Carefully crafted library to operate with continuous streams of data in a reactive style with publish/subscribe and broker functionality.

Project description

https://img.shields.io/pypi/v/broqer.svg https://img.shields.io/travis/semiversus/python-broqer.svg https://codecov.io/gh/semiversus/python-broqer/branch/master/graph/badge.svg https://img.shields.io/github/license/semiversus/python-broqer.svg

Carefully crafted library to operate with continuous streams of data in a reactive style with publish/subscribe and broker functionality.

Synopsis

  • Pure python implementation without dependencies (except Python 3.5+)

  • Operators known from ReactiveX and other streaming frameworks (like distinct, combine_latest, …)

  • Supporting asyncio for time depended operations and using coroutines (e.g. map_async, debounce, …)

  • Publishers are awaitable (e.g. await adc_raw)

  • Compact library (<1000 lines of code), but well documented (>1000 lines of comments)

  • Fully unit tested (coverage towards 100%), coding style checked with flake8, static typing checked with mypy

  • Under MIT license (2018 Günther Jena)

Install

pip install broqer

Example

In the first example adc_raw is a Publisher emitting values from an analog digital converter. The value will be converter (scaled by factor 0.3), sampled and a moving average is applied. Filtering for values greater 1 will be printed (with the prefix ‘Voltage too high:’)

from broqer import op
import statistics

( adc_raw
  | op.map(lambda v:v*0.3) # apply a function with one argument returning to value multiplied by 0.3
  | op.sample(0.1) # periodically emit the actual value every 0.1 seconds
  | op.sliding_window(4) # append the value to a buffer with 4 elements (and drop the oldest value)
  | op.map(statistics.mean) # use ``statistics.mean`` to calulate the average over the emitted sequence
  | op.filter(lambda v:v>1) # emit only values greater 1
  | op.sink (print, 'Voltage too high:') # call ``print`` with 'Voltage too high:' and the value
)
https://github.com/semiversus/python-broqer/blob/master/docs/example1.svg

Output to stdout:

Voltage too high: 1.25
Voltage too high: 1.5
Voltage too high: 1.75
Voltage too high: 2
Voltage too high: 2
Voltage too high: 2
Voltage too high: 2

API

Publishers

A Publisher is the source for messages.

Subject ()

Source with .emit(*args) method to publish a new message

Value (*init)

Source with a state (initialized via init)

FromIterable (iterable)

Use an iterable and emit each value

Using asyncio event loop:

FromPolling (interval, func, …)

Call func(*args, **kwargs) periodically and emit the returned values

Operators

accumulate (func, init)

Apply func(value, state) which is returning new state and value to emit

cache (*init)

Caching the emitted values to access it via .cache property

catch_exception (*exceptions)

Catching exceptions of following operators in the pipelines

combine_latest (*publishers)

Combine the latest emit of multiple publishers and emit the combination

distinct (*init)

Only emit values which changed regarding to the cached state

filter (predicate, …)

Filters values based on a predicate function

map (map_func, *args, **kwargs)

Apply map_func(*args, value, **kwargs) to each emitted value

merge (*publishers)

Merge emits of multiple publishers into one stream

pack (*args)

Emit a multi-argument emit as tuple of arguments

partition (size)

Group size emits into one emit as tuple

pluck (*picks)

Apply sequence of picks via getitem to emitted values

reduce (func, init)

Apply func to the current emitted value and the last result of func

sliding_window (size, …)

Group size emitted values overlapping

switch (mapping)

Emit selected source mapped by mapping

unpack (args)

Unpacking a sequence of values and use it to emit as arguments

Using asyncio event loop:

debounce (duetime)

Emit a value only after a given idle time (emits meanwhile are skipped)

delay (delay)

Emit every value delayed by the given time

map_async (map_coro, mode, …)

Apply map_coro to each emitted value allowing async processing

sample (interval)

Emit the last received value periodically

throttle (duration)

Rate limit emits by the given time

Subscribers

A Subscriber is the sink for messages.

sink (func, *args, **kwargs)

Apply func(*args, value, **kwargs) to each emitted value

to_future (timeout=None)

Build a future able to await for

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 continous 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!

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

broqer-0.1.5.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

broqer-0.1.5-py2.py3-none-any.whl (27.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file broqer-0.1.5.tar.gz.

File metadata

  • Download URL: broqer-0.1.5.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for broqer-0.1.5.tar.gz
Algorithm Hash digest
SHA256 5ada91a4562a33faf18e6173211b4a2247ed9ef4f4fa0ac1efa7ec88131194e0
MD5 bc505b41f563e8ee4d82afc3b7ad6a6a
BLAKE2b-256 cedb9584878b89427ca6fa36936222225fc6cb68645477c98c64cc390ba3ba5a

See more details on using hashes here.

Provenance

File details

Details for the file broqer-0.1.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for broqer-0.1.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b1e51beecc140b266ba53d0a0e816a3b9c685fd2acb51b8480bd3512b0179d09
MD5 388f7b1810c3e2f2d652cc308b6a4a6d
BLAKE2b-256 dd97c8049b177dac80253814e4d813ea0b6f9c63225734b67bedeaf1c42ff82f

See more details on using hashes here.

Provenance

Supported by

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