kickerde_api_client.mapping

Mapping and transformation features.

Submodules

Attributes

Submapper

Function that accepts a key-value pair and returns another

SubmapperKey

Immutable key that describes a location in an object hierarchy.

Classes

XmlMappingHelper

Mapping helper designed to be plugged into

Functions

map_default(key, value)

Mapping helper that maps the id property to an integer and

Package Contents

kickerde_api_client.mapping.Submapper: TypeAlias = Callable[[str, Any], tuple[str, Any] | None]

Function that accepts a key-value pair and returns another (transformed) key-value pair. Used for on-the-fly post-processing while mapping an object hierarchy from a source to a target format.

A Submapper is conceptually similar to, but not the same thing as, a callback function used in the postprocessor kwarg of the xmltodict.parse() function. A submapper is less flexible than a postprocessor kwarg because the latter accepts a complex path that points to an element location in the object hierarchy. On the other hand, a submapper assumes a fixed (implied) location, so it only accepts key and value as arguments but not the element’s location in the hierarchy.

(The name Submapper was chosen to emphasize that any given function of this type is usually focused on mapping a specific sub-element in an object hierarchy.)

A submapper may return None instead of a key-value pair. If a caller receives None as a return value, it is supposed to discard the element, i.e. not include it in the target hierarchy.

kickerde_api_client.mapping.SubmapperKey: TypeAlias = str | tuple[str, ...]

Immutable key that describes a location in an object hierarchy. Used internally to look up functions of the Submapper type by location.

If the key is a string, then the key matches a location if and only if the element name is equal to the key.

Example: The key “baz” matches elements located at foo.bar.baz and qux.baz. However, it does not match qux.baz.quux.

If the key is a tuple of strings, then it matches a location if and only if the tuple is equal to the chain of element names.

Example: The key (“foo”, “bar”, “baz”) matches elements located at foo.bar.baz. It does not match any other elements.

class kickerde_api_client.mapping.XmlMappingHelper(submappers)

Mapping helper designed to be plugged into xmltodict.parse().

The purpose of this helper class is to aggregate and dispatch Submapper functions for various elements in an XML document while the document is being parsed.

The map() method of this class is designed to be plugged into the postprocessor kwarg of the xmltodict.parse() function.

Parameters:

submappers (collections.abc.Mapping[SubmapperKey, Submapper]) –

filter_rules: collections.abc.MutableSequence[collections.abc.Callable[Ellipsis, bool]]
submappers
map(path, original_key, value)

Transforms an XML key-value pair into a Pythonic type that matches the domain type as closely as possible.

Parameters:
  • path (collections.abc.Sequence[tuple[str, Ellipsis]]) –

  • original_key (str) –

  • value (Any) –

Return type:

tuple[str, Any] | None

kickerde_api_client.mapping.map_default(key, value)

Mapping helper that maps the id property to an integer and keeps everything else as is.

Parameters:
  • key (str) –

  • value (Any) –

Return type:

tuple[str, str | int]