Skip to content

Cities

Cities(perfdb)

Class used for handling Cities. Can be accessed via perfdb.cities.

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='pl.DataFrame')

Gets all inventory centers and its attributes. The most useful keys/columns returned are:

  • id
  • name (index for pd.DataFrame)
  • state

Parameters:

  • output_type

    (Literal['dict', 'DataFrame', 'pl.DataFrame'], default: 'pl.DataFrame' ) –

    Output type of the data. Can be one of ["dict", "DataFrame", "pl.DataFrame"] By default "pl.DataFrame"

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/cities.py
@validate_call
def get(
    self,
    output_type: Literal["dict", "DataFrame", "pl.DataFrame"] = "pl.DataFrame",
) -> dict[str, dict[str, Any]] | pd.DataFrame | pl.DataFrame:
    """Gets all inventory centers and its attributes. The most useful keys/columns returned are:

    - id
    - name (index for pd.DataFrame)
    - state

    Parameters
    ----------
    output_type : Literal["dict", "DataFrame", "pl.DataFrame"], optional
        Output type of the data. Can be one of ["dict", "DataFrame", "pl.DataFrame"]
        By default "pl.DataFrame"

    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, ...]
    pl.DataFrame
        In case output_type is "pl.DataFrame", returns a polars DataFrame
    """
    query = sql.Composed([sql.SQL("SELECT * FROM performance.cities"), sql.SQL(" ORDER BY name")])

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

    if output_type == "pl.DataFrame":
        return df

    df = df.to_pandas(use_pyarrow_extension_array=True)
    df = df.set_index("name")

    return df.to_dict(orient="index") if output_type == "dict" else df