Skip to content

Entity Types

entity_types

Entity type management module for the Kaleidoscope system.

This module provides classes and services for working with entity types in Kaleidoscope. Entity types define classifications of entities with associated key fields and slice names for data organization and retrieval.

Classes:

Name Description
EntityType

Represents a single entity type with its configuration and key fields.

EntityTypesService

Service class for managing and querying entity types.

Example
# get all entity types
all_types = client.entity_types.get_types()

# get a specific type by name
specific_type = client.entity_types.get_type_by_name("my_entity")

EntityType

Bases: _KaleidoscopeBaseModel

Represents an entity type in the Kaleidoscope system.

An EntityType defines a classification of entities with associated key fields and a slice name for data organization and retrieval.

Attributes:

Name Type Description
id str

UUID of the entity type.

key_field_ids list[str]

List of field IDs that serve as key fields for this entity type.

slice_name str

Name of the entity slice associated with this type.

Methods:

Name Description
get_record_ids

Retrieve a list of record IDs associated with the current entity slice.

get_record_ids

get_record_ids() -> list[str]

Retrieve a list of record IDs associated with the current entity slice.

Returns:

Type Description
list[str]

List[str]: A list of record IDs as strings.

Note

If an exception occurs during the API request, it logs the error and returns an empty list.

Source code in kalpy/entity_types.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def get_record_ids(self) -> List[str]:
    """Retrieve a list of record IDs associated with the current entity slice.

    Returns:
        List[str]: A list of record IDs as strings.

    Note:
        If an exception occurs during the API request, it logs the error
        and returns an empty list.
    """
    try:
        resp = self._client._get("/records/search?entity_slice_id=" + self.id)
        return resp
    except Exception as e:
        _logger.error(f"Error fetching record_ids of this entity type: {e}")
        return []

EntityTypesService

EntityTypesService(client: KaleidoscopeClient)

Service class for managing and retrieving entity types from the Kaleidoscope API.

This service provides methods to fetch, filter, and search entity types based on various criteria such as name and key field IDs. It handles the conversion of raw API responses into validated EntityType objects.

Example
# get all entity types
all_types = client.entity_types.get_types()

# get a specific type by name
specific_type = client.entity_types.get_type_by_name("my_entity")

Methods:

Name Description
get_types

Retrieve a list of entity types from the client.

get_type_by_name

Retrieve an EntityType object from the list of entity types by its name.

get_types_with_key_fields

Return a list of EntityType objects that contain all the specified key field IDs.

get_type_exact_keys

Retrieve an EntityType object whose key_field_ids exactly match the provided list.

Source code in kalpy/entity_types.py
85
86
def __init__(self, client: KaleidoscopeClient):
    self._client = client

get_types cached

get_types() -> list[EntityType]

Retrieve a list of entity types from the client.

This method caches its values.

Returns:

Type Description
list[EntityType]

List[EntityType]: A list of EntityType objects created from the response.

Note

If an exception occurs during the API request, it logs the error, clears the cache, and returns an empty list.

Source code in kalpy/entity_types.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
@lru_cache
def get_types(self) -> List[EntityType]:
    """Retrieve a list of entity types from the client.

    This method caches its values.

    Returns:
        List[EntityType]: A list of EntityType objects created from the response.

    Note:
        If an exception occurs during the API request, it logs the error,
        clears the cache, and returns an empty list.
    """
    try:
        resp = self._client._get("/entity_slices")
        return self._create_entity_type_list(resp)
    except Exception as e:
        _logger.error(f"Error fetching entity types: {e}")
        self.get_types.cache_clear()
        return []

get_type_by_name

get_type_by_name(name: str) -> EntityType | None

Retrieve an EntityType object from the list of entity types by its name.

Parameters:

Name Type Description Default
name str

The name of the entity type to search for.

required

Returns:

Type Description
EntityType | None

The EntityType object with the matching name if found, otherwise None.

Source code in kalpy/entity_types.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def get_type_by_name(self, name: str) -> EntityType | None:
    """Retrieve an EntityType object from the list of entity types by its name.

    Args:
        name (str): The name of the entity type to search for.

    Returns:
        (EntityType | None): The EntityType object with the matching name if found, otherwise None.
    """
    entity_types = self.get_types()
    return next(
        (et for et in entity_types if et.slice_name == name),
        None,
    )

get_types_with_key_fields

get_types_with_key_fields(key_field_ids: list[str]) -> list[EntityType]

Return a list of EntityType objects that contain all the specified key field IDs.

Parameters:

Name Type Description Default
key_field_ids list[str]

A list of key field IDs to filter entity types.

required

Returns:

Type Description
list[EntityType]

List[EntityType]: A list of EntityType instances where each entity type includes all the given key field IDs.

Source code in kalpy/entity_types.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def get_types_with_key_fields(self, key_field_ids: List[str]) -> List[EntityType]:
    """Return a list of EntityType objects that contain all the specified key field IDs.

    Args:
        key_field_ids (List[str]): A list of key field IDs to filter entity types.

    Returns:
        List[EntityType]: A list of EntityType instances where each entity type includes all the given key field IDs.
    """
    entity_types = self.get_types()
    return [
        et
        for et in entity_types
        if all([id in et.key_field_ids for id in key_field_ids])
    ]

get_type_exact_keys

get_type_exact_keys(key_field_ids: list[str]) -> EntityType | None

Retrieve an EntityType object whose key_field_ids exactly match the provided list.

Parameters:

Name Type Description Default
key_field_ids list[str]

A list of key field IDs to match against entity types.

required

Returns:

Type Description
EntityType | None

The matching EntityType object if found; otherwise, None.

Source code in kalpy/entity_types.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
def get_type_exact_keys(self, key_field_ids: List[str]) -> EntityType | None:
    """Retrieve an EntityType object whose key_field_ids exactly match the provided list.

    Args:
        key_field_ids (List[str]): A list of key field IDs to match against entity types.

    Returns:
        (EntityType | None): The matching EntityType object if found; otherwise, None.
    """
    entity_types = self._client.entity_types.get_types()
    return next(
        (et for et in entity_types if set(et.key_field_ids) == set(key_field_ids)),
        None,
    )