Middleware Functions¶
This page documents the built-in middleware functions provided by Skarv.
Throttling¶
skarv.middlewares.throttle(at_most_every: float) -> Callable[[Any], Any | None]
¶
Create a throttling middleware that allows values through at most once every specified interval.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
at_most_every
|
float
|
Minimum interval in seconds between allowed values. |
required |
Returns:
Type | Description |
---|---|
Callable[[Any], Any | None]
|
Callable[[Any], Any | None]: Middleware function that returns the value or None if throttled. |
Source code in skarv/middlewares.py
Averaging¶
skarv.middlewares.average(no_of_samples: int) -> Callable[[Numeric], Numeric]
¶
Create a middleware that computes the moving average over a window of samples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
no_of_samples
|
int
|
Number of samples to average over. |
required |
Returns:
Type | Description |
---|---|
Callable[[Numeric], Numeric]
|
Callable[[Numeric], Numeric]: Middleware function that returns the moving average. |
Source code in skarv/middlewares.py
Weighted Averaging¶
skarv.middlewares.weighted_average(no_of_samples: int) -> Callable[[Numeric], Numeric]
¶
Create a middleware that computes a weighted moving average over a window of samples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
no_of_samples
|
int
|
Number of samples to use for the weighted average. |
required |
Returns:
Type | Description |
---|---|
Callable[[Numeric], Numeric]
|
Callable[[Numeric], Numeric]: Middleware function that returns the weighted moving average. |
Source code in skarv/middlewares.py
Differentiation¶
skarv.middlewares.differentiate() -> Callable[[Numeric], Numeric | None]
¶
Create a middleware that computes the numerical derivative of the input values.
Returns:
Type | Description |
---|---|
Callable[[Numeric], Numeric | None]
|
Callable[[Numeric], Numeric | None]: Middleware function that returns the derivative or None for the first value. |
Source code in skarv/middlewares.py
Batching¶
skarv.middlewares.batch(size: int) -> Callable[[Any], Sequence[Any] | None]
¶
Create a middleware that batches input values and outputs them as a sequence when the batch size is reached.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
The number of values to collect before emitting a batch. |
required |
Returns:
Type | Description |
---|---|
Callable[[Any], Sequence[Any] | None]
|
Callable[[Any], Sequence[Any] | None]: Middleware function that returns a batch or None if not enough values have been collected. |