예제 #1
0
파일: loader.py 프로젝트: alan-yap/opennem
def load_registry() -> List[StationSchema]:
    """
        Loads the facility registry into a list of Station schema's
    """
    stations = load_data("facility_registry.json")

    records = []

    for station_id, station_record in stations.items():
        facilities = []

        for duid, facility_record in station_record["duid_data"].items():
            status = map_compat_facility_state(
                station_record.get("status", {}).get("state", "")
            )
            fuel_tech = map_compat_fueltech(
                facility_record.get("fuel_tech", "")
            )
            registered_capacity = clean_capacity(
                facility_record.get("registered_capacity")
            )

            facility = FacilitySchema(
                name=normalize_whitespace(station_record.get("display_name")),
                network_region=map_compat_network_region(
                    station_record["region_id"]
                ),
                status=status,
                duid=duid,
                fueltech=fuel_tech,
                capacity=registered_capacity,
            )

            facilities.append(facility)

        record = StationSchema(
            name=normalize_whitespace(station_record.get("display_name")),
            code=station_id,
            state=station_record.get("location", {}).get("state", None),
            facilities=_sort_facilities(facilities),
        )

        records.append(record)

    records = _sort_stations(records)

    return records
예제 #2
0
def optimized_data_parser(date_str: str) -> Optional[datetime]:
    """
    Turns out that dateutil's date parser is slow since
    it does a lot of string parsing. Here we try matching
    the date using known string formats.
    """
    dt_return = None

    date_str = normalize_whitespace(date_str).strip()

    for date_format_str in DATE_FORMATS:
        try:
            dt_return = datetime.strptime(date_str, date_format_str)
        except (KeyError, OverflowError, ValueError, AttributeError):
            continue
        if dt_return:
            return dt_return

    return dt_return
예제 #3
0
파일: schema.py 프로젝트: zalihat/opennem
    def name_clean(cls, v):
        name = normalize_whitespace(v)

        return name