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