Subcomponent Types¶
SubcomponentsTypes(perfdb)
¶
Class used for handling subcomponents types. Can be accessed via perfdb.components.subcomponents.types.
Parameters:
Source code in echo_postgres/perfdb_root.py
def __init__(self, perfdb: e_pg.PerfDB) -> None:
"""Base class that all subclasses should inherit from.
Parameters
----------
perfdb : PerfDB
Top level object carrying all functionality and the connection handler.
"""
self._perfdb: e_pg.PerfDB = perfdb
get(component_types=None, output_type='dict')
¶
Gets all subcomponents types definitions with detailed information.
The most useful keys/columns returned are:
- id
- description
- component_type_id
Parameters:
-
(component_types¶list[str] | None, default:None) –List of component types to get the subcomponents types ids. By default None.
-
(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, Any]]–In case output_type is "dict", returns a dictionary in the format {component_type_name: {name: {attribute: value, ...}, ...}, ...}
-
DataFrame–In case output_type is "DataFrame", returns a pandas DataFrame with the following format: index = MultiIndex["component_type_name", "name"], columns = [attribute, ...]
-
DataFrame–In case output_type is "pl.DataFrame", returns a Polars DataFrame
Source code in echo_postgres/subcomponent_types.py
@validate_call
def get(
self,
component_types: list[str] | None = None,
output_type: Literal["dict", "DataFrame", "pl.DataFrame"] = "dict",
) -> dict[str, dict[str, Any]] | pd.DataFrame | pl.DataFrame:
"""Gets all subcomponents types definitions with detailed information.
The most useful keys/columns returned are:
- id
- description
- component_type_id
Parameters
----------
component_types : list[str] | None, optional
List of component types to get the subcomponents types ids. By default None.
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, Any]]
In case output_type is "dict", returns a dictionary in the format {component_type_name: {name: {attribute: value, ...}, ...}, ...}
pd.DataFrame
In case output_type is "DataFrame", returns a pandas DataFrame with the following format: index = MultiIndex["component_type_name", "name"], columns = [attribute, ...]
pl.DataFrame
In case output_type is "pl.DataFrame", returns a Polars DataFrame
"""
# checking arguments
where = self._check_get_args(component_types)
query = [sql.SQL("SELECT * FROM performance.v_subcomponent_types ")]
query.append(where)
query.append(sql.SQL(" ORDER BY component_type_name, name"))
query = sql.Composed(query)
df = self._perfdb.conn.read_to_polars(query)
return convert_output(df, output_type, index_col=["component_type_name", "name"], nest_by_index=True)
get_ids(component_types=None)
¶
Gets all subcomponents types and their respective ids.
Parameters:
-
(component_types¶list[str] | None, default:None) –List of component types to get the subcomponents types ids. By default None.
Returns:
-
dict[str, dict[str, int]]–Dictionary with all subcomponents types and their respective ids in the format {component_type: {subcomponents_type: id, ...}, ...}.
Source code in echo_postgres/subcomponent_types.py
@validate_call
def get_ids(self, component_types: list[str] | None = None) -> dict[str, int]:
"""Gets all subcomponents types and their respective ids.
Parameters
----------
component_types : list[str] | None, optional
List of component types to get the subcomponents types ids. By default None.
Returns
-------
dict[str, dict[str, int]]
Dictionary with all subcomponents types and their respective ids in the format {component_type: {subcomponents_type: id, ...}, ...}.
"""
# checking arguments
where = self._check_get_args(component_types)
query = [sql.SQL("SELECT component_type_name, name, id FROM performance.v_subcomponent_types ")]
query.append(where)
query.append(sql.SQL(" ORDER BY component_type_name, name"))
query = sql.Composed(query)
df = self._perfdb.conn.read_to_polars(query)
df = df.to_pandas(use_pyarrow_extension_array=True)
df = df.set_index(["component_type_name", "name"])
result = df.to_dict(orient="index")
final_result = {}
for (component_type_name, name), values in result.items():
if component_type_name not in final_result:
final_result[component_type_name] = {}
final_result[component_type_name][name] = values["id"]
return final_result