Skip to main content

Autogenerate mappings between dataclasses

Project description

pypi version supported Python version licence Read the documentation at https://dataclass-mapper.readthedocs.io/en/latest/ build status Code coverage

Writing mapper methods between two similar dataclasses is boring, need to be actively maintained and are error-prone. Much better to let this library auto-generate them for you.

The focus of this library is:

  • Concise and easy syntax:

    • using it has to be a lot less overhead than writing the mappers by hand

    • trivial mappings should not require code

    • identical syntax for mapping between dataclasses and Pydantic models

  • Safety:

    • using this library must give equal or more type safety than writing the mappers by hand

    • the types between source and target classes must matches (including optional checks)

    • all target fields must be actually initialized

    • mappings cannot reference non-existing fields

    • in case of an error a clean exception must be raised

  • Performance:

    • mapping an object using this library must be the same speed than mapping using a custom mapper function

    • the type checks shouldn’t slow down the program

    • because of the first two points, all type checks and the generation of the mapper functions happen during the definition of the classes

Motivation

A couple of example usecases, that show why this library might be useful.

  • Given an API with multiple, different interfaces (e.g. different API versions), that are all connected to a common algorithm with some common datamodel. All the different API models needs to be mapped to the common datamodel, and afterwards mapped back to the API model.

  • Given an API that has a POST and a GET endpoint. Both models (POST request body model and GET response body model) are almost the same, but there are some minor differences. E.g. response model has an additional id parameter. You need a way of mapping the request model to a response model.

Installation

dataclass-mapper can be installed using:

pip install dataclass-mapper
# or for Pydantic support
pip install 'dataclass-mapper[pydantic]'

Small Example

We have the following target data structure, a class called Person.

>>> from dataclasses import dataclass

>>> @dataclass
... class Person:
...     first_name: str
...     second_name: str
...     age: int

We want to have a mapper from the source data structure, a class called ContactInfo. Notice that the attribute second_name of Person is called surname in ContactInfo. Other than that, all the attribute names are the same.

Instead of writing a mapper to_Person by hand:

>>> @dataclass
... class ContactInfo:
...     first_name: str
...     surname: str
...     age: int
...
...     def to_Person(self) -> Person:
...         return Person(
...             first_name=self.first_name,
...             second_name=self.surname,
...             age=self.age,
...         )

>>> contact = ContactInfo(first_name="Henry", surname="Kaye", age=42)
>>> contact.to_Person()
Person(first_name='Henry', second_name='Kaye', age=42)

you can let the mapper autogenerate with:

>>> from dataclass_mapper import map_to, mapper
>>>
>>> @mapper(Person, {"second_name": "surname"})
... @dataclass
... class ContactInfo:
...     first_name: str
...     surname: str
...     age: int
>>>
>>> contact = ContactInfo(first_name="Henry", surname="Kaye", age=42)
>>> map_to(contact, Person)
Person(first_name='Henry', second_name='Kaye', age=42)

The dataclass-mapper library autogenerated some a mapper, that can be used with the map_to function. All we had to specify was the name of the target class, and optionally specify which fields map to which other fields. Notice that we only had to specify that the second_name field has to be mapped to surname, all other fields were mapped automatically because the field names didn’t change.

And the dataclass-mapper library will perform a lot of checks around this mapping. It will check if the data types match, if some fields would be left uninitialized, etc.

Features

The current version has support for:

License

The project is released under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dataclass_mapper-1.5.3.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

dataclass_mapper-1.5.3-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file dataclass_mapper-1.5.3.tar.gz.

File metadata

  • Download URL: dataclass_mapper-1.5.3.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.16 Linux/5.15.0-1024-azure

File hashes

Hashes for dataclass_mapper-1.5.3.tar.gz
Algorithm Hash digest
SHA256 ece540b92a48b39d0e27c5b1310678fa953116a95b8d23b9671dc89f7d222d46
MD5 2570e80ec6bb809d7b459725cd5cd0cf
BLAKE2b-256 1d2592a4474939b8d95aca1f9d4929cdf3079c60bc4fec2d022f471a10276948

See more details on using hashes here.

File details

Details for the file dataclass_mapper-1.5.3-py3-none-any.whl.

File metadata

  • Download URL: dataclass_mapper-1.5.3-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.16 Linux/5.15.0-1024-azure

File hashes

Hashes for dataclass_mapper-1.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 20dbc81ce88eff272cd8d9409e9b9fa173c482855d189dc9a550acc7363cc7dd
MD5 69bcb62f2ef82d9ffbe54896c13c736d
BLAKE2b-256 540198711ce5d2783c502d7be27e0ad35df2d17cf64f4fb4a96fc8c66fee5436

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page