Coverage for muutils / kappa.py: 100%
15 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-18 02:51 -0700
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-18 02:51 -0700
1"""anonymous getitem class
3util for constructing a class which has a getitem method which just calls a function
5a `lambda` is an anonymous function: kappa is the letter before lambda in the greek alphabet,
6hence the name of this class"""
8from __future__ import annotations
10from typing import Callable, Final, Mapping, TypeVar
12_kappa_K = TypeVar("_kappa_K")
13_kappa_V = TypeVar("_kappa_V")
15# get the docstring of this file
16_BASE_DOC: Final[str] = (
17 # TYPING: type checkers complain here, they have no idea that this module does in fact have a __doc__
18 __doc__
19 or "anonymous getitem class"
20 + """
22source function docstring:
23==============================\n
24"""
25)
28class Kappa(Mapping[_kappa_K, _kappa_V]):
29 def __init__(self, func_getitem: Callable[[_kappa_K], _kappa_V]) -> None:
30 self.func_getitem = func_getitem
31 self.doc = _BASE_DOC + str(
32 getattr(
33 func_getitem, "__doc__", "<no docstring provided for source function>"
34 )
35 )
37 def __getitem__(self, x: _kappa_K) -> _kappa_V:
38 return self.func_getitem(x)
40 def __iter__(self) -> None: # type: ignore[override]
41 raise NotImplementedError(
42 "This method is not implemented for Kappa, we don't know the valid inputs"
43 )
45 def __len__(self) -> int:
46 raise NotImplementedError(
47 "This method is not implemented for Kappa, no idea how many valid inputs there are"
48 )