Utilities¶
- class load_atoms.utils.LazyMapping(keys: Sequence[T], loader: Callable[[T], Y])[source]¶
Bases:
Mapping
[T
,Y
]A mapping that lazily loads its values.
Concretely, the first time a key is accessed, the loader function is called to get the value for that key. Subsequent accesses to the same key will return the same value without calling the loader function again.
- Parameters:
keys (Sequence[T]) – The keys of the mapping.
loader (Callable[[T], Y]) – A function that takes a key and returns a value.
Examples
>>> def loader(key): ... print(f"Loading value for key={key}") ... return key * 2 ... >>> mapping = LazyMapping([1, 2, 3], loader) >>> mapping[3] Loading value for key=3 6 >>> mapping[3] 6 >>> 1 in mapping True >>> 4 in mapping False