avwx.station.meta

Shared list and metadata

 1"""
 2Shared list and metadata
 3"""
 4
 5# stdlib
 6from functools import lru_cache
 7from typing import List, Optional
 8
 9# module
10from avwx.exceptions import BadStation
11from avwx.load_utils import LazyLoad
12from avwx.static.core import IN_REGIONS, M_IN_REGIONS, M_NA_REGIONS, NA_REGIONS
13
14__LAST_UPDATED__ = "2023-10-28"
15
16# Lazy data loading to speed up import times for unused features
17STATIONS = LazyLoad("stations")
18
19
20# maxsize = 2 ** number of boolean options
21@lru_cache(maxsize=2)
22def station_list(reporting: bool = True) -> List[str]:
23    """Returns a list of station idents matching the search criteria"""
24    return [
25        code
26        for code, station in STATIONS.items()
27        if not reporting or station["reporting"]
28    ]
29
30
31def uses_na_format(station: str, default: Optional[bool] = None) -> bool:
32    """Returns True if the station uses the North American format,
33
34    False if the International format
35    """
36    if station[0] in NA_REGIONS:
37        return True
38    if station[0] in IN_REGIONS:
39        return False
40    if station[:2] in M_NA_REGIONS:
41        return True
42    if station[:2] in M_IN_REGIONS:
43        return False
44    if default is not None:
45        return default
46    raise BadStation("Station doesn't start with a recognized character set")
47
48
49def valid_station(station: str) -> None:
50    """Checks the validity of a station ident
51
52    This function doesn't return anything. It merely raises a BadStation error if needed
53    """
54    station = station.strip()
55    if len(station) != 4:
56        raise BadStation("Report station ident must be four characters long")
57    uses_na_format(station)
STATIONS = <avwx.load_utils.LazyLoad object>
@lru_cache(maxsize=2)
def station_list(reporting: bool = True) -> List[str]:
22@lru_cache(maxsize=2)
23def station_list(reporting: bool = True) -> List[str]:
24    """Returns a list of station idents matching the search criteria"""
25    return [
26        code
27        for code, station in STATIONS.items()
28        if not reporting or station["reporting"]
29    ]

Returns a list of station idents matching the search criteria

def uses_na_format(station: str, default: Optional[bool] = None) -> bool:
32def uses_na_format(station: str, default: Optional[bool] = None) -> bool:
33    """Returns True if the station uses the North American format,
34
35    False if the International format
36    """
37    if station[0] in NA_REGIONS:
38        return True
39    if station[0] in IN_REGIONS:
40        return False
41    if station[:2] in M_NA_REGIONS:
42        return True
43    if station[:2] in M_IN_REGIONS:
44        return False
45    if default is not None:
46        return default
47    raise BadStation("Station doesn't start with a recognized character set")

Returns True if the station uses the North American format,

False if the International format

def valid_station(station: str) -> None:
50def valid_station(station: str) -> None:
51    """Checks the validity of a station ident
52
53    This function doesn't return anything. It merely raises a BadStation error if needed
54    """
55    station = station.strip()
56    if len(station) != 4:
57        raise BadStation("Report station ident must be four characters long")
58    uses_na_format(station)

Checks the validity of a station ident

This function doesn't return anything. It merely raises a BadStation error if needed