avwx.load_utils

Data load utilities.

 1"""Data load utilities."""
 2
 3from __future__ import annotations
 4
 5import json
 6from pathlib import Path
 7from typing import TYPE_CHECKING, Any
 8
 9if TYPE_CHECKING:
10    from collections.abc import Callable, ItemsView, Iterator, KeysView, ValuesView
11
12
13class LazyLoad:
14    """Lazy load a dictionary from the JSON data cache."""
15
16    source: Path
17    _data: dict[str, Any] | None = None
18
19    def __init__(self, filename: str):
20        self.source = Path(__file__).parent.joinpath("data", "files", f"{filename}.json")
21
22    def _load(self) -> None:
23        with self.source.open(encoding="utf8") as fin:
24            self._data = json.load(fin)
25
26    def _check(self) -> None:
27        if self._data is None:
28            self._load()
29
30    @property
31    def data(self) -> dict[str, Any]:
32        return self._data or {}
33
34    def __getitem__(self, key: str) -> Any:
35        self._check()
36        return self.data[key]
37
38    def __contains__(self, key: str) -> bool:
39        self._check()
40        return key in self.data
41
42    def __len__(self) -> int:
43        self._check()
44        return len(self.data)
45
46    def __iter__(self) -> KeysView:
47        self._check()
48        return self.data.keys()
49
50    def items(self) -> ItemsView:
51        self._check()
52        return self.data.items()
53
54    def keys(self) -> Iterator[str]:
55        self._check()
56        yield from self.data.keys()
57
58    def values(self) -> ValuesView:
59        self._check()
60        return self.data.values()
61
62
63# LazyCalc lets us avoid the global keyword
64class LazyCalc:
65    """Delay data calculation until needed."""
66
67    _func: Callable
68    _value: Any | None = None
69
70    def __init__(self, func: Callable):
71        self._func = func
72
73    @property
74    def value(self) -> Any:
75        if self._value is None:
76            self._value = self._func()
77        return self._value
class LazyLoad:
14class LazyLoad:
15    """Lazy load a dictionary from the JSON data cache."""
16
17    source: Path
18    _data: dict[str, Any] | None = None
19
20    def __init__(self, filename: str):
21        self.source = Path(__file__).parent.joinpath("data", "files", f"{filename}.json")
22
23    def _load(self) -> None:
24        with self.source.open(encoding="utf8") as fin:
25            self._data = json.load(fin)
26
27    def _check(self) -> None:
28        if self._data is None:
29            self._load()
30
31    @property
32    def data(self) -> dict[str, Any]:
33        return self._data or {}
34
35    def __getitem__(self, key: str) -> Any:
36        self._check()
37        return self.data[key]
38
39    def __contains__(self, key: str) -> bool:
40        self._check()
41        return key in self.data
42
43    def __len__(self) -> int:
44        self._check()
45        return len(self.data)
46
47    def __iter__(self) -> KeysView:
48        self._check()
49        return self.data.keys()
50
51    def items(self) -> ItemsView:
52        self._check()
53        return self.data.items()
54
55    def keys(self) -> Iterator[str]:
56        self._check()
57        yield from self.data.keys()
58
59    def values(self) -> ValuesView:
60        self._check()
61        return self.data.values()

Lazy load a dictionary from the JSON data cache.

LazyLoad(filename: str)
20    def __init__(self, filename: str):
21        self.source = Path(__file__).parent.joinpath("data", "files", f"{filename}.json")
source: pathlib.Path
data: dict[str, typing.Any]
31    @property
32    def data(self) -> dict[str, Any]:
33        return self._data or {}
def items(self) -> ItemsView:
51    def items(self) -> ItemsView:
52        self._check()
53        return self.data.items()
def keys(self) -> Iterator[str]:
55    def keys(self) -> Iterator[str]:
56        self._check()
57        yield from self.data.keys()
def values(self) -> ValuesView:
59    def values(self) -> ValuesView:
60        self._check()
61        return self.data.values()
class LazyCalc:
65class LazyCalc:
66    """Delay data calculation until needed."""
67
68    _func: Callable
69    _value: Any | None = None
70
71    def __init__(self, func: Callable):
72        self._func = func
73
74    @property
75    def value(self) -> Any:
76        if self._value is None:
77            self._value = self._func()
78        return self._value

Delay data calculation until needed.

LazyCalc(func: Callable)
71    def __init__(self, func: Callable):
72        self._func = func
value: Any
74    @property
75    def value(self) -> Any:
76        if self._value is None:
77            self._value = self._func()
78        return self._value