select¶
-
class
Select¶ A Select is a method of modifying a
Mapping[str, Any]. A Select should be considered immutable-
empty= Select()¶ An Empty Select, to be used as a default value to represent no mutations over a mapping
-
__init__(*, keys_to_add: Union[Iterable[Tuple[str, Any]], Mapping[str, Any]] = (), keys_to_maybe_add: Union[Iterable[Tuple[str, Any]], Mapping[str, Any]] = (), keys_to_remove: Union[Iterable[str], str] = (), keys_to_maybe_remove: Union[Iterable[str], str] = (), keys_to_rename: Union[Iterable[Tuple[str, str]], Mapping[str, str]] = (), keys_to_maybe_rename: Union[Iterable[Tuple[str, str]], Mapping[str, str]] = ())¶ - Parameters
keys_to_add – Keys to add to the mapping, raising an error if they already exist
keys_to_maybe_add – Keys to add to the mapping, or skip if they already exist
keys_to_remove – Keys to remove from the mapping, raising an error if they don’t exist. If a single string is presented, it is interpreted as a single key to remove.
keys_to_maybe_remove – Keys to remove from the mapping if they exist. If a single string is presented, it is interpreted as a single key to remove.
keys_to_rename – Keys to whose value to transfer to other keys inside the mapping, raising an error if the new name already exists
keys_to_maybe_rename – Keys to whose value to transfer to other keys inside the mapping, or skip if the new name already exists
-
merge(*others: records.select.Select, **kwargs)¶ combine several Selects into one
-
-
@SelectableFactory(*args, **kwds)¶ A class to hold class factories that can be configured with the
selectmethodNote
Users can create their own SelectableFactory by using this class as decorator (on top of
@classmethod)-
select(self, *selects: records.select.Select, **kwargs)¶ create a new bound factory with a modified
Select
-
-
@Exporter(*args, **kwds)¶ An exported function that supports selection and additional exporting configuration
Note
Users can create their own Exporter by using this class as decorator (on top of
@staticmethod)-
select(self, *selects: records.select.Select, **kwargs)¶ create a new bound exporter with a modified
Select
-
export_with(self, *args, **kwargs)¶ create a new bound exporter with modified export arguments
- Parameters
args – forwarded to
RecordBase._to_dict()kwargs – forwarded to
RecordBase._to_dict()
- Returns
a new bound exporter
-
Selection¶
All selectable factories and exporters can be called with
selection. Selection allows users to treat certain dictionary keys differently. This can be used to create aliases,
or to hide or synthesize some attributes when exporting or importing from other APIs. Selection is quite intuitive
and is done with the SelectableFactory.select
or Exporter.select methods.
class A(RecordBase):
x: int
y: str
# when exporting
a = A(x=5, y='6')
assert a.to_dict() == {'x': 5, 'y': 6}
assert a.to_dict.select(keys_to_remove='x')() == {'y': 6}
select = Select(keys_to_add = {'z': 3})
assert a.to_dict.select(select, keys_to_rename={'x':'X'}) == {'X': 5, 'y': 6, 'z': 3}
# when importing
A.from_dict({'x':5, 'y': '6'}) == a
A.from_dict.select(keys_to_add={'x': 5})({'y': 6}) == a
select = Select(keys_to_remove = 'z')
A.from_dict.select(select, keys_to_rename={'X': 'x'})({'X': 5, 'y': 6, 'z': 3}) == a