muutils.kappa
anonymous getitem class
util for constructing a class which has a getitem method which just calls a function
a lambda is an anonymous function: kappa is the letter before lambda in the greek alphabet,
hence the name of this class
1"""anonymous getitem class 2 3util for constructing a class which has a getitem method which just calls a function 4 5a `lambda` is an anonymous function: kappa is the letter before lambda in the greek alphabet, 6hence the name of this class""" 7 8from __future__ import annotations 9 10from typing import Callable, Final, Mapping, TypeVar 11 12_kappa_K = TypeVar("_kappa_K") 13_kappa_V = TypeVar("_kappa_V") 14 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 + """ 21 22source function docstring: 23==============================\n 24""" 25) 26 27 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 ) 36 37 def __getitem__(self, x: _kappa_K) -> _kappa_V: 38 return self.func_getitem(x) 39 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 ) 44 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 )
class
Kappa(typing.Mapping[~_kappa_K, ~_kappa_V]):
29class Kappa(Mapping[_kappa_K, _kappa_V]): 30 def __init__(self, func_getitem: Callable[[_kappa_K], _kappa_V]) -> None: 31 self.func_getitem = func_getitem 32 self.doc = _BASE_DOC + str( 33 getattr( 34 func_getitem, "__doc__", "<no docstring provided for source function>" 35 ) 36 ) 37 38 def __getitem__(self, x: _kappa_K) -> _kappa_V: 39 return self.func_getitem(x) 40 41 def __iter__(self) -> None: # type: ignore[override] 42 raise NotImplementedError( 43 "This method is not implemented for Kappa, we don't know the valid inputs" 44 ) 45 46 def __len__(self) -> int: 47 raise NotImplementedError( 48 "This method is not implemented for Kappa, no idea how many valid inputs there are" 49 )
A Mapping is a generic container for associating key/value pairs.
This class provides concrete generic implementations of all methods except for __getitem__, __iter__, and __len__.
Inherited Members
- collections.abc.Mapping
- get
- keys
- items
- values