py-automapper: Object Mapping Library

2.2.0 · active · verified Fri Apr 17

py-automapper is a Python library designed for automatically mapping data from one object to another, reducing boilerplate code in data transfer operations. It is currently at version 2.2.0 and receives active maintenance with several releases per year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to map a SourceDTO object to a UserDomainModel. It includes mapping configuration for different field names and a function configuration for custom value transformations, specifically concatenating first and last names into a full name.

from py_automapper import automap

class SourceDTO:
    def __init__(self, first_name: str, last_name: str, age: int):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

class UserDomainModel:
    def __init__(self, full_name: str, years_old: int):
        self.full_name = full_name
        self.years_old = years_old

source_data = SourceDTO(first_name="Alice", last_name="Smith", age=30)

# Map with custom field names and a transformation function
domain_model = automap(
    source_data,
    UserDomainModel,
    mapping_cfg={
        "first_name": "full_name",
        "last_name": lambda src: src.last_name, # Can also use lambdas for complex transformations
        "age": "years_old"
    },
    func_cfg={
        "full_name": lambda src: f"{src.first_name} {src.last_name}"
    }
)

print(f"Full Name: {domain_model.full_name}")
print(f"Years Old: {domain_model.years_old}")

view raw JSON →