Companies¶
Companies(perfdb)
¶
Class used for handling Companies. Can be accessed via perfdb.v_companies.
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(company_involved=None, company_responsible=None, company_types=None, output_type='dict')
¶
Gets all companies with detailed information.
The most useful keys/columns returned are:
- id
- country
Parameters:
-
(company_types¶list[str] | None, default:None) –Filter companies by their type name (column
type_namein the view). 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, 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 DataFrame with the following format: index = name, columns = [attribute, ...]
Source code in echo_postgres/companies.py
@validate_call
def get(
self,
company_involved: bool | None = None,
company_responsible: bool | None = None,
company_types: list[str] | None = None,
output_type: Literal["dict", "DataFrame"] = "dict",
) -> dict[str, dict[str, str | int]] | DataFrame:
"""Gets all companies with detailed information.
The most useful keys/columns returned are:
- id
- country
Parameters
----------
company_types : list[str] | None, optional
Filter companies by their type name (column `type_name` in the view). 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, 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 DataFrame with the following format: index = name, columns = [attribute, ...]
"""
# checking arguments and building WHERE clause
where = self._check_get_args(company_involved, company_responsible, company_types)
query = sql.Composed([sql.SQL("SELECT * FROM performance.v_companies"), where, sql.SQL(" ORDER BY name")])
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query)
df = df.set_index("name")
return df.to_dict(orient="index") if output_type == "dict" else df
get_ids(company_involved=None, company_responsible=None, company_types=None)
¶
Gets all companies and their respective ids.
Parameters:
-
(company_involved¶bool | None, default:None) –Filter by
company_involvedcolumn when provided. By default None. -
(company_responsible¶bool | None, default:None) –Filter by
company_responsiblecolumn when provided. By default None.
Returns:
-
dict[str, int]–Dictionary with all companies and their respective ids in the format {data_type: id, ...}.
Source code in echo_postgres/companies.py
@validate_call
def get_ids(
self,
company_involved: bool | None = None,
company_responsible: bool | None = None,
company_types: list[str] | None = None,
) -> dict[str, int]:
"""Gets all companies and their respective ids.
Parameters
----------
company_involved : bool | None, optional
Filter by `company_involved` column when provided. By default None.
company_responsible : bool | None, optional
Filter by `company_responsible` column when provided. By default None.
Returns
-------
dict[str, int]
Dictionary with all companies and their respective ids in the format {data_type: id, ...}.
"""
# checking arguments and building WHERE clause
where = self._check_get_args(company_involved, company_responsible, company_types)
query = sql.Composed([sql.SQL("SELECT name, id FROM performance.v_companies"), where, sql.SQL(" ORDER BY name")])
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query)
return df.set_index("name").to_dict()["id"]