Object Models¶
ObjectModels(perfdb)
¶
Class used for handling object models. Can be accessed via perfdb.objects.models.
Parameters:
Source code in echo_postgres/object_models.py
def __init__(self, perfdb: e_pg.PerfDB) -> None:
"""Class used for handling object models. Can be accessed via `perfdb.objects.models`.
Parameters
----------
perfdb : PerfDB
Top level object carrying all functionality and the connection handler.
"""
super().__init__(perfdb)
from .object_model_attributes import ObjectModelAttributes
# * subclasses
self.attributes = ObjectModelAttributes(perfdb)
get(object_models=None, object_types=None, filter_type='and', get_attributes=False, output_type='dict')
¶
Gets all object models definitions with detailed information.
The most useful keys/columns returned are:
- id
- display_name
- description
- object_type_name
Parameters:
-
(object_models¶list[str] | None, default:None) –List of object model names to filter the results. By default None
-
(object_types¶list[str] | None, default:None) –List of object type names to filter the results. By default None
-
(filter_type¶Literal['and', 'or'], default:'and') –How to treat multiple filters. Can be one of ["and", "or"]. By default "and"
-
(get_attributes¶bool, default:False) –If True, will also get the attributes of the object models.
-
(output_type¶Literal['dict', 'DataFrame', 'pl.DataFrame'], default:'dict') –Output type of the data. Can be one of ["dict", "DataFrame", "pl.DataFrame"] By default "dict"
Returns:
-
dict[str, dict[str, str | int]]–In case output_type is "dict", returns a dictionary in the format {name: {attribute: value, ...}, ...}
-
DataFrame–In case output_type is "DataFrame", returns a pandas DataFrame with the following format: index = name, columns = [attribute, ...]
-
DataFrame–In case output_type is "pl.DataFrame", returns a Polars DataFrame
Source code in echo_postgres/object_models.py
@validate_call
def get(
self,
object_models: list[str] | None = None,
object_types: list[str] | None = None,
filter_type: Literal["and", "or"] = "and",
get_attributes: bool = False,
output_type: Literal["dict", "DataFrame", "pl.DataFrame"] = "dict",
) -> dict[str, dict[str, str | int]] | pd.DataFrame | pl.DataFrame:
"""Gets all object models definitions with detailed information.
The most useful keys/columns returned are:
- id
- display_name
- description
- object_type_name
Parameters
----------
object_models : list[str] | None, optional
List of object model names to filter the results.
By default None
object_types : list[str] | None, optional
List of object type names to filter the results.
By default None
filter_type : Literal["and", "or"], optional
How to treat multiple filters. Can be one of ["and", "or"].
By default "and"
get_attributes : bool, optional
If True, will also get the attributes of the object models.
output_type : Literal["dict", "DataFrame", "pl.DataFrame"], optional
Output type of the data. Can be one of ["dict", "DataFrame", "pl.DataFrame"]
By default "dict"
Returns
-------
dict[str, dict[str, str | int]]
In case output_type is "dict", returns a dictionary in the format {name: {attribute: value, ...}, ...}
pd.DataFrame
In case output_type is "DataFrame", returns a pandas DataFrame with the following format: index = name, columns = [attribute, ...]
pl.DataFrame
In case output_type is "pl.DataFrame", returns a Polars DataFrame
"""
# checking inputs
where = self._check_get_args(object_models, object_types, filter_type)
query = [
sql.SQL("SELECT * FROM performance.v_object_models "),
where,
sql.SQL(" ORDER BY name"),
]
query = sql.Composed(query)
df = self._perfdb.conn.read_to_polars(query)
# getting attributes
if get_attributes:
# names of the object models
got_object_models = df["name"].to_list()
attrs: pl.DataFrame = self._perfdb.objects.models.attributes.get(
object_models=got_object_models,
output_type="pl.DataFrame",
values_only=True,
)
# pivot the attributes
attrs = attrs.pivot(index="object_model_name", on="attribute_name", values="attribute_value")
# merging the attributes with the object models
df = df.join(attrs, left_on="name", right_on="object_model_name", how="left")
return convert_output(df, output_type)
get_ids(object_models=None, object_types=None, filter_type='and')
¶
Gets all object models and their respective ids.
Parameters:
-
(object_models¶list[str] | None, default:None) –List of object model names to filter the results. By default None
-
(object_types¶list[str] | None, default:None) –List of object type names to filter the results. By default None
-
(filter_type¶Literal['and', 'or'], default:'and') –How to treat multiple filters. Can be one of ["and", "or"]. By default "and"
Returns:
-
dict[str, int]–Dictionary with all object models and their respective ids in the format {name: id, ...}.
Source code in echo_postgres/object_models.py
@validate_call
def get_ids(
self,
object_models: list[str] | None = None,
object_types: list[str] | None = None,
filter_type: Literal["and", "or"] = "and",
) -> dict[str, int]:
"""Gets all object models and their respective ids.
Parameters
----------
object_models : list[str] | None, optional
List of object model names to filter the results.
By default None
object_types : list[str] | None, optional
List of object type names to filter the results.
By default None
filter_type : Literal["and", "or"], optional
How to treat multiple filters. Can be one of ["and", "or"].
By default "and"
Returns
-------
dict[str, int]
Dictionary with all object models and their respective ids in the format {name: id, ...}.
"""
# checking inputs
where = self._check_get_args(object_models, object_types, filter_type)
query = [
sql.SQL("SELECT name, id FROM performance.v_object_models "),
where,
sql.SQL(" ORDER BY name"),
]
query = sql.Composed(query)
df = self._perfdb.conn.read_to_polars(query)
return dict(zip(df["name"].to_list(), df["id"].to_list(), strict=False))