Skip to content

Companies

Companies(perfdb)

Class used for handling Companies. Can be accessed via perfdb.companies.

Parameters:

  • perfdb

    (PerfDB) –

    Top level object carrying all functionality and the connection handler.

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(output_type='dict')

Gets all companies with detailed information.

The most useful keys/columns returned are:

  • id
  • country

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, 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, 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
    ----------
    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, ...]
    """
    query = sql.SQL("SELECT * FROM performance.companies 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()

Gets all companies and their respective ids.

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
def get_ids(self) -> dict[str, int]:
    """Gets all companies and their respective ids.

    Returns
    -------
    dict[str, int]
        Dictionary with all companies and their respective ids in the format {data_type: id, ...}.
    """
    query = sql.SQL("SELECT name, id FROM performance.companies ORDER BY name")

    with self._perfdb.conn.reconnect() as conn:
        df = conn.read_to_pandas(query)

    return df.set_index("name").to_dict()["id"]