Object Types¶
ObjectTypes(baze)
¶
Class used for handling object types.
Parameters:
Source code in echo_baze/object_types.py
def __init__(self, baze: e_bz.Baze) -> None:
"""Class used for handling object types.
Parameters
----------
baze : Baze
Top level object carrying all functionality and the connection handler.
"""
super().__init__(baze)
# * subclasses
from .object_type_attributes import ObjectTypeAttributes
self.attributes = ObjectTypeAttributes(baze)
get(output_type='dict')
¶
Gets all object types with detailed information.
The most useful keys/columns returned are: - itemTypeId - acronym - type
Parameters:
-
(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_types.py
@validate_call
def get(self, output_type: Literal["dict", "DataFrame"] = "dict") -> dict[str, dict[str, Any]] | DataFrame:
"""Gets all object types with detailed information.
The most useful keys/columns returned are:
- itemTypeId
- acronym
- type
Parameters
----------
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, ...]
"""
# checking input
endpoint = "objectTypes"
# getting the data
result = self.baze.conn.get(endpoint)
self._handle_http_errors(result)
# converting to dict
result: list[dict[str, Any]] = result.json()
# 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 result}
# removing the name from the dict values
for entry in result_dict.values():
del entry["name"]
return result_dict
case "DataFrame":
df = json_normalize(result, 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}")
get_ids()
¶
Gets a dictionary with all object type names and ids.
Returns:
-
dict[str, int]:–Dictionary with all object types in the format {name: id, ...}
Source code in echo_baze/object_types.py
def get_ids(self) -> dict[str, int]:
"""Gets a dictionary with all object type names and ids.
Returns
-------
dict[str, int]:
Dictionary with all object types in the format {name: id, ...}
"""
endpoint = "objectTypes"
# getting the data
result = self.baze.conn.get(endpoint)
self._handle_http_errors(result)
# converting to dict
result: list[dict[str, Any]] = result.json()
return {entry["name"]: entry["type"]["typeId"] for entry in result}