Skip to content

Dataset Resources

CceeOpenDataDatasetsResources(ccee)

Class used for handling CCEE dataset resources. Can be accessed via ccee.opendata.datasets.resources.

Parameters:

  • ccee

    (Ccee) –

    Top level object carrying all functionality.

Source code in echo_ons/ccee_opendata_datasets_resources.py
def __init__(self, ccee: e_o.Ccee) -> None:
    """Class used for handling CCEE dataset resources. Can be accessed via `ccee.opendata.datasets.resources`.

    Parameters
    ----------
    ccee : Ccee
        Top level object carrying all functionality.
    """
    super().__init__(ccee)

    # * subclasses

    self.values = CceeOpenDataDatasetsResourcesValues(ccee)

get(dataset_name, output_type='dict')

Gets all the resources from a dataset.

Parameters:

  • dataset_name

    (str) –

    Name of the dataset to get the resources from.

  • output_type

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

    Type of the output, by default "dict"

Returns:

  • dict[str, dict[str, Any]]

    If output_type is "dict", returns a dictionary with the results. The keys are the resource names.

  • DataFrame

    If output_type is "DataFrame", returns a DataFrame with the results. The index is the resource names.

Source code in echo_ons/ccee_opendata_datasets_resources.py
def get(
    self,
    dataset_name: str,
    output_type: Literal["dict", "DataFrame"] = "dict",
) -> dict[str, dict[str, Any]] | DataFrame:
    """Gets all the resources from a dataset.

    Parameters
    ----------
    dataset_name : str
        Name of the dataset to get the resources from.
    output_type : Literal["dict", "DataFrame"], optional
        Type of the output, by default "dict"

    Returns
    -------
    dict[str, dict[str, Any]]
        If output_type is "dict", returns a dictionary with the results. The keys are the resource names.
    DataFrame
        If output_type is "DataFrame", returns a DataFrame with the results. The index is the resource names.
    """
    if not isinstance(dataset_name, str):
        raise ValueError(f"dataset_name must be of type str, not {type(dataset_name)}")
    if output_type not in ["dict", "DataFrame"]:
        raise ValueError(f"output_type must be 'dict' or 'DataFrame', not {output_type}")

    # searching for the dataset
    dataset = self._ccee.opendata.datasets.search("name", dataset_name, output_type="dict")
    if dataset_name not in dataset:
        raise ValueError(f"Dataset {dataset_name} not found")

    resources = dataset[dataset_name]["resources"]

    if output_type == "dict":
        # converting list of dicts to dict
        resources = {resource["name"]: resource for resource in resources}
    else:
        # converting list of dicts to DataFrame
        resources = DataFrame(resources).set_index("name")

    return resources