rics.mapping package

Submodules

rics.mapping.exceptions module

Mapping errors.

exception rics.mapping.exceptions.MappingError[source]

Bases: ValueError

Something failed to map.

rics.mapping.score_functions module

Functions which return a “likeness” score.

rics.mapping.score_functions.MappingScoreFunction

Signature for a likeness score function.

Parameters
  • name – An element to find matches for.

  • candidates – Potential matches for value.

Keyword Arguments

kwargs – Accepted only by some functions.

Yields

A score for each candidate c in candidates.

alias of Callable[[H, Set[H]], Iterable[float]]

rics.mapping.score_functions.like_database_table(name: str, candidates: Iterable[str], apply_heuristics: bool = False) Iterable[float][source]

Try to make value look like the name of a database table.

rics.mapping.score_functions.modified_hamming(name: str, candidates: Iterable[str]) Iterable[float][source]

Compute hamming distance modified by candidate length, in reverse.

rics.mapping.score_functions.equality(value: H, candidates: Iterable[H]) Iterable[float][source]

Return 1.0 if k == c_i, 0.0 otherwise.

Parameters
  • value – An element to find matches for.

  • candidates – Potential matches for value.

Yields

A score for each candidate c in candidates.

rics.mapping.score_functions.get(name: str) Callable[[H, Set[H]], Iterable[float]][source]

Get a scoring function by name.

Module contents

Mapping implementations for matching groups of elements.

class rics.mapping.DirectionalMapping(cardinality: Optional[Union[str, Cardinality]] = None, left_to_right: Optional[Union[Dict[HL, Tuple[HR, ...]], Dict[HR, Tuple[HL, ...]]]] = None, right_to_left: Optional[Union[Dict[HL, Tuple[HR, ...]], Dict[HR, Tuple[HL, ...]]]] = None, _verify: bool = True)[source]

Bases: Generic[HL, HR]

A two-way mapping between hashable elements.

Parameters
  • cardinality – Explicit cardinality. None=derive.

  • left_to_right – A left-to-right mapping of elements.

  • right_to_left – A right-to-left mapping of elements.

  • _verify – If False, input checks are disabled. Intended for internal use.

Raises
property cardinality: Cardinality

Cardinality with which this mapping was created.

Returns

Cardinality with which this mapping was created.

property left: Tuple[HL, ...]

Left-side elements in the mapping.

property right: Tuple[HR, ...]

Right-side elements in the mapping.

property left_to_right: Union[Dict[HL, Tuple[HR, ...]], Dict[HR, Tuple[HL, ...]]]

Left-to-right element mappings.

property right_to_left: Union[Dict[HL, Tuple[HR, ...]], Dict[HR, Tuple[HL, ...]]]

Right-to-left element mappings.

property reverse: DirectionalMapping

Reverse the mapping by swapping the sides.

Returns

A copy with data identical to the calling instance, but with sides inversed compared to the caller.

flatten() Dict[HL, HR][source]

Return a flattened version of self as a dict.

Returns

A dict {left: right}.

Raises

CardinalityError – If cardinality is not OneToOne.

select_left(elements: Iterable[HL], exclude: bool = False) DirectionalMapping[source]

Perform a selection on left-side elements.

Parameters
  • elements – Elements to select.

  • exclude – If True, return everything except the given elements.

Returns

A new Mapping for the selection.

Raises

KeyError – If any of the chosen elements do not exist and exclude=False.

select_right(elements: Iterable[HR], exclude: bool = False) DirectionalMapping[source]

Perform a selection on right-side elements.

Parameters
  • elements – Elements to select.

  • exclude – If True, return everything except the given elements.

Returns

A new instance for the selection.

Raises

KeyError – If any of the chosen elements do not exist and exclude=False.

class rics.mapping.Mapper(candidates: Optional[Iterable[CandidateType]] = None, score_function: Union[Literal['heuristic', 'equality', 'like_database_table', 'modified_hamming'], Callable[[H, Set[H]], Iterable[float]]] = 'equality', min_score: float = 1.0, overrides: Optional[Dict[ValueType, CandidateType]] = None, unmapped_values_action: Literal['raise', 'ignore'] = 'ignore', cardinality: Optional[Union[str, Cardinality]] = Cardinality.OneToOne, **score_function_kwargs: Any)[source]

Bases: Generic[ValueType, CandidateType]

Map candidates (right side) to values (left side).

A Mapper creates BidirectionalMapping-instances from collections of candidates for sets of values passed to apply().

Parameters
  • candidates – Possible items for values to be matched with.

  • score_function – A callable which accepts a value k and an ordered collection of candidates c, returning a score s_i for each candidate c_i in c. Default: s_i = float(k == c_i). Higher=better match.

  • min_score – Minimum score s_i, as given by score(k, c_i), to consider k a match for c_i.

  • overrides – User-defined 1:1 mappings which (value to candidate) override the scoring logic.

  • unmapped_values_action – Action to take if mapping fails.

  • cardinality – Desired cardinality for mapped values. None=derive.

classmethod from_dict(config: Dict[str, Any]) Mapper[source]

Create instance from a dict.

Parameters

config – Dict configuration.

Returns

A new instance.

property candidates: Set[CandidateType]

Candidates to match with when apply is called.

apply(values: Iterable[ValueType]) DirectionalMapping[source]

Map values to candidates.

Parameters

values – Iterable of elements to match to candidates.

Returns

A BidirectionalMapping with values on the left side and candidates on the right.