cbrkit.adapt
CBRkit contains a selection of adaptation functions for different data types.
Besides functions for standard data types like
numbers (cbrkit.adapt.numbers
),
strings (cbrkit.adapt.strings
),
and generic data (cbrkit.adapt.generic
),
there is also a function for attribute-value data.
1""" 2CBRkit contains a selection of adaptation functions for different data types. 3Besides functions for standard data types like 4numbers (`cbrkit.adapt.numbers`), 5strings (`cbrkit.adapt.strings`), 6and generic data (`cbrkit.adapt.generic`), 7there is also a function for attribute-value data. 8""" 9 10from . import generic, numbers, strings 11from .attribute_value import attribute_value 12 13__all__ = [ 14 "generic", 15 "strings", 16 "numbers", 17 "attribute_value", 18]
@dataclass(slots=True, frozen=True)
class
attribute_value26@dataclass(slots=True, frozen=True) 27class attribute_value[V](AdaptationFunc[V]): 28 """Adapt values of attributes using specified adaptation functions. 29 30 This class allows for the adaptation of multiple attributes of a case by applying 31 one or more adaptation functions to each attribute. It supports different data structures 32 like mappings (dictionaries) and dataframes 33 34 Args: 35 attributes: A mapping of attribute names to either single adaptation functions or 36 sequences of adaptation functions that will be applied in order. 37 value_getter: Function to retrieve values from objects. Defaults to dictionary/attribute access. 38 value_setter: Function to set values on objects. Defaults to dictionary/attribute assignment. 39 40 Returns: 41 A new case with adapted attribute values. 42 43 Examples: 44 >>> func = attribute_value({ 45 ... "name": lambda x, y: x if x == y else y, 46 ... "age": lambda x, y: x if x > y else y, 47 ... }) 48 >>> result = func( 49 ... {"name": "Alice", "age": 30}, 50 ... {"name": "Peter", "age": 25} 51 ... ) 52 >>> result 53 {'name': 'Peter', 'age': 30} 54 """ 55 56 attributes: Mapping[str, MaybeSequence[AdaptationFunc[Any]]] 57 value_getter: Callable[[Any, str], Any] = default_value_getter 58 value_setter: Callable[[Any, str, Any], None] = default_value_setter 59 60 @override 61 def __call__(self, case: V, query: V) -> V: 62 for attr_name in self.attributes: 63 adapt_funcs = produce_sequence(self.attributes[attr_name]) 64 65 case_attr_value = self.value_getter(case, attr_name) 66 query_attr_value = self.value_getter(query, attr_name) 67 68 for adapt_func in adapt_funcs: 69 case_attr_value = adapt_func(case_attr_value, query_attr_value) 70 71 self.value_setter(case, attr_name, case_attr_value) 72 73 return case
Adapt values of attributes using specified adaptation functions.
This class allows for the adaptation of multiple attributes of a case by applying one or more adaptation functions to each attribute. It supports different data structures like mappings (dictionaries) and dataframes
Arguments:
- attributes: A mapping of attribute names to either single adaptation functions or sequences of adaptation functions that will be applied in order.
- value_getter: Function to retrieve values from objects. Defaults to dictionary/attribute access.
- value_setter: Function to set values on objects. Defaults to dictionary/attribute assignment.
Returns:
A new case with adapted attribute values.
Examples:
>>> func = attribute_value({ ... "name": lambda x, y: x if x == y else y, ... "age": lambda x, y: x if x > y else y, ... }) >>> result = func( ... {"name": "Alice", "age": 30}, ... {"name": "Peter", "age": 25} ... ) >>> result {'name': 'Peter', 'age': 30}
attribute_value( attributes: Mapping[str, MaybeSequence[cbrkit.typing.AdaptationFunc[typing.Any]]], value_getter: Callable[[typing.Any, str], typing.Any] = <function default_value_getter>, value_setter: Callable[[typing.Any, str, typing.Any], None] = <function default_value_setter>)
attributes: Mapping[str, MaybeSequence[cbrkit.typing.AdaptationFunc[typing.Any]]]