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, 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) -> Iterator[str]: 47 self._check() 48 yield from self.data 49 50 def items(self) -> ItemsView: 51 self._check() 52 return self.data.items() 53 54 def values(self) -> ValuesView: 55 self._check() 56 return self.data.values() 57 58 59# LazyCalc lets us avoid the global keyword 60class LazyCalc: 61 """Delay data calculation until needed.""" 62 63 _func: Callable 64 _value: Any | None = None 65 66 def __init__(self, func: Callable): 67 self._func = func 68 69 @property 70 def value(self) -> Any: 71 if self._value is None: 72 self._value = self._func() 73 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) -> Iterator[str]: 48 self._check() 49 yield from self.data 50 51 def items(self) -> ItemsView: 52 self._check() 53 return self.data.items() 54 55 def values(self) -> ValuesView: 56 self._check() 57 return self.data.values()
Lazy load a dictionary from the JSON data cache.
class
LazyCalc:
61class LazyCalc: 62 """Delay data calculation until needed.""" 63 64 _func: Callable 65 _value: Any | None = None 66 67 def __init__(self, func: Callable): 68 self._func = func 69 70 @property 71 def value(self) -> Any: 72 if self._value is None: 73 self._value = self._func() 74 return self._value
Delay data calculation until needed.