Build data streaming pipelines using faststan
Project description
FastSTAN
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.pyand 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.5.tar.gz
(6.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file faststan-0.1.5.tar.gz.
File metadata
- Download URL: faststan-0.1.5.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.10 CPython/3.8.5 Linux/4.19.78-coreos
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e03e6d1180674a63a97b0e0662ebd563a6b33629248815f68dad4c49915177f
|
|
| MD5 |
955ab0b0e043d9152ed47322faca296a
|
|
| BLAKE2b-256 |
df47828bf4a090209c8da012136fe4ff1fc746d5596861b7a62b7efc33a13aaf
|
File details
Details for the file faststan-0.1.5-py3-none-any.whl.
File metadata
- Download URL: faststan-0.1.5-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 Linux/4.19.78-coreos
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69898484bb0d406200db14fa80432f711e5dad9cdc7e6652ab3f80041c644a77
|
|
| MD5 |
426a421169530e47a3752c5a8307eeb8
|
|
| BLAKE2b-256 |
19d65a6e4811be810318490e66fe02ee96580a0e2ef591f8279e344e80ef7637
|