Skip to main content

Build data streaming pipelines using faststan

Project description

FastSTAN

pipeline status coverage report Code style: black

Easily deploy NATS Streaming subscribers using Python.

Features

  • Define subscribers using sync and async python functions
  • Automatic data parsing and validation using type annotations and pydantic
  • Support custom validation using any function
  • Allow several subscribers on same channel
  • Support all subscription configuration available in stan.py
  • Healthcheck available using HTTP GET request to monitor the applications
  • (TODO) Metrics available using HTTP GET requests to monitor subsriptions status
  • All of FastAPI features

Quick start

  • Install the package from pypi:
pip install faststan
  • Create your first subscriber. Create a file named app.py and write the following lines:
from faststan import FastSTAN

app = FastSTAN()

@app.stan.subscribe("demo")
def on_event(message: str):
    print(f"INFO :: Received new message: {message}")
  • Start your subscriber:
uvicorn app:app
  • Or if you are in a jupyter notebook environment, start the subscriptions:
await app.stan.run()

Advanced features

Using error callbacks

from faststan import FastSTAN


app = FastSTAN()


def handle_error(error):
    print("ERROR: {error}")


@app.stan.subscribe("demo", error_cb=handle_error)
def on_event(message: str):
    print(f"INFO :: Received new message: {message}")

Using pydantic models

You can use pydantic models in order to automatically parse incoming messages:

from pydantic import BaseModel
from faststan import FastSTAN


class Event(BaseModel):
    timestamp: int
    temperature: float
    humidity: float


app = FastSTAN()


@app.stan.subscribe("event")
def on_event(event):
    msg = f"INFO :: {event.timestamp} :: Temperature: {event.temperature} | Humidity: {event.humidity}"
    print(msg)

Using pydantic models with numpy or pandas

import numpy as np
from pydantic import BaseModel
from faststan import FastSTAN


class NumpyEvent(BaseModel):
    values: np.ndarray
    timestamp: int

    @validator("temperature", pre=True)
    def validate_array(cls, value):
        return np.array(value, dtype=np.float32)

    @validator("humidity", pre=True)
    def validate_array(cls, value):
        return np.array(value, dtype=np.float32)

    class Config:
        arbitrary_types_allowed = True

@app.stan.subscribe("event")
def on_event(event: NumpyEvent):
    print(
        f"INFO :: {event.timestamp} :: Temperature values: {event.values[0]} | Humidity values: {event.values[1]}"
    )

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

faststan-0.1.4.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

faststan-0.1.4-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file faststan-0.1.4.tar.gz.

File metadata

  • Download URL: faststan-0.1.4.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.5 Windows/10

File hashes

Hashes for faststan-0.1.4.tar.gz
Algorithm Hash digest
SHA256 8e2bab2fc5881b9f93bc1a5597f1e24a37cdc41fe6ea18cb0be5eeb1b28b5ccb
MD5 c1006df4297a5fc83b6bee9e5ff24281
BLAKE2b-256 493108b56ef0c38a6e9925b220415fe2b30b616bd91ee63039d44e97f7456ea8

See more details on using hashes here.

File details

Details for the file faststan-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: faststan-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.5 Windows/10

File hashes

Hashes for faststan-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 458a38e3dd1d77b3e6e0db581e075dc6f2bed7a05ab5cf734748829d9a059122
MD5 4319f614c7411dd9a26feae6a4422705
BLAKE2b-256 10370e40a5e829b357ac1864c99cb0904bc98e3281b7f20ddf34498e363bcfe0

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