Utilities¶
Validators, the generic sliding Window, and a timing
context manager. These are public because user-defined transforms need the
same validation behaviour as the built-in ones.
utils
¶
Small, dependency-free helpers shared across the package.
Three things live here:
- Validators (
as_floats,require_non_empty,require_positive) that coerce and check inputs once, at the public API boundary, and raiseValidationErrorwith a uniform message. Window, a generic fixed-capacity sliding window used by the streaming filters inmypackage.smoothing.timer, a context manager for wall-clock measurements in examples and benchmarks.
Window
¶
A fixed-capacity sliding window over a stream of values.
Pushing past capacity evicts the oldest element, so the window always
holds the most recent capacity items. The element type is generic: the
window itself never inspects the values it stores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capacity
|
int
|
Maximum number of retained elements. Must be positive. |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If |
Examples:
>>> window: Window[int] = Window(capacity=3)
>>> for value in (1, 2, 3, 4):
... _ = window.push(value)
>>> list(window)
[2, 3, 4]
>>> len(window), window.is_full
(3, True)
Pushing returns self, so calls chain:
Source code in src/mypackage/utils.py
Elapsed
dataclass
¶
as_floats
¶
as_floats(series: Series | Sequence[str], *, name: str = 'series') -> list[float]
Coerce a sequence of numbers to a fresh list[float].
Strings that parse as numbers are accepted too, which is what lets
mypackage.cli hand raw tokens straight to this function instead of
duplicating the validation logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series | Sequence[str]
|
The sequence to coerce. Never mutated. |
required |
name
|
str
|
Argument name used in the error message. |
'series'
|
Returns:
| Type | Description |
|---|---|
list[float]
|
A new list containing every element of |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If any element cannot be converted to |
Examples:
>>> as_floats([1, 2, 3])
[1.0, 2.0, 3.0]
>>> as_floats(range(3))
[0.0, 1.0, 2.0]
>>> as_floats(["1.5", "2"])
[1.5, 2.0]
>>> try:
... as_floats([1.0, "oops"])
... except ValidationError as err:
... print(err)
'series' must contain only numbers, got 'oops' at index 1
Source code in src/mypackage/utils.py
require_non_empty
¶
require_non_empty(series: Series, *, name: str = 'series') -> list[float]
Coerce series to floats and require at least one element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series
|
The sequence to validate. |
required |
name
|
str
|
Argument name used in the error message. |
'series'
|
Returns:
| Type | Description |
|---|---|
list[float]
|
A new |
Raises:
| Type | Description |
|---|---|
EmptySeriesError
|
If |
ValidationError
|
If any element is not a number. |
Examples:
>>> require_non_empty((1, 2))
[1.0, 2.0]
>>> try:
... require_non_empty([])
... except EmptySeriesError as err:
... print(err)
'series' must contain at least one value, got 0
Source code in src/mypackage/utils.py
require_positive
¶
Require value to be a strictly positive integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
The integer to validate. |
required |
name
|
str
|
Argument name used in the error message. |
'window'
|
Returns:
| Type | Description |
|---|---|
int
|
The validated integer. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If |
Examples:
>>> require_positive(3, name="window")
3
>>> try:
... require_positive(0, name="window")
... except ValidationError as err:
... print(err)
'window' must be a positive integer, got 0
Source code in src/mypackage/utils.py
timer
¶
timer() -> Iterator[Elapsed]
Measure the wall-clock time spent inside a with block.
The yielded Elapsed is populated when the block
exits — including when it exits via an exception.
Yields:
| Type | Description |
|---|---|
Elapsed
|
An |
Examples:
>>> with timer() as elapsed:
... total = sum(range(1_000))
>>> elapsed.seconds >= 0.0
True
>>> elapsed.milliseconds == elapsed.seconds * 1e3
True