Skip to content

Helpers

helpers

Module of helper methods for the Kaleidoscope Python Client.

This module provides utility functions for data transformation and other helper tasks. Currently, it includes functionality to map field IDs to human-readable field names.

Functions:

Name Description
export_data

Transforms data records by mapping field IDs to field names.

Example
from kalpy.helpers import export_data

# Transform raw data with field IDs to data with field names
processed_data = export_data(client, raw_data)

export_data

export_data(client: KaleidoscopeClient, data: list[dict[str, Any]]) -> list[dict[str, Any]]

Transform a list of data records by mapping field IDs to their corresponding field names.

This function takes raw data records where keys are field IDs and converts them into records where keys are human-readable field names. It uses the KaleidoscopeClient to retrieve field metadata and perform the mapping. If a field ID is not found in the metadata, the original ID is preserved as the key.

Parameters:

Name Type Description Default
client KaleidoscopeClient

The client instance containing field metadata used to map field IDs to field names.

required
data list[dict[str, Any]]

A list of records, each represented as a dictionary mapping field IDs to their values.

required

Returns:

Type Description
list[dict[str, Any]]

A list of transformed records, each represented as a dictionary mapping field names (as keys) to their values.

Source code in kalpy/helpers.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def export_data(
    client: KaleidoscopeClient, data: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
    """Transform a list of data records by mapping field IDs to their corresponding field names.

    This function takes raw data records where keys are field IDs and converts them into
    records where keys are human-readable field names. It uses the KaleidoscopeClient to
    retrieve field metadata and perform the mapping. If a field ID is not found in the
    metadata, the original ID is preserved as the key.

    Args:
        client (KaleidoscopeClient): The client instance containing field metadata used
            to map field IDs to field names.
        data (List[Dict[str, Any]]): A list of records, each represented as a dictionary
            mapping field IDs to their values.

    Returns:
        (List[Dict[str, Any]]): A list of transformed records, each represented as a dictionary
            mapping field names (as keys) to their values.
    """
    key_fields = client.entity_fields.get_key_fields()
    data_fields = client.entity_fields.get_data_fields()

    id_to_field = {item.id: item for item in key_fields + data_fields}
    return [
        {
            (id_to_field[id].field_name if id in id_to_field else id): value
            for id, value in record.items()
        }
        for record in data
    ]