Reusable DTO mapping patterns with type safety
Project description
Python DTO Mappers
Reusable DTO mapping patterns with type safety and auto-mapping functionality.
Installation
pip install python-dto-mappers
Features
- BaseMapper Protocol: Type-safe mapper interface
- Auto-Mapping: Automatic field mapping with @auto_map decorator
- Field Transformations: Common transformations (snake_case ↔ camelCase, datetime ↔ ISO)
- Partial Updates: Extract changed fields for PATCH operations
- Chained Mapping: ORM → Domain → DTO transformations
Usage
Basic Mapper
from pydantic import BaseModel
from python_dto_mappers import BaseMapper
class UserEntity(BaseModel):
id: int
name: str
email: str
class UserDTO(BaseModel):
id: int
name: str
email: str
class UserMapper(BaseMapper[UserEntity, UserDTO]):
def map(self, source: UserEntity) -> UserDTO:
return UserDTO(
id=source.id,
name=source.name,
email=source.email
)
Auto-Mapping
from python_dto_mappers import auto_map
@auto_map(UserEntity, UserDTO)
class UserAutoMapper:
def transform_name(self, source):
# Custom transformation for specific field
return source.name.upper()
mapper = UserAutoMapper()
dto = mapper.map(user_entity)
Field Transformations
from python_dto_mappers import snake_to_camel, map_datetime_to_iso
# Case conversion
camel = snake_to_camel("user_name") # "userName"
snake = camel_to_snake("userName") # "user_name"
# Datetime conversion
iso_str = map_datetime_to_iso(datetime.now())
dt = map_iso_to_datetime("2024-01-01T12:00:00")
Partial Updates (PATCH)
from python_dto_mappers import extract_changed_fields
original = User(id=1, name="John", email="john@example.com")
update = UserUpdateDTO(name="Jane", email="john@example.com")
changed = extract_changed_fields(original, update)
# {'name': 'Jane'} - only changed fields
Chained Mapping
from python_dto_mappers import chain_map
# ORM → Domain → DTO
dto = chain_map(
orm_user,
through=[DomainUser, UserDTO]
)
API Reference
Mapper[TSource, TTarget] (Protocol)
Type-safe mapper protocol.
BaseMapper[TSource, TTarget]
Base mapper class with type information.
@auto_map(source_type, target_type, exclude=None)
Auto-generate mapping for matching fields.
Parameters:
source_type: Source model typetarget_type: Target model typeexclude: Set of fields to exclude
@field_transform(field_name)
Mark method as field transformation.
Field Transformers
snake_to_camel(snake_str): snake_case → camelCasecamel_to_snake(camel_str): camelCase → snake_casemap_datetime_to_iso(dt): datetime → ISO stringmap_iso_to_datetime(iso_str): ISO string → datetimemap_nested_object(obj, mapper): Map nested object
Utils
extract_changed_fields(original, update_dto, exclude): Extract changed fieldschain_map(source, through): Chain multiple mappings
Dependencies
pydantic>=2.0.0typing-extensions>=4.0.0
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_dto_mappers-0.1.0.tar.gz.
File metadata
- Download URL: python_dto_mappers-0.1.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
162be7aa4d85ee6ee60f0aafa04458b40abc4f7e77376e514904f7b31a923a33
|
|
| MD5 |
a0368f6ea336a0a3621b324cf0faf877
|
|
| BLAKE2b-256 |
b57d2b36aceb5ad3544a8d5c1cfaf93bbe04d248ff99c5b228ffc50b19c38ae4
|
File details
Details for the file python_dto_mappers-0.1.0-py3-none-any.whl.
File metadata
- Download URL: python_dto_mappers-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29fda6b768042384a358f3316598294cfe32d3efe22db5d3017133837321d271
|
|
| MD5 |
55c7a4fdf399b7e046885109e01ca6e6
|
|
| BLAKE2b-256 |
e631ca2ee05d8113a38c8c6bed973f299053f3bc85bb1a195f754a4f287cda48
|