Skip to content

Unique IDs

Models for handling unique identifiers.


UniqueID

UniqueID(*args, **kwargs)

Bases: str, PydanticStrMixin

An augmented str class intended to represent a unique ID type

Validate that the string is a valid UUID4.

Raises:

Type Description
ValidationError

If the string is not a valid UUID4.

Source code in src/aibs_informatics_core/models/unique_ids.py
20
21
22
23
24
25
26
27
28
29
30
def __init__(self, *args, **kwargs):
    """Validate that the string is a valid UUID4.

    Raises:
        ValidationError: If the string is not a valid UUID4.
    """
    try:
        uuid_obj = uuid.UUID(self, version=4)
    except ValueError:
        raise ValidationError(f"'{self}' is not a valid {self.__class__.__name__} (uuid4)!")
    self._uuid_obj = uuid_obj

as_uuid

as_uuid()

Return the underlying UUID object.

Source code in src/aibs_informatics_core/models/unique_ids.py
44
45
46
def as_uuid(self) -> uuid.UUID:
    """Return the underlying UUID object."""
    return self._uuid_obj

create classmethod

create(seed=None)

Create a new UniqueID, optionally seeded for deterministic generation.

Parameters:

Name Type Description Default
seed int | str | None

Optional seed value. If provided, generates a deterministic UUID.

None

Returns:

Type Description
UNIQUE_ID_TYPE

A new UniqueID instance.

Source code in src/aibs_informatics_core/models/unique_ids.py
32
33
34
35
36
37
38
39
40
41
42
@classmethod
def create(cls: type[UNIQUE_ID_TYPE], seed: int | str | None = None) -> UNIQUE_ID_TYPE:
    """Create a new UniqueID, optionally seeded for deterministic generation.

    Args:
        seed: Optional seed value. If provided, generates a deterministic UUID.

    Returns:
        A new UniqueID instance.
    """
    return cls(uuid_str(str(seed)) if seed is not None else uuid.uuid4())

from_env classmethod

from_env()

Load a UniqueID from environment variables.

Checks the environment variables listed in ENV_VARS.

Returns:

Type Description
UNIQUE_ID_TYPE

A UniqueID loaded from the environment.

Raises:

Type Description
ValueError

If no matching environment variable is found.

Source code in src/aibs_informatics_core/models/unique_ids.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@classmethod
def from_env(cls: type[UNIQUE_ID_TYPE]) -> UNIQUE_ID_TYPE:
    """Load a UniqueID from environment variables.

    Checks the environment variables listed in ``ENV_VARS``.

    Returns:
        A UniqueID loaded from the environment.

    Raises:
        ValueError: If no matching environment variable is found.
    """
    env_var = get_env_var(*cls.ENV_VARS)
    if env_var is None:
        raise ValueError(
            f"Could not find environment variable for {cls} given ENV VARS: {cls.ENV_VARS}"
        )
    return cls(env_var)