objectmapper

A small module for mapping between object types.

Installation

objectmapper requires Python >= 3.6 because it relies on modern type annotations.

pip install objectmapper

Example

>>> import objectmapper
>>> mapper = objectmapper.ObjectMapper()
>>> # One-to-one mappings
>>> def int_to_str(i: int) -> str:
...     return str(i)
>>> mapper.create_map(int, str, int_to_str)
>>> mapper.map(42, str)
'42'
>>> # Many-to-one mappings
>>> def int_float_to_str(i: int, f: float) -> str:
...     return str(i + f)
>>> mapper.create_map((int, float), str, int_float_to_str)
>>> mapper.map((42, 42.5), str)
'84.5'

API

Documentation

objectmapper.create_map(input_types, output_type, map_function=None, force=False)[source]

Stores a mapping (map_function) from objects of types input_types to an object of type output_type. If force is True, then any pre-existing mapping from input_types to output_type is overwritten.

>>> # One-to-one
>>> objectmapper.create_map(int, str, lambda i: str(i))
>>> objectmapper.map(42, str)
'42'
>>> # Many-to-one
>>> objectmapper.create_map((int, float), str, lambda i, j: str(i + j))
>>> objectmapper.map((25, 25.1), str)
'50.1'

Can also be used as a _decorator_

>>> @objectmapper.create_map((int, float), str)
... def int_float_to_str(i: int, j: float) -> str:
...     return str(i + j)
>>> objectmapper.map((25, 25.1), str)
'50.1'

MapTypeError is raised if and of the input_types or output_type are not types

MapFunctionTypeError is raised if map_function is not callable.

DuplicateMappingError is raised if there is a pre-existing mapping from input_types to output_type and force is False.

objectmapper.map(input_instances, output_type)[source]

Returns an object of type output_type by giving *input_instances to the mapping from map(type, input_instances) to output_type.

>>> import objectmapper
>>> # One-to-one
>>> @objectmapper.create_map(int, str)
... def longform_int_to_str(i: int) -> str:
...     digits = (str(x) for x in range(10))
...     words = ['zero', 'one', 'two', 'three', 'four',
...              'five', 'six', 'seven', 'eight', 'nine']
...     digit_to_word = {d: w for d, w in zip(digits, words)}
...     return ' '.join(digit_to_word[c] for c in str(i))
>>> objectmapper.map(451, str)
'four five one'
>>> # Many-to-one
>>> @objectmapper.create_map((int, int), str)
... def longform_int_int_to_str(i: int, j: int) -> str:
...     return ' '.join(map(longform_int_to_str, [i, j]))
>>> objectmapper.map((451, 234), str)
'four five one two three four'

Raises MapInputKeyError if there are no mappings from map(type, input_instances).

Raises MapOutputKeyError if there are no mappings from map(type, input_instances) to output_type.

class objectmapper.ObjectMapper[source]

Class for recording and accessing mappings between object types. This will often be a singleton in user code.

create_map(input_types, output_type, map_function=None, force=False)[source]

Stores a localized mapping between types. See create_map.

map(input_instances, output_type)[source]

Converts input_instances using a mapping from map(type, input_instances) to output_type. See map.

exception objectmapper.DuplicateMappingError(input_types, output_type, mapping)[source]

Raised when a mapping between classes is redundantly defined without forcing an overwrite.

exception objectmapper.ObjectMapperError[source]

Parent exception class

exception objectmapper.MapTypeError(invalid_type)[source]

Raised when attempting to create an invalid mapping.

exception objectmapper.MapInputKeyError(input_types)[source]

Raised when mapping an object of an unknown type.

exception objectmapper.MapOutputKeyError(input_types, output_type)[source]

Raised when mapping an object to an unknown type.