aimbat.utils
Miscellaneous helpers for AIMBAT.
Covers four areas:
- JSON — render JSON data as Rich tables (
json_to_table). - Sample data — download and delete the bundled sample dataset
(
download_sampledata,delete_sampledata). - Styling — shared Rich/table style helpers (
make_table). - UUIDs — look up model records by short UUID prefix (
get_by_uuid).
Classes:
| Name | Description |
|---|---|
TableStyling |
This class is to set the colour of the table columns and elements. |
Functions:
| Name | Description |
|---|---|
delete_sampledata |
Delete sample data. |
download_sampledata |
Download sample data. |
json_to_table |
Print a JSON dict or list of dicts as a rich table. |
string_to_uuid |
Determine a UUID from a string containing the first few characters. |
uuid_shortener |
Calculates the shortest unique prefix for a UUID, returning with dashes. |
TableStyling
dataclass
This class is to set the colour of the table columns and elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
|
'bright_blue'
|
mine
|
str
|
|
'cyan'
|
linked
|
str
|
|
'magenta'
|
parameters
|
str
|
|
'green'
|
Source code in src/aimbat/utils/_style.py
delete_sampledata
download_sampledata
download_sampledata(force: bool = False) -> None
Download sample data.
Source code in src/aimbat/utils/_sampledata.py
json_to_table
json_to_table(
data: dict[str, Any] | list[dict[str, Any]],
title: str | None = None,
formatters: (
dict[str, Callable[[Any], str]] | None
) = None,
skip_keys: list[str] | None = None,
column_order: list[str] | None = None,
column_kwargs: dict[str, dict[str, Any]] | None = None,
common_column_kwargs: dict[str, Any] | None = None,
) -> None
Print a JSON dict or list of dicts as a rich table.
For a single dict the table has Key and Value columns with one row
per key-value pair. For a list of dicts the keys become column headers and
each list item becomes a row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any] | list[dict[str, Any]]
|
A single JSON dict or a list of JSON dicts. |
required |
title
|
str | None
|
Optional title displayed above the table. |
None
|
formatters
|
dict[str, Callable[[Any], str]] | None
|
Optional mapping of key names to callables that receive the raw value and return a string for display. |
None
|
skip_keys
|
list[str] | None
|
Optional list of keys to exclude from the table. |
None
|
column_order
|
list[str] | None
|
Optional list of keys defining the display order. Keys not listed are appended after in their original order. For a single dict this controls row order; for a list of dicts it controls column order. |
None
|
column_kwargs
|
dict[str, dict[str, Any]] | None
|
Optional mapping of key names to keyword arguments
forwarded to |
None
|
common_column_kwargs
|
dict[str, Any] | None
|
Optional keyword arguments applied to every
column, merged with any per-column entries in |
None
|
Examples:
>>> json_to_table({"name": "Alice", "age": 30}, title="Person")
>>> json_to_table([{"id": 1}, {"id": 2}], formatters={"id": str})
>>> json_to_table({"name": "Alice", "secret": "x"}, skip_keys=["secret"])
>>> json_to_table(
... [{"id": 1, "name": "Alice"}],
... column_order=["name", "id"],
... column_kwargs={"id": {"justify": "right", "style": "cyan"}},
... )
Source code in src/aimbat/utils/_json.py
string_to_uuid
string_to_uuid(
session: Session,
id: str,
aimbat_class: type[AimbatTypes],
custom_error: str | None = None,
) -> UUID
Determine a UUID from a string containing the first few characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
Database session. |
required |
id
|
str
|
Input string to find UUID for. |
required |
aimbat_class
|
type[AimbatTypes]
|
Aimbat class to use to find UUID. |
required |
custom_error
|
str | None
|
Overrides the default error message. |
None
|
Returns:
| Type | Description |
|---|---|
UUID
|
The full UUID. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the UUID could not be determined. |
Source code in src/aimbat/utils/_uuid.py
uuid_shortener
uuid_shortener(
session: Session,
aimbat_obj: T | type[T],
min_length: int = 2,
str_uuid: str | None = None,
) -> str
Calculates the shortest unique prefix for a UUID, returning with dashes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
An active SQLModel/SQLAlchemy session. |
required |
aimbat_obj
|
T | type[T]
|
Either an instance of a SQLModel or the SQLModel class itself. |
required |
min_length
|
int
|
The starting character length for the shortened ID. |
2
|
str_uuid
|
str | None
|
The full UUID string. Required only if |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The shortest unique prefix string, including hyphens where applicable. |