Mapping Helpers

Odin includes a few helpers to help defining mappings.

Defining Mappings

When defining mappings using the shorthand mappings property these methods simplify the definition of mapping rules. They also provide sensible defaults.

Mapping data between resources or other object types.

odin.mapping.define(from_field: None | str | Sequence[str] = None, action: None | Callable[[Any, ...], Any | None] | Callable[[Mapping, Any, ...], Any | None] = None, to_field: None | str | Sequence[str] = None, to_list: bool = False, bind: bool = False, skip_if_none: bool = False)[source]

Helper method for defining a mapping.

Parameters:
  • from_field – Source field or fields to map from.

  • action – Callable Action to perform during mapping, if the bind flag is used the first parameter is the current mapping instance.

  • to_field – Destination field to map to; if not specified defaults to the from_field

  • to_list – Assume the result is a list (rather than a multi value tuple).

  • bind – During the mapping operation the first parameter should be the mapping instance.

  • skip_if_none – If the from field is None do not include the field (this allows the destination object to define it’s own defaults)

Returns:

A mapping definition.

odin.mapping.assign(to_field: str | Sequence[str], action: Callable[[Any, ...], Any | None] | Callable[[Mapping, Any, ...], Any | None], to_list=False, bind=True, skip_if_none=False)[source]

Helper method for defining an assignment mapping.

Parameters:
  • to_field – Destination field to map to; if not specified the from_field

  • action – Action callable to perform during mapping, accepted fields differ based on options.

  • to_list – Assume the result is a list (rather than a multi value tuple).

  • bind – During the mapping operation the first parameter should be the mapping instance; defaults to True

  • skip_if_none – If the from field is None do not include the field (this allows the destination object to define it’s own defaults etc)

Returns:

A mapping definition.

Action Helpers

Predefined actions for use in mapping definitions.

odin.mapping.helpers.sum_fields(*field_values)[source]

Return the sum of a number of fields.

Example:

define(from_field=("field_a", "field_b"), action=sum_fields, to_field="field_total")
class odin.mapping.helpers.JoinFields(sep: str = '')[source]

Helper for combining multiple fields.

Example:

define(from_field=("field_a", "field_b"), action=JoinFields(sep=":"), to_field="field_joined")
class odin.mapping.helpers.SplitField(sep: str = None, max_split: int = None)[source]

Helper for splitting a field into multiple fields.

Example:

define(from_field="field_joined", action=SplitField(sep=":"), to_field=("field_a", "field_b"))
class odin.mapping.helpers.ApplyMapping(mapping, allow_subclass: bool = False)[source]

Helper for applying a mapper.

This helper should be used along with the bind flag so the context object can be maintained.

Special Mappers

class odin.mapping.helpers.NoOpMapper[source]

Helper that provides the mapper interface performs no operation on the object.

This is used with the MapListOf and MapDictAs fields when both contain the same Resource type.