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

Parameters
  • others – other selects to combine with self

  • kwargs – additional arguments to create a select with, and merge it.

Returns

a single Select, formed by self, all of others, and a Select formed with kwargs

__call__(mapping: Mapping[str, Any]) → Mapping[str, Any]
Parameters

mapping – The mapping to use as input

Returns

A modified Mapping as specified by self

Warning

This function may modify mapping.

@SelectableFactory(*args, **kwds)

A class to hold class factories that can be configured with the select method

Note

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

Parameters
  • selects – the new Selects to merge into the existing select

  • kwargs – keyword argument to merge into the new Selects

Returns

a new bound factory

@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

Parameters
  • selects – the new Selects to merge into the existing select

  • kwargs – keyword argument to merge into the new Select

Returns

a new bound exporter

export_with(self, *args, **kwargs)

create a new bound exporter with modified export arguments

Parameters
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