Units
albert.collections.units.UnitCollection
Bases: BaseCollection
Manage Units of measure in the Albert platform.
A Unit is a unit of measure (e.g. g, mL, °C). Units are referenced
throughout the platform: they qualify inventory quantities, parameter values,
and property results. Each unit has a name, an optional display symbol, an
optional list of synonyms (alternate spellings), and a category
(UnitCategory, e.g. Mass or Volume).
Units are referenced by their Unit ID (format UNI..., e.g. "UNI9999999").
This collection is accessed as client.units.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AlbertSession
|
The authenticated Albert session used for API calls. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
base_path |
str
|
The base API route for unit requests. |
Methods:
| Name | Description |
|---|---|
create |
Create a new unit. |
get_or_create |
Return the existing unit matching the name, or create it. |
get_by_id |
Get a single unit by its ID. |
get_by_ids |
Get many units by their IDs. |
get_by_name |
Get a unit by name, or None if not found. |
get_all |
Iterate over units with optional filters. |
update |
Update an existing unit. |
delete |
Delete a unit by its ID. |
exists |
Check whether a unit with the given name exists. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AlbertSession
|
The authenticated Albert session used for API calls. |
required |
Source code in src/albert/collections/units.py
create
Create a new unit.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
Unit
|
The unit to create. |
required |
Returns:
| Type | Description |
|---|---|
Unit
|
The newly created unit, including its assigned Unit ID. |
Source code in src/albert/collections/units.py
get_or_create
Return the existing unit matching the given name, or create it.
Looks for an existing unit with the same name (exact match). If one is found it is returned unchanged; otherwise a new unit is created.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
Unit
|
The unit to find or create. |
required |
Returns:
| Type | Description |
|---|---|
Unit
|
The existing or newly created unit. |
Source code in src/albert/collections/units.py
get_by_id
Get a single unit by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UnitId
|
The Unit ID to retrieve (format |
required |
Returns:
| Type | Description |
|---|---|
Unit
|
The fully populated unit. |
Source code in src/albert/collections/units.py
get_by_ids
Get many units by their IDs.
IDs are fetched in batches, so arbitrarily long lists are supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ids
|
list[UnitId]
|
The Unit IDs to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
list[Unit]
|
The matching units. Units not found are omitted. |
Source code in src/albert/collections/units.py
update
Update an existing unit.
Fetch a unit (e.g. via get_by_id), modify the updatable fields on
the returned object, then pass it here. The unit is matched by its id.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
Unit
|
The unit carrying the desired changes. Must have its |
required |
Returns:
| Type | Description |
|---|---|
Unit
|
The updated unit, re-fetched from Albert. |
Notes
The following fields can be updated: category, symbol, synonyms.
Source code in src/albert/collections/units.py
delete
delete(*, id: UnitId) -> None
Delete a unit by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UnitId
|
The Unit ID to delete. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in src/albert/collections/units.py
get_all
get_all(
*,
name: str | list[str] | None = None,
category: UnitCategory | None = None,
order_by: OrderBy = DESCENDING,
exact_match: bool = False,
verified: bool | None = None,
start_key: str | None = None,
max_items: int | None = None,
) -> Iterator[Unit]
Iterate over units, with optional filters.
Results are fetched page by page as you iterate, so this scales to large result sets without loading everything at once.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str or list[str]
|
One or more unit names to filter by. |
None
|
category
|
UnitCategory
|
Restrict results to a single unit category (e.g. |
None
|
order_by
|
OrderBy
|
Sort direction for results. Defaults to |
DESCENDING
|
exact_match
|
bool
|
Whether |
False
|
verified
|
bool
|
Filter by whether the unit is verified. Defaults to None (no filter). |
None
|
start_key
|
str
|
Pagination key to resume iteration from a previous position. |
None
|
max_items
|
int
|
Maximum number of units to return in total. If None, iterates over all matching units. |
None
|
Returns:
| Type | Description |
|---|---|
Iterator[Unit]
|
An iterator over the matching units. |
Source code in src/albert/collections/units.py
get_by_name
Get a unit by its name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The unit name to retrieve. |
required |
exact_match
|
bool
|
Whether to match the name exactly, by default False. |
False
|
Returns:
| Type | Description |
|---|---|
Unit or None
|
The matching unit, or None if no unit with that name exists. |
Source code in src/albert/collections/units.py
exists
Check whether a unit with the given name exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The unit name to check. |
required |
exact_match
|
bool
|
Whether to match the name exactly, by default True. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if a matching unit exists, False otherwise. |