advanced-automapper
Object automapper based on type hints.
Installation
Using pip:
pip install advanced-automapper
Using poetry
poetry add advanced-automapper
Get started
It is important to note that PyAutomapper requieres that both origin and destination classes have have type hints to define the type for every field.
Let's say you have a Pydantic model called Person, and you need to map it to a SqlAlchmey model to save it to a database:
from enum import Enum
from typing import Dict, List, Optional
from pydantic import BaseModel
from sqlalchemy import Enum as SqlEnum
from sqlalchemy import ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, declarative_base, mapped_column, relationship
class GenderPydantic(Enum):
MALE = 1
FEMALE = 2
FURRY = 3
OTHER = 4
class PersonPydantic(BaseModel):
name: str
age: int
gender: GenderPydantic
Base = declarative_base()
class GenderAlchemy(Enum):
MALE = 1
FEMALE = 2
FURRY = 3
OTHER = 4
class PersonAlchemy(Base):
__tablename__ = "persons"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(50), nullable=False)
age: Mapped[int] = mapped_column(Integer)
gender: Mapped[GenderAlchemy] = mapped_column(
SqlEnum(GenderAlchemy), nullable=False
)
def __repr__(self):
return f"<PersonAlchemy(name='{self.name}', age={self.age}, gender='{self.gender}')>"
# Create a person
person = PersonPydantic(name="John", age=25, gender=GenderPydantic.MALE)
To create a PersonAlchemy object:
from automapper import mapper
mapped_person = mapper.map(person, PersonAlchemy)
print(mapped_person)
Add custom mapping
PyAutomapper allows to map fields with different names between them using custom mapping.
Imagine that, in the previous SqlAlchemy class the gender field is called "genero":
from enum import Enum
from typing import Dict, List, Optional
from pydantic import BaseModel
from sqlalchemy import Enum as SqlEnum
from sqlalchemy import ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, declarative_base, mapped_column, relationship
class GenderPydantic(Enum):
MALE = 1
FEMALE = 2
FURRY = 3
OTHER = 4
class PersonPydantic(BaseModel):
name: str
age: int
gender: GenderPydantic
Base = declarative_base()
class GenderAlchemy(Enum):
MALE = 1
FEMALE = 2
FURRY = 3
OTHER = 4
class PersonAlchemy(Base):
__tablename__ = "persons"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(50), nullable=False)
age: Mapped[int] = mapped_column(Integer)
# Let's rename this field
genero: Mapped[GenderAlchemy] = mapped_column(
SqlEnum(GenderAlchemy), nullable=False
)
def __repr__(self):
return f"<PersonAlchemy(name='{self.name}', age={self.age}, gender='{self.gender}')>"
# Create a person
person = PersonPydantic(name="John", age=25, gender=GenderPydantic.MALE)
The solution is to add a cutom mapping in the Mapper relating the field "gender", in the source class, with "genero" in the target.
from automapper import mapper
mapper.add_custom_mapping(PersonPydantic, "gender", "genero")
mapped_person = mapper.map(person, PersonAlchemy)
print(mapped_person)
More examples
The tests folder in the code repository contains examples of mapping between different python objects.
Metadata
Release files for advanced-automapper 0.1.15
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| advanced_automapper-0.1.15.tar.gz | 5.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| advanced_automapper-0.1.15-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.6 kB
Release files / advanced_automapper-0.1.15.tar.gz
| Download URL | advanced_automapper-0.1.15.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
feb7d085aab547ed146896d5b724a05507020a615ce3c165a340af8849560a9d
|
|
BLAKE2b-256 checksum How to use checksums |
64394cd923607ce0c1cded90b7a1591539aa37bf949731198bbc91065b6a5067
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.12.6 Linux/6.8.0-1014-azure
|
Release files / advanced_automapper-0.1.15-py3-none-any.whl
| Download URL | advanced_automapper-0.1.15-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9d289db08fc5d9ceb59783f62e5c9125abaec9e5c3883fea4ee329dacf6ce9c7
|
|
BLAKE2b-256 checksum How to use checksums |
748d57d06f0895f785320e5d158092c4974fb500c3f836b0698fcf59badbfab6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.12.6 Linux/6.8.0-1014-azure
|