Source code for airbase.util

"""Utility functions for processing the raw Portal responses, url templating, etc."""

import datetime

from .resources import (
    LINK_LIST_URL_TEMPLATE,
    CURRENT_YEAR,
    DATE_FMT,
    ALL_SOURCES,
)


[docs]def string_safe_list(obj): """ Turn an (iterable) object into a list. If it is a string or not iterable, put the whole object into a list of length 1. :param obj: :return list: """ if isinstance(obj, str) or not hasattr(obj, "__iter__"): return [obj] else: return list(obj)
[docs]def countries_from_summary(summary): """ Get the list of unique countries from the summary. :param list[dict] summary: The E1a summary. :return list[str]: The available countries. """ return list({d["ct"] for d in summary})
[docs]def pollutants_from_summary(summary): """ Get the list of unique pollutants from the summary. :param list[dict] summary: The E1a summary. :return dict: The available pollutants, with name ("pl") as key and pollutant number ("shortpl") as value. """ return {d["pl"]: d["shortpl"] for d in summary}
[docs]def pollutants_per_country(summary): """ Get the available pollutants per country from the summary. :param list[dict] summary: The E1a summary. :return dict[list[dict]]: All available pollutants per country. """ output = dict() for d in summary.copy(): country = d.pop("ct") if country in output: output[country].append(d) else: output[country] = [d] return output