haive.core.common.mixins.identifier¶
Identifier mixin for unique identification of objects.
This module provides a mixin class that adds UUID-based identification and human-readable naming to Pydantic models. The mixin handles validation, generation, and utility methods for working with identifiers.
Uses Pydantic v2 patterns with field_validator and computed fields.
- Usage:
from haive.core.common.mixins.identifier import IdentifierMixin
- class MyComponent(IdentifierMixin, BaseModel):
# Other fields content: str
# Create with auto-generated ID component = MyComponent(content=”Hello”) print(component.id) # UUID string print(component.short_id) # First 8 chars of UUID
# Create with custom name named_component = MyComponent(content=”Hello”, name=”GreetingComponent”) print(named_component.display_name) # “GreetingComponent”
Classes¶
Mixin that adds unique identification to any Pydantic model. |
Module Contents¶
- class haive.core.common.mixins.identifier.IdentifierMixin(/, **data)[source]¶
Bases:
pydantic.BaseModel
Mixin that adds unique identification to any Pydantic model.
This mixin provides both UUID-based identification and human-readable naming capabilities. It automatically generates UUIDs, validates provided IDs, and offers convenience methods for working with the identifiers.
- Parameters:
data (Any)
- id¶
A UUID string that uniquely identifies the object.
- name¶
An optional human-readable name for the object.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- initialize_uuid_obj()[source]¶
Initialize UUID object after model validation.
- Returns:
Self, with the _uuid_obj private attribute initialized.
- Return type:
Self
- matches_id(id_or_name)[source]¶
Check if this object matches the given ID or name.
This method checks if the provided string matches this object’s full ID, short ID, or name (case-insensitive).
- regenerate_id()[source]¶
Generate a new ID and return it.
This method creates a new UUID, updates the ID field, and returns the new ID string.
- Returns:
The newly generated UUID string.
- Return type:
- set_name(name)[source]¶
Set the name with validation.
- Parameters:
name (str) – The new name to set.
- Return type:
None
- property display_name: str¶
Display name (uses name if available, otherwise short_id).
- Returns:
The human-readable name if set, otherwise “Object-{short_id}”.
- Return type:
- property has_custom_name: bool¶
Whether this object has a custom name (not auto-generated).
- Returns:
True if a non-empty name is set, False otherwise.
- Return type: