Object Model Attributes¶
ObjectModelAttributeDefinitions(baze)
¶
Class used for handling object model attribute definitions.
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 the attribute definitions for specific object models.
These can be viewed in the portal in Object Types > Model Attribute Definition.
Most of the attributes will have the following properties:
- attributeId
- categoryId
- displayId
- groupId
- isBuiltIn
- isMandatory
- isVisible
- name
- valueType
Parameters:
-
(object_models¶list[str]) –List of object models to get the attributes 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: {name: {attribute: value, ...}, ...}
-
DataFrame–In case output_type == "DataFrame" it will return a DataFrame with the following format: index = name, columns = [attribute, ...]
Source code in echo_baze/object_model_attribute_definitions.py
@validate_call
def get(self, object_models: list[str], output_type: Literal["dict", "DataFrame"] = "dict") -> dict[str, dict[str, Any]] | DataFrame:
"""Gets the attribute definitions for specific object models.
These can be viewed in the portal in `Object Types > Model Attribute Definition`.
Most of the attributes will have the following properties:
- attributeId
- categoryId
- displayId
- groupId
- isBuiltIn
- isMandatory
- isVisible
- name
- valueType
Parameters
----------
object_models : list[str]
List of object models to get the attributes 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: {name: {attribute: value, ...}, ...}
DataFrame
In case output_type == "DataFrame" it will return a DataFrame with the following format: index = name, columns = [attribute, ...]
"""
# getting object type
object_models_types = self.baze.objects.models.get()
object_models_types = {k: v["itemTypeName"] for k, v in object_models_types.items() if k in object_models}
if not object_models_types:
raise ValueError("No valid object models were given.")
# getting object type and category ids
object_types = self.baze.objects.types.get()
object_types = {k: {"typeId": v["type"]["typeId"], "categoryId": v["type"]["categoryId"]} for k, v in object_types.items()}
attributes_def = []
# getting attribute definitions
for object_model_name, object_model_type in object_models_types.items():
# getting the data
endpoint = f"configuration/objecttype/{object_types[object_model_type]['typeId']}/category/{object_types[object_model_type]['categoryId']}/attributes/model"
result = self.baze.conn.get(endpoint)
self._handle_http_errors(result)
# converting to dict
result: dict[str, Any] = result.json()
# converting to a better format
# {attribute_key: {name: name, value: value, valueType: valueType}, ...}
result = {entry["key"]: entry for entry in result}
# removing key
for attribute in result.values():
del attribute["key"]
# adding to root list
attributes_def.append({"name": object_model_name} | result)
# returning on the desired format
match output_type:
case "dict":
# defining the name as the key for the dict
result_dict = {entry["name"]: entry for entry in attributes_def}
# removing the name from the dict values
for entry in result_dict.values():
del entry["name"]
return result_dict
case "DataFrame":
df = json_normalize(attributes_def, max_level=1)
df = df.convert_dtypes(dtype_backend="pyarrow")
df = df.set_index("name")
return df
case _:
raise ValueError(f"Invalid output_type: {output_type}")
ObjectModelAttributeValues(baze)
¶
Class used for handling object model attribute values.
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 the attribute values for specific object models.
These can be viewed and set in the Object Models section of the portal.
Parameters:
-
(object_models¶list[str]) –List of object models to get the attributes 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: {name: {attribute: value, ...}, ...}
-
DataFrame–In case output_type == "DataFrame" it will return a DataFrame with the following format: index = name, columns = [attribute, ...]
Source code in echo_baze/object_model_attribute_values.py
@validate_call
def get(self, object_models: list[str], output_type: Literal["dict", "DataFrame"] = "dict") -> dict[str, dict[str, Any]] | DataFrame:
"""Gets the attribute values for specific object models.
These can be viewed and set in the Object Models section of the portal.
Parameters
----------
object_models : list[str]
List of object models to get the attributes 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: {name: {attribute: value, ...}, ...}
DataFrame
In case output_type == "DataFrame" it will return a DataFrame with the following format: index = name, columns = [attribute, ...]
"""
# getting ids of the object models
object_models_ids = self.baze.objects.models.get_ids()
for object_model in object_models:
if object_model not in object_models_ids:
raise ValueError(f"Invalid object model: {object_model}")
attribute_values = []
for object_model in object_models:
endpoint = f"configuration/domains/{object_models_ids[object_model]}"
# getting the data
result = self.baze.conn.get(endpoint)
self._handle_http_errors(result)
# converting to dict
result: dict[str, Any] = result.json()
# casting data types
result = self.baze.objects.instances._cast_attributes([result])[0] # pylint: disable=protected-access # noqa
# converting to a better format
# {attribute_key: value, ...}
result = result["attributes"]
# adding to root list
attribute_values.append({"model_name": object_model} | result)
# returning on the desired format
match output_type:
case "dict":
# defining the name as the key for the dict
result_dict = {entry["model_name"]: entry for entry in attribute_values}
# removing the name from the dict values
for entry in result_dict.values():
del entry["model_name"]
return result_dict
case "DataFrame":
df = json_normalize(attribute_values, max_level=1)
df = df.convert_dtypes(dtype_backend="pyarrow")
df = df.set_index("model_name")
return df
case _:
raise ValueError(f"Invalid output_type: {output_type}")
update(object_model, attributes)
¶
Updates attribute values for a specific object model.
This action can be done in the Object Models section of the portal.
Parameters:
-
(object_model¶str) –Name of the desired object model.
-
(attributes¶dict[str, Any]) –Dict with the attributes to be updated. It must be in the format {key: value, ...}
Source code in echo_baze/object_model_attribute_values.py
@validate_call
def update(self, object_model: str, attributes: dict[str, Any]) -> None:
"""Updates attribute values for a specific object model.
This action can be done in the Object Models section of the portal.
Parameters
----------
object_model : str
Name of the desired object model.
attributes : dict[str, Any]
Dict with the attributes to be updated. It must be in the format {key: value, ...}
"""
# getting ids of the object models
object_models_ids = self.baze.objects.models.get_ids()
# getting the model definition
endpoint = f"configuration/domains/{object_models_ids[object_model]}"
# getting the data
result = self.baze.conn.get(endpoint)
self._handle_http_errors(result)
# converting to dict
model_def: dict[str, Any] = result.json()
# changing attributes
for attribute_key, attribute_value in attributes.items():
if attribute_key not in model_def["attributes"]:
raise ValueError(f"Attribute key {attribute_key} is not valid for object model {object_model}.")
model_def["attributes"][attribute_key] = attribute_value
# updating
endpoint = "configuration/domains"
payload = deepcopy(model_def)
result = self.baze.conn.post(endpoint, json=payload)
self._handle_http_errors(result)
# converting to dict
result: dict[str, Any] = result.json()
# checking if the update was successful
for attribute_name, attribute_value in result["attributes"].items():
if attribute_value != str(payload["attributes"][attribute_name]):
raise ValueError(f"Updating attribute {attribute_name} failed.")