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'], 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 is "dict", returns a dictionary in the format {component_type_name: {name: {attribute: value, ...}, ...}, ...}
-
DataFrame–In case output_type is "DataFrame", returns a DataFrame with the following format: index = MultiIndex["component_type_name", "name"], columns = [attribute, ...]
Source code in echo_postgres/subcomponent_types.py
@validate_call
def get(
self,
component_types: list[str] | None = None,
output_type: Literal["dict", "DataFrame"] = "dict",
) -> dict[str, dict[str, Any]] | 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"], 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 is "dict", returns a dictionary in the format {component_type_name: {name: {attribute: value, ...}, ...}, ...}
DataFrame
In case output_type is "DataFrame", returns a DataFrame with the following format: index = MultiIndex["component_type_name", "name"], columns = [attribute, ...]
"""
# 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)
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query)
df = df.set_index(["component_type_name", "name"])
if output_type == "DataFrame":
return df
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
return final_result
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)
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query)
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