Skip to content

Point Templates

PointTemplates(baze)

Class used for handling points.

Source code in echo_baze/baze_root.py
def __init__(self, baze: e_bz.Baze) -> None:
    """Base class that all subclasses should inherit from.

    Parameters
    ----------
    baze : Baze
        Top level object carrying all functionality and the connection handler.

    """
    # check inputs
    if not isinstance(baze, e_bz.Baze):
        raise ValueError(f"baze must be of type Baze, not {type(baze)}")

    self.baze: e_bz.Baze = baze

get(object_models, output_type='dict')

Gets all point templates for the given object models.

The most useful keys/columns returned are:

  • schemaTemplateId
  • acronym
  • description

Parameters:

  • object_models

    (list[str]) –

    Desired object models to get the point templates from.

  • output_type

    (Literal['dict', 'DataFrame'], default: 'dict' ) –

    Output type of the data. Can be one of ["dict", "DataFrame"] By default "dict"

Returns:

  • dict[str, dict[str, Any]]

    In case output_type == "dict" it will return a dict with the following format: {model_name: {template_name: {attribute: value, ...}, ...}, ...}

  • DataFrame

    In case output_type == "DataFrame" it will return a DataFrame with the following format: index = MultiIndex with levels ["object_model", "template"], columns = [attribute, ...]

Source code in echo_baze/point_templates.py
@validate_call
def get(self, object_models: list[str], output_type: Literal["dict", "DataFrame"] = "dict") -> dict[str, dict[str, Any]] | DataFrame:
    """Gets all point templates for the given object models.

    The most useful keys/columns returned are:

    - schemaTemplateId
    - acronym
    - description

    Parameters
    ----------
    object_models : list[str]
        Desired object models to get the point templates from.
    output_type : Literal["dict", "DataFrame"], optional
        Output type of the data. Can be one of ["dict", "DataFrame"]
        By default "dict"

    Returns
    -------
    dict[str, dict[str, Any]]
        In case output_type == "dict" it will return a dict with the following format: {model_name: {template_name: {attribute: value, ...}, ...}, ...}
    DataFrame
        In case output_type == "DataFrame" it will return a DataFrame with the following format: index = MultiIndex with levels ["object_model", "template"], columns = [attribute, ...]

    """
    # getting the object model ids
    object_model_ids = self.baze.objects.models.get_ids()
    object_model_ids = {name: model_id for name, model_id in object_model_ids.items() if name in object_models}
    if len(object_model_ids) != len(object_models):
        raise ValueError(
            f"Coudn't find all object models. The following were not found: {set(object_models) - set(object_model_ids)}",
        )

    # getting the point templates
    results = {}
    for object_model_name, object_model_id in object_model_ids.items():
        endpoint = f"configuration/domain/schematemplates?domainId={object_model_id}&includeParent=true"

        # getting the data
        result = self.baze.conn.get(endpoint)
        self._handle_http_errors(result)

        # converting to dict
        result: dict[str, list[dict[str, Any]]] = result.json()

        # converting to desired format
        result = {entry["name"]: entry for entry in result["schemaTemplates"]}

        # adding to the results
        results[object_model_name] = result

    # returning on the desired format
    match output_type:
        case "dict":
            return results
        case "DataFrame":
            df = DataFrame.from_dict(
                {(model, template): attributes for model, templates in results.items() for template, attributes in templates.items()},
                orient="index",
            )
            df.index.names = ["object_model", "template"]
            df = df.reset_index(drop=False).convert_dtypes(dtype_backend="pyarrow").set_index(["object_model", "template"])
            return df
        case _:
            raise ValueError(f"Invalid output_type: {output_type}")

get_ids(object_models)

Gets a dictionary with all point template ids.

Parameters:

  • object_models

    (list[str]) –

    List of object models to get the point templates from.

Returns:

  • dict[str, dict[str, int]]:

    Dictionary with all point templates in the format {object_model: {name: id, ...}, ...}

Source code in echo_baze/point_templates.py
@validate_call
def get_ids(self, object_models: list[str]) -> dict[str, dict[str, int]]:
    """Gets a dictionary with all point template ids.

    Parameters
    ----------
    object_models : list[str]
        List of object models to get the point templates from.

    Returns
    -------
    dict[str, dict[str, int]]:
        Dictionary with all point templates in the format {object_model: {name: id, ...}, ...}

    """
    # getting the object model ids
    object_model_ids = self.baze.objects.models.get_ids()
    object_model_ids = {name: model_id for name, model_id in object_model_ids.items() if name in object_models}
    if len(object_model_ids) != len(object_models):
        raise ValueError(
            f"Coudn't find all object models. The following were not found: {set(object_models) - set(object_model_ids)}",
        )

    # getting the point templates
    results = {}
    for object_model_name, object_model_id in object_model_ids.items():
        endpoint = f"configuration/domain/schematemplates?domainId={object_model_id}&includeParent=true"

        # getting the data
        result = self.baze.conn.get(endpoint)
        self._handle_http_errors(result)

        # converting to dict
        result: dict[str, list[dict[str, Any]]] = result.json()

        # converting to desired format
        result = {entry["name"]: entry["schemaTemplateId"] for entry in result["schemaTemplates"]}

        # adding to the results
        results[object_model_name] = result

    return results